douwei8295 2013-12-18 21:40
浏览 39
已采纳

如何在ZF2中使用Yelp的API?

I'm trying to connect to Yelp's API, currently using ZF2 and ZendOAuth. I don't know why I'm getting a 404. Here is the raw request and response headers.

POST /v2/search?term=tacos&location=sf HTTP/1.1
Host: api.yelp.com
Connection: close
Accept-Encoding: gzip, deflate
User-Agent: Zend\Http\Client
Content-Type: application/x-www-form-urlencoded
Authorization: OAuth realm="",oauth_consumer_key="<key>",oauth_nonce="<nonce>",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1387401249",oauth_version="1.0",oauth_token="<token>",oauth_signature="<signature>"

HTTP/1.1 404 Not Found
Date: Wed, 18 Dec 2013 21:14:09 GMT
Server: Apache
X-Node: web41, api_com
Content-Length: 8308
Vary: User-Agent
Connection: close
Content-Type: text/html; charset=UTF-8
X-Mode: rw
X-Proxied: lb1

Does that request look like it should connect somewhere?

Here's some code.

    $accessToken = new \ZendOAuth\Token\Access();
    $accessToken->setToken('<token>');
    $accessToken->setTokenSecret('<secret>');
    $host = 'http://' . $_SERVER['HTTP_HOST'];
    $config = array(
        'consumerKey'=>'<key>',
        'consumerSecret'=>'<secret>',
    );
    $client = $accessToken->getHttpClient($config);
    $client->setUri('http://api.yelp.com/v2/search?term=tacos&location=sf');
    $client->setMethod('POST');
    $adapter = new \Zend\Http\Client\Adapter\Socket();
    $client->setAdapter($adapter);
    $response = $client->send();
    $result = $response->getBody();  

All the examples of OAuth I've seen get the access token with a request token, but Yelp already gave me the token and secret, so I'm trying to construct it manually.


Update: Changing

    $client->setMethod('POST');

to

    $client->setMethod('GET');

is the first step, but the parameters can't be added manually to the URL, they have to be added with setParameterGet();. So here's my updated working code.

$accessToken = new \ZendOAuth\Token\Access();
$accessToken->setToken('<token>');
$accessToken->setTokenSecret('<secret>');
$host = 'http://' . $_SERVER['HTTP_HOST'];
$config = array(
    'consumerKey'=>'<key>',
    'consumerSecret'=>'<secret>',
);
$client = $accessToken->getHttpClient($config);
$client->setUri('http://api.yelp.com/v2/search');
$client->setMethod('GET');
$params = array('term'=>'tacos', 'location'=>'sf');                                                                                                                  
$client->setParameterGet($params);
$adapter = new \Zend\Http\Client\Adapter\Socket();
$client->setAdapter($adapter);
$response = $client->send();
$result = $response->getBody(); 
  • 写回答

1条回答 默认 最新

  • doumi2106 2013-12-18 22:11
    关注

    That api requires GET method. So change:

    $client->setMethod('POST');
    

    To:

    $client->setMethod('GET');
    

    And try again )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示