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 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题