由于 twitter 已从未经身份验证的1.0
API 更改为 OAuth 1.1
,因此如果您想使用该插件,您现在必须通过 PHP 代理 API 请求。
这是代理请求的PHP。在您的站点中创建此页面twitter-proxy.php
,并更新oauth_access_token、oauth_access_token_secret、consumer_key、consumer_secret和screen_name的值以反映您自己的 Twitter 帐户。
如果您需要创建应用程序来获取这些值,请访问https://dev.twitter.com/apps 。
<?php
/* Twitter Proxy for updated OAuth */
$config = array(
//Twitter OAuth config
'oauth_access_token' => 'get from twitter',
'oauth_access_token_secret' => 'get from twitter',
'consumer_key' => 'get from twitter',
'consumer_secret' => 'get from twitter',
'base_url' => 'https://api.twitter.com/1.1/',
//Request specific user
'screen_name' => 'your_twitter_screenname',
'count' => 3
);
$twitter_request = 'statuses/user_timeline.json?screen_name='.$config['screen_name'].'&count='.$config['count'];
// Parse $twitter_request into URL parameters
$url_part = parse_url($twitter_request);
/* url_arguments=
* Array
* (
* [screen_name] => lcherone
* [count] => 3
* )
*/
parse_str($url_part['query'], $url_arguments);
$base_url = $config['base_url'].$url_part['path'];
$full_url = $config['base_url'].$twitter_request;
// Set up the OAuth authorization array
$oauth = array(
'oauth_consumer_key' => $config['consumer_key'],
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $config['oauth_access_token'],
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
// Build vectors for request
$composite_request = _BaseString($base_url, 'GET', array_merge($oauth, $url_arguments));
$composite_key = rawurlencode($config['consumer_secret']).'&'.rawurlencode($config['oauth_access_token_secret']);
$oauth_signature = base64_encode(hash_hmac('sha1', $composite_request, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
// Make cURL Request
$options = array(
CURLOPT_HTTPHEADER => array(_AuthorizationHeader($oauth),'Expect:'),
CURLOPT_HEADER => false,
CURLOPT_URL => $full_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
);
$feed = curl_init();
curl_setopt_array($feed, $options);
$result = curl_exec($feed);
$info = curl_getinfo($feed);
curl_close($feed);
// Send suitable headers to the end user.
if(isset($info['content_type']) && isset($info['size_download'])){
header('Content-Type: '.$info['content_type']);
header('Content-Length: '.$info['size_download']);
}
exit($result);
function _BaseString($base_url, $method, $values) {
$ret = array();
ksort($values);
foreach($values as $key=>$value)
$ret[] = $key."=".rawurlencode($value);
return $method."&".rawurlencode($base_url).'&'.rawurlencode(implode('&', $ret));
}
function _AuthorizationHeader($oauth) {
$ret = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = $key.'="'.rawurlencode($value).'"';
$ret .= implode(', ', $values);
return $ret;
}
?>
现在用下面的函数替换文件build_api_url
中的现有函数jquery.tweets.js
,确保替换 yoursite.com
function build_api_url() {
var proto = ('https:' == document.location.protocol ? 'https:' : 'http:');
var count = (s.fetch === null) ? s.count : s.fetch;
var common_params = '&callback=?';
if (s.list) {
return 'http://yoursite.com/twitter-proxy.php?url='+s.username[0]+"/lists/"+s.list+"/statuses.json?page="+s.page+"&per_page="+count+common_params;
} else if (s.favorites) {
return 'http://yoursite.com/twitter-proxy.php?url=favorites.json?screen_name='+s.username[0]+"&page="+s.page+"&count="+count+common_params;
} else if (s.query === null && s.username.length == 1) {
return 'http://yoursite.com/twitter-proxy.php?url='+encodeURIComponent('statuses/user_timeline.json?screen_name='+s.username[0]+'&count='+count+common_params);
} else {
var query = (s.query || 'from:'+s.username.join(' OR from:'));
return 'http://yoursite.com/twitter-proxy.php?url=/search.json?&q='+encodeURIComponent(query)+'&rpp='+count+'&page='+s.page+common_params;
}
}
2014 年 12 月 17 日更新:自 2014 年 2 月 27 日起,Twitter 要求使用 SSL 连接到其 API 服务器。我已经更新了代码以反映这一点。