php如何使用oauth制作正确的twitter api卷曲

yul*_*ika 6 php twitter curl twitter-oauth

我想用php curloauth从中获取JSON数据twitter.这是我的代码.返回na错误消息"error":"Timestamp out of bounds".

我想知道,如何用oauth做一个正确的推特api卷曲?

  1. 是什么oauth_consumer_key,oauth_token,oauth_nonce,oauth_signature?我对吗?

  2. 怎么解决"error":"Timestamp out of bounds"

  3. 我的curl_setopt方法怎么样?

在很多方面,我想用它php curl来解码其他twitter api(更改的URL).非常感谢.

$callback="<callback url>";
$consumer_key="<Consumer key>";
$consumer_secret="<Consumer secret>";
$oauth_token="<Access Token (oauth_token)>";
$oauth_signature="<Access Token Secret (oauth_token_secret)>";//these key word in my api panel
$time = mktime(date("Y-m-d H:i:s"))-86400;
$url = "https://api.twitter.com/1/friends/ids.json?";
$url .= "user_id=<user id>";
$url .= "&realm=".urlencode($callback)."";
$url .= "&service_provider_id=11";
$url .= "&oauth_consumer_key=".$consumer_key."";
$url .= "&oauth_token=".$oauth_token."";
$url .= "&oauth_nonce=".$consumer_secret."";
$url .= "&oauth_signature_method=HMAC-SHA1";
$url .= "&oauth_timestamp=".$time."";
$url .= "&oauth_version=1.0";
$url .= "&oauth_signature=".$oauth_signature."";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: api.twitter.com'));
$json = curl_exec($ch);
curl_close ($ch);
$data = json_decode($json, true);
print_r($json);
Run Code Online (Sandbox Code Playgroud)

Jef*_*ker 3

您错误地使用了mktime,它不接受日期字符串。然而,我建议你这样做......

$time = time() - 86400;
Run Code Online (Sandbox Code Playgroud)

With regards to your other questions, I recommend getting an oauth capable twitter library for PHP. There are plenty out there, and there's no point in reinventing the wheel ... usually :)

https://dev.twitter.com/docs/auth/oauth/single-user-with-examples