php - 需要解析 Yelp 响应 : "Signature was invalid" using PHP code Sample

标签 php cakephp oauth cakephp-2.0 yelp

嘿,我正在尝试将 Yelp API 集成到我的 cakephp 网站中,但出现“签名无效”错误。

我试图通过google和stackoverflow找出问题的原因,但无法解决

我使用以下代码:

public function bar($hostel_data=array()){

    $term = "bar";
    $location = "Berlin";
    $test1 = $this->search($term, $location);   

    debug($test1 );  die();         
}




function yelpRequest($path) {

    $consumer_key = "xxxxxxx";
    $consumer_secret = "xxxxxxxx";
    $token = "xxxxxxxxx";
    $token_secret = "xxxxxxxxx";

    require APPLIBS.'OAuth'.DS.'oAuth.php';


    $unsigned_url = "http://" . "api.yelp.com" . $path;

    // Token object built using the OAuth library
    $token = new OAuthToken($token, $token_secret);

    // Consumer object built using the OAuth library
    $consumer = new OAuthConsumer($consumer_key, $consumer_secret);

    // Yelp uses HMAC SHA1 encoding
    $signature_method = new OAuthSignatureMethod_HMAC_SHA1();

    $oauthrequest = OAuthRequest::from_consumer_and_token(
        $consumer, 
        $token, 
        'GET', 
        $unsigned_url
    );

     // Sign the request
$oauthrequest->sign_request($signature_method, $consumer, $token);

// Get the signed URL
$signed_url = $oauthrequest->to_url();

debug($signed_url);
//utf8_encode($signed_url);

// Send Yelp API Call
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);


return $data;
}


function search($term, $location) {

$search_path ="/v2/search/";

$url_params = array();
$url_params['term'] = $term ?: $GLOBALS['DEFAULT_TERM'];
$url_params['location'] = $location?: $GLOBALS['DEFAULT_LOCATION'];
$url_params['limit'] = 20;

$search_path = $search_path . "?" . http_build_query($url_params);

$yelpResponse = $this->yelpRequest($search_path);

debug($url_params); debug($yelpResponse); die();
return $yelpResponse;
}

调试($signed_url):

http://api.yelp.com/v2/search/?limit=20&location=Berlin&oauth_consumer_key=qHsEZDzSkqfT8aOxr79Isw&oauth_nonce=60b30afc5087fcc509eb4245d1e9410c&oauth_signature=XFZFpzdg36G0eG42TZKy5Ny9BbU%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1406153682&oauth_token=kqq8iu-whGeYubw3Q8o_fNyzUgDEpnrO&oauth_version=1.0&term=bar

错误响应:

'{"error": {"text": "Signature was invalid", "id": "INVALID_SIGNATURE", "description": "Invalid signature. Expected signature base string: GET\u0026http%3A%2F%2Fapi.yelp.com%2Fv2%2Fsearch%2F\u0026limit%3D20%26location%3DBerlin%26oauth_consumer_key%3DqHsEZDzSkqfT8aOxr79Isw%26oauth_nonce%3D60b30afc5087fcc509eb4245d1e9410c%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1406153682%26oauth_token%3Dkqq8iu-whGeYubw3Q8o_fNyzUgDEpnrO%26oauth_version%3D1.0%26term%3Dbar"}}'

我希望有人能提供一些帮助或提示如何解决它......

或者是否有关于 cake/php 的好的教程或插件?我坚持了两天多了:/

最佳答案

在你的代码中:

$search_path ="/v2/search/";

和你的请求 url($signed_url)

  http://api.yelp.com/v2/search/?limit=20&location=Berlin&oauth_consumer_key=qHsEZDzSkqfT8aOxr79Isw&oauth_nonce=60b30afc5087fcc509eb4245d1e9410c&oauth_signature=XFZFpzdg36G0eG42TZKy5Ny9BbU%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1406153682&oauth_token=kqq8iu-whGeYubw3Q8o_fNyzUgDEpnrO&oauth_version=1.0&term=bar

您的网址路径错误。搜索路径应该是:

$search_path ="/v2/search";

请求 url 将更改为:

http://api.yelp.com/v2/search?limit=20&location=Berlin&oauth_consumer_key=qHsEZDzSkqfT8aOxr79Isw&oauth_nonce=60b30afc5087fcc509eb4245d1e9410c&oauth_signature=XFZFpzdg36G0eG42TZKy5Ny9BbU%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1406153682&oauth_token=kqq8iu-whGeYubw3Q8o_fNyzUgDEpnrO&oauth_version=1.0&term=bar

这是我的 git repository documentation 的代码和谷歌文档.

关于php - 需要解析 Yelp 响应 : "Signature was invalid" using PHP code Sample,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24922446/

相关文章:

validation - 在更新表单中禁用验证 - CakePHP

php - CakePHP - 为 Form->input : I want to have a line break, 设置默认值但是如何设置?

node.js - 当我尝试在 Node 中执行 https post 请求时,出现 getaddrinfo ENOTFOUND 错误

oauth - Instagram API : do scopes work with OAuth2 implicit authentication flow?

javascript - 在 PHP 的新选项卡中打开超链接

php - 将压缩后的数据编码为json字符串

html - cakephp 2.x 如何包含来自外部模板 css 的文件

oauth - 如果无法访问服务器,如何安装 oAuth?

php - SQL 查询月份

php - 从两个表中选择行