Twitter OAUTH - 返回响应代码“0”
我尝试使用 Matt Harris 的 Twitter OAUTH 库 (https://github.com/themattharris/tmhOAuth) 用我的密钥和令牌替换默认数据,但由于某种原因我无法获得有效的响应代码。
我正在测试的网址以端口(8888)结尾,但我不确定这是否与它有关。我正在跟踪 PHP 日志,没有任何错误。
$tweet_text = 'Hello world!';
print "Posting...\n";
$result = post_tweet($tweet_text);
print "Response code: " . $result . "\n";
function post_tweet($tweet_text) {
require_once('tmhOAuth.php');
$connection = new tmhOAuth(array(
'consumer_key' => '(hidden)',
'consumer_secret' => '(hidden)',
'user_token' => '(hidden)',
'user_secret' => '(hidden)',
));
$connection->request('POST',
$connection->url('1/statuses/update'),
array('status' => $tweet_text));
return $connection->response['code'];
}
有人有什么想法吗?
非常感谢。
I've tried using Matt Harris' Twitter OAUTH library (https://github.com/themattharris/tmhOAuth) replacing default data with my keys and tokens, but for some reason I can't get a valid response code.
The url I'm testing with ends with a port (8888), but I'm not sure if that is to do with it. I'm tailing the PHP log and there are no errors.
$tweet_text = 'Hello world!';
print "Posting...\n";
$result = post_tweet($tweet_text);
print "Response code: " . $result . "\n";
function post_tweet($tweet_text) {
require_once('tmhOAuth.php');
$connection = new tmhOAuth(array(
'consumer_key' => '(hidden)',
'consumer_secret' => '(hidden)',
'user_token' => '(hidden)',
'user_secret' => '(hidden)',
));
$connection->request('POST',
$connection->url('1/statuses/update'),
array('status' => $tweet_text));
return $connection->response['code'];
}
Does anybody have any ideas?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了这个问题,这是由于 2011 年 12 月的最新更改所致:
如果您按照 Darren 的建议转储 $connection->response,您可能会看到一个错误,例如我遇到的这个错误:
确保证书文件 cacert.pem 就位,或禁用 SSL_VERIFYPEER。
我愿意打赌这会解决您的问题(尽管它可能不是最安全的解决方案):
2015 年 2 月更新
今天回顾这个答案,我意识到建议人们将curl_ssl_verifypeer 设置为false 不是一个很好的答案(此时您无法再确定您正在与 Twitter 交谈,因此实际上这是一个糟糕的答案)。相反,请采纳我提供的其他建议并确保适当的根 CA 证书文件 (cacert.pem) 已就位。
I had this problem and it's due to this recent change in December 2011:
If you dump $connection->response as Darren advises, you may see an error such as this one which I had:
Either make sure that the certificate file cacert.pem is in place, or disable SSL_VERIFYPEER.
I'm willing to bet that this will fix your issue (although it may not be the most secure solution):
Update February 2015
Looking back on this answer today, I realise that advising people to set curl_ssl_verifypeer to false is not a very good answer (at this point you can no longer be sure you're talking to Twitter, so in fact it's a terrible answer). Instead, take the other advice I gave and ensure the appropriate Root CA Certificate file (cacert.pem) is in place.
如果与 https://github.com/themattharris/tmhOAuth/blob 进行比较/master/examples/tweet.php 您的代码可能需要更改为如下所示:
但是,查看源代码,可以发现两件事。首先,您的代码应该与此一样好,因为
$this->response['code']
设置为返回的值。其次,该函数(实际上是curlit()
)也可以返回 void。当它发生时,response['code']
是未定义的。 (在我看到这个设计错误之前,这看起来像是一个很有前途的 Twitter 库。)进一步探索,当
$this->config['prevent_request']
时,它只会返回void
code> 存在且为真。你没有这样做,我们又回到了原点,无法解释你所看到的行为。因此,我的下一个故障排除步骤是将
error_reporting(E_ALL|E_NOTICE)
放在顶部,然后检查错误日志以获取更多线索。在调用request()
后,还可以执行print_r($connection->response)
来查看其中还有哪些内容。If you compare with https://github.com/themattharris/tmhOAuth/blob/master/examples/tweet.php your code might need to change to look like:
However, looking at the source code, reveals two things. First your code should be just as good as that, because
$this->response['code']
is set to the value that gets returned. Second that that function (actuallycurlit()
) can also return void. When it does thatresponse['code']
is undefined. (This was looking like a promising twitter library until I saw that design mistake.)Probing even further, it would only return
void
when$this->config['prevent_request']
exists and is true. You're not doing that, and we've gone full circle to not being able to explain the behaviour you see.So, my next troubleshooting step would be to put
error_reporting(E_ALL|E_NOTICE)
at the top, and then check the error logs for more clues. Also do aprint_r($connection->response)
after your call torequest()
to see what else you have in there.看来您需要确保您拥有当前的证书。
根据TMH的git repo:
版本0.60强化了库的安全性,并将curl_ssl_verifypeer默认为true。由于某些托管提供商不提供最新的证书根文件,它现在包含在 TMH 的存储库中。如果版本已过时或者您希望自己下载证书根,您可以从以下位置获取它们: http://curl.haxx.se/ca/cacert.pem
在升级您使用的 tmhOAuth 版本之前,请务必通过运行 Examples/verify_ssl.php 脚本来验证 SSL 处理在您的服务器上是否正常工作。
Looks like you need to make sure you have a current cert.
According to TMH's git repo:
Version 0.60 hardened the security of the library and defaulted curl_ssl_verifypeer to true. As some hosting providers do not provide the most current certificate root file it is now included in TMH's repository. If the version is out of date OR you prefer to download the certificate roots yourself, you can get them from: http://curl.haxx.se/ca/cacert.pem
Before upgrading the version of tmhOAuth that you use, be sure to verify the SSL handling works on your server by running the examples/verify_ssl.php script.