由于Twitter API 1.0于2013年6月11日退休,下面的脚本不再工作。
// Create curl resource
$ch = curl_init();
// Set url
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/myscreenname.json?count=10");
// Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// Close curl resource to free up system resources
curl_close($ch);
if ($output)
{
$tweets = json_decode($output,true);
foreach ($tweets as $tweet)
{
print_r($tweet);
}
}
如何用最少的代码获得user_timeline(最近的状态)?
我找到了这个:https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
但是我得到了以下错误:
"{"errors":[{"message":"Could not authenticate you","code":32}]}"
有很多课程,但在尝试了几个之后,它们似乎都没有工作,因为Twitter上的这些更新,加上其中一些是非常高级的课程,有很多功能,我真的不需要。
用PHP获取最近用户状态的最简单/最短的方法是什么?
首先我要感谢jimbo和(他的post / twitter-api-php简单库)。
如果你打算使用GET搜索/tweets API和"twitter-api-php" PHP库(TwitterAPIExchange.php):
首先,您必须注释“执行POST请求并回显响应”代码区域。
只需使用“执行GET请求并回显响应”代码并回显响应并更改这两行:
$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?screen_name=J7mbo';
to
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=J7mbo';
(将screen_name改为q,就是这样:)
首先我要感谢jimbo和(他的post / twitter-api-php简单库)。
如果你打算使用GET搜索/tweets API和"twitter-api-php" PHP库(TwitterAPIExchange.php):
首先,您必须注释“执行POST请求并回显响应”代码区域。
只需使用“执行GET请求并回显响应”代码并回显响应并更改这两行:
$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?screen_name=J7mbo';
to
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=J7mbo';
(将screen_name改为q,就是这样:)
感谢这个帖子,尤其是budidino,因为他的代码让我明白了这一点。
只是想贡献一下如何从请求中检索JSON数据。
更改“//create request”请求数组部分的代码以执行不同的请求。最终,这将把JSON输出到浏览器屏幕上
<?php
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;
}
function returnTweet(){
$oauth_access_token = "2602299919-lP6mgkqAMVwvHM1L0Cplw8idxJzvuZoQRzyMkOx";
$oauth_access_token_secret = "wGWny2kz67hGdnLe3Uuy63YZs4nIGs8wQtCU7KnOT5brS";
$consumer_key = "zAzJRrPOj5BvOsK5QhscKogVQ";
$consumer_secret = "Uag0ujVJomqPbfdoR2UAWbRYhjzgoU9jeo7qfZHCxR6a6ozcu1";
$twitter_timeline = "user_timeline"; // mentions_timeline / user_timeline / home_timeline / retweets_of_me
// create request
$request = array(
'screen_name' => 'burownrice',
'count' => '3'
);
$oauth = array(
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
// merge request and oauth to one array
$oauth = array_merge($oauth, $request);
// do some magic
$base_info = buildBaseString("https://api.twitter.com/1.1/statuses/$twitter_timeline.json", '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 request
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => "https://api.twitter.com/1.1/statuses/$twitter_timeline.json?". http_build_query($request),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return $json;
}
$tweet = returnTweet();
echo $tweet;
?>
访问dev.twitter.com并创建一个应用程序。这将为您提供所需的证书。下面是我最近用PHP和cURL编写的一个实现。
<?php
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;
}
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$oauth_access_token = "YOURVALUE";
$oauth_access_token_secret = "YOURVALUE";
$consumer_key = "YOURVALUE";
$consumer_secret = "YOURVALUE";
$oauth = array( 'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0');
$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), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json);
//print it out
print_r ($twitter_data);
?>
这可以从命令行运行:
$ php <name of PHP script>.php