Twitter搜索api 1.1和PHP CURL:无法验证您的身份


Twitter search api 1.1 and PHP CURL: Could not authenticate you

我使用James Mallisons Twitter-API从Github:http://github.com/j7mbo/twitter-api-php

通过身份验证凭据后,我尝试获得定义的地理坐标周围的tweet列表:

$fields = "?q=test&geocode=".urlencode("$lat,$lon,$radius")."&count=100";
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$method = 'GET';
$result = $twitter->buildOauth($url, $method)
    ->setGetfield($fields)
    ->performRequest();

这将在api类中生成以下CURL选项:(带XXX的掩码认证证书)

CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => 'Authorization: OAuth oauth_consumer_key="XXX", oauth_nonce="XXX", oauth_signature_method="HMAC-SHA1", oauth_token="XXX", oauth_timestamp="XXX", oauth_version="1.0", oauth_signature="XXX"',
CURLOPT_URL => "https://api.twitter.com/1.1/search/tweets.json?q=test&geocode=48.39677%2C9.98042%2C35km&count=100",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false

使用此选项发出的请求将导致以下错误:

["message"]=>
  string(26) "Could not authenticate you"
["code"]=>
  int(32)

你知道这个错误是怎么回事吗?从/friends/ids获取我的好友列表。Json工作得很好。所以认证是正确的。似乎是查询参数错误。

看起来作者就在几天前对逗号的编码做了一个关键的修复。https://github.com/J7mbo/twitter-api-php/commit/5c2b97e402d04114ca3be6a793eebcd8521d76a5

我猜这就是为什么你的地理代码调用失败,你的朋友/id很好。最好更新库。

如果你仍然卡住,试试我的

注意:我已经+了Tim的答案,确保你有最新的版本;)

你的问题-不要urlencode!

好吧,你确切的问题是你不需要 urlencode()你的位置的东西。它应该是一个普通的旧式GET请求。

根据docs for geocode:

示例值:37.781157,-122.398720,1mi

将其放入getfield即可:

$getfield = '?q=test&geocode=37.781157,-122.398720,1mi&count=100';

所以确保它是按照文档的格式,你就可以开始了。我将把这个例子也添加到wiki中。