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条回答 默认 最新

相关推荐 更多相似问题