如何在ZF2中使用Yelp的API?

时间:2013-12-18 21:40:33

标签: php http rest zend-framework2 yelp

我正在尝试连接到Yelp的API,目前正在使用ZF2和ZendOAuth。我不知道为什么我得到404.这是原始请求和响应标题。

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

请求看起来应该连接到哪里吗?

这是一些代码。

    $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();  

我看到的所有OAuth示例都获得了带有请求令牌的访问令牌,但Yelp已经给了我令牌和秘密,所以我试图手动构建它。


更新 改变

    $client->setMethod('POST');

    $client->setMethod('GET');

是第一步,但无法手动将参数添加到网址中,必须使用setParameterGet();添加参数。所以这是我更新的工作代码。

$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 个答案:

答案 0 :(得分:0)

那api需要GET方法。所以改变:

$client->setMethod('POST');

要:

$client->setMethod('GET');

再试一次)