无法让推特小部件工作



我想知道是否有人可以提供帮助?

我已经为客户购买了一个网络主题,但无法让 Twitter 小部件正常工作。这可能是非常简单的事情,但由于我不是Web开发人员,我正在努力找出阻止它工作的原因。

这是网页 http://www.blackrocksearch.co.uk/new/- Twitter 提要应显示在页脚中。它适用于模板演示站点,但我在项目评论中注意到其他客户有同样的问题,所以认为某处可能存在故障。

演示它在这里工作的地方:http://vasterad.com/themes/sensation/index.html

这是 twitter.php 文件中的代码片段,这显然是我需要配置的唯一区域(为了安全起见,我省略了实际的访问令牌编号):

    <?php
/**
* Usage:
* Send the url you want to access url encoded in the url paramater, for example (This is with JS):
* /twitter-proxy.php?url='+encodeURIComponent('statuses/user_timeline.json?screen_name=MikeRogers0&count=2')
*/
// The tokens, keys and secrets from the app you created at https://dev.twitter.com/apps
$config = array(
'oauth_access_token' => 'token-here',
'oauth_access_token_secret' => 'token-here',
'consumer_key' => 'token-here',
'consumer_secret' => 'token-here',
'use_whitelist' => true, // If you want to only allow some requests to use this script.
'base_url' => 'http://api.twitter.com/1.1/'
);
/*
* Ok, no more config should really be needed. Yay!
*/
// We'll get the URL from $_GET[]. Make sure the url is url encoded, for example encodeURIComponent('statuses/user_timeline.json?screen_name=MikeRogers0&count=10&include_rts=false&exclude_replies=true')
if(!isset($_GET['url'])){
die('No URL set');
}
$url = $_GET['url'];

if($config['use_whitelist'] && !isset($whitelist[$url])){
die('URL is not authorised');
}
// Figure out the URL parmaters
$url_parts = parse_url($url);
parse_str($url_parts['query'], $url_arguments);
$full_url = $config['base_url'].$url; // Url with the query on it.
$base_url = $config['base_url'].$url_parts['path']; // Url with the query.
/**
* Code below from http://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1 by Rivers
* with a few modfications by Mike Rogers to support variables in the URL nicely
*/
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;
}
// 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'
);
$base_info = buildBaseString($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', $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 => $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']);
}
echo($result);
?>

希望有人能帮忙!

通过在PHP文件上查看对Twitter响应的请求的网站,您的请求似乎需要建立安全连接。所以,我的猜测是:

1. 通过 https 连接

将变量$config上的base_url密钥更改为 https。

$config = array(
    'oauth_access_token' => 'token-here',
    'oauth_access_token_secret' => 'token-here',
    'consumer_key' => 'token-here',
    'consumer_secret' => 'token-here',
    'use_whitelist' => true,
    'base_url' => 'https://api.twitter.com/1.1/' //was http://api.twitter.com/1.1/
);

2. 添加 CA(证书颁发机构)证书文件

如果我的第一个猜测没有解决任何问题,在 cURL 请求中添加 CA 证书可能会有所帮助。首先,从这里获取您自己的cacert.pem。将其保存在您可以识别的路径上。然后只需将 cURL 选项CURLOPT_SSL_VERIFYPEER设置为 true,您也可以选择将 CURLOPT_SSL_VERIFYHOST 显式设置为其默认值 2。

例如,根据您的代码段,保存在与您的Twitter cURL PHP文件相同的路径中。

$options = array(
    CURLOPT_CAINFO => __DIR__ . '/cacert.pem', //or dirname(__FILE__) . '/cacert.pem' for PHP < 5.3
    CURLOPT_HTTPHEADER => $header,
    //CURLOPT_POSTFIELDS => $postfields,
    CURLOPT_HEADER => false,
    CURLOPT_URL => $full_url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYHOST => 2,
    CURLOPT_SSL_VERIFYPEER => true,
);

作为一个选项,您还可以在 php.ini 配置文件中预定义 cacert.pem。只需添加/编辑以下行,也不要忘记路径

curl.cainfo = "some/path/cacert.pem"

让我知道

最新更新