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 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!