尝试获取Twitter用户的关注者列表。
尝试手动编码版本和使用codebird - codebird版本只是尝试发布测试推文。
每次我收到“无法验证您身份”错误消息。
我已经重复了几次所有的键并重试了 - 仍然会得到同样的错误。
我已尝试设置新帐户,在该帐户中设置新应用,但仍然会收到“无法验证您”的消息。
这两个版本在我上周使用它们时都有效 - 本周,它们都不起作用。
任何帮助都非常感激 - 这是代码,Codebird首先:
// require codebird
require_once('codebird/src/codebird.php');
\Codebird\Codebird::setConsumerKey("consumerkey", "consumersecret");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("oauthtoken", "oauthsecret");
$params = array(
'status' => 'another Auto Post on Twitter with PHP #php #twitter');
$reply = $cb->statuses_update($params);
var_dump($reply);
+++++++++++++++ 这是手写的:
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
$myaccount = "@twitterdev";
$cursor = -1;
$url = "https://api.twitter.com/1.1/followers/list.json";
$consumer_key = "consumerkey";
$consumer_secret = "consumersecret";
$oauth_access_token = "accesstoken";
$oauth_access_token_secret = "accesssecret";
$oauth = array('count' => 200,
'cursor' => $cursor,
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_token' => $oauth_access_token,
'oauth_version' => '1.0',
'screen_name' => $myaccount
);
$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
// Make requests
$header = array(buildAuthorizationHeader($oauth));
foreach($header as $key=>$value){
echo "$key: $value<BR>";
}
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $url.'?screen_name='.$myaccount.'&cursor='.$cursor.'&count=200',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => TRUE);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
var_dump($json);
$twitter_data = json_decode($json,true);
foreach($twitter_data AS $key => $value){
if($key == 'next_cursor_str'){
$next_cursor = $value;
}
if($key == 'users'){
foreach ($value AS $key2 => $value2){
if($key == 'users'){
$id = $value2["id"];
$name = $value2["name"];
$screen_name = $value2["screen_name"];
$description = $value2["description"];
$followers_count = $value2["followers_count"];
$friends_count = $value2["friends_count"];
$statuses_count = $value2["statuses_count"];
$statustime = '00-00-00 00:00:00';
$statustime = @$value2["status"]["created_at"];
$statime = date('Y-m-d H:i:s', strtotime($statustime));
$allfollow[$id]["name"] = mysql_real_escape_string(@$name);
$allfollow[$id]["screen_name"] = mysql_real_escape_string(@$screen_name);
$allfollow[$id]["statuses_time"] = mysql_real_escape_string(@$statime);
$allfollow[$id]["description"] = mysql_real_escape_string(@$description);
$allfollow[$id]["followers_count"] = @$followers_count;
$allfollow[$id]["friends_count"] = @$friends_count;
$allfollow[$id]["statuses_count"] = @$statuses_count;
}
}
}
}
echo "<table>";
echo "<th>Name</th><th>Screen name</th><th>Description</th><th>Followers</th><th>Friends</th><th>Statuses</th><th>Time</th>";
foreach($allfollow AS $key => $value){
echo "<tr>";
echo "<td>{$value[name]}</td>
<td>{$value[screen_name]}</td>
<td>{$value[description]}</td>
<td>{$value[followers_count]}</td>
<td>{$value[friends_count]}</td>
<td>{$value[statuses_count]}</td>
<td> {$value[statuses_time]}</td>";
echo "</tr>";
}
echo "</table>";