我尝试使用这篇方便的文章将twitter api [搜索]整合到modx网站中:Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
我设置了一个引用api的静态文件,它完美地工作,就像一个魅力。
我将其整合为modx扩展程序&我得到的全部是:
{"errors":[{"message":"Could not authenticate you","code":32}]}
我已经为每个例子抛出了curlopts,它们是相同的:
Array
(
[10023] => Array
(
[0] => Authorization: OAuth oauth_consumer_key="xxx",
oauth_nonce="xxx",
oauth_signature_method="HMAC-SHA1",
oauth_token="xxx",
oauth_timestamp="111111111111",
oauth_version="1.0",
q="%2523BOACanada",
count="3",
oauth_signature="xxx"
[1] => Expect:
)
[42] =>
[10002] => https://api.twitter.com/1.1/search/tweets.json?q=#BOACanada&count=3
[19913] => 1
[13] => 10
)
就像我说这是modx所以我调用组件如:
[[!TwitterExchange? &getfield=`#BOACanada` &count=`3`]]
TwitterExchange片段:
<?php
$output = '';
$exchange_core = $modx->getOption('core_path').'components/TwitterAPIExchange/model/';
$scriptProperties['oauth_access_token'] = "xxxxx";
$scriptProperties['oauth_access_token_secret'] = "xxxxx";
$scriptProperties['consumer_key'] = "xxxxx";
$scriptProperties['consumer_secret'] = "xxxxx";
$scriptProperties['url'] = 'https://api.twitter.com/1.1/search/tweets.json';
$exchange = $modx->getService('twitterapiexchange', 'TwitterAPIExchange', $exchange_core, $scriptProperties);
if (!$exchange instanceof TwitterAPIExchange){
return 'Insantiation failed';
}
$output = $exchange->searchTweets();
return $output;
最后,全班同学。我删除了原始类中未修改的任何内容:
<?php
class TwitterAPIExchange
{
private $oauth_access_token;
private $oauth_access_token_secret;
private $consumer_key;
private $consumer_secret;
private $postfields;
private $getfield;
protected $oauth;
public $url;
public function __construct(modX &$modx,array $scriptProperties){
$this->modx =& $modx;
$this->modx->setLogLevel(xPDO::LOG_LEVEL_ERROR);
$this->modx->setLogTarget('ECHO');
$this->scriptProperties = $scriptProperties;
if (!in_array('curl', get_loaded_extensions()))
{
throw new Exception('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
}
if (!isset($scriptProperties['oauth_access_token'])
|| !isset($scriptProperties['oauth_access_token_secret'])
|| !isset($scriptProperties['consumer_key'])
|| !isset($scriptProperties['consumer_secret']))
{
throw new Exception('Make sure you are passing in the correct parameters');
}
$this->oauth_access_token = $scriptProperties['oauth_access_token'];
$this->oauth_access_token_secret = $scriptProperties['oauth_access_token_secret'];
$this->consumer_key = $scriptProperties['consumer_key'];
$this->consumer_secret = $scriptProperties['consumer_secret'];
}
public function searchTweets(){
$url = $this->scriptProperties['url'];
$getfield = "?q=".$this->scriptProperties['getfield']."&count=".$this->scriptProperties['count']."";
$requestMethod = 'GET';
$qstring = $this->setGetfield($getfield);
$oath = $this->buildOauth($url, $requestMethod, $qstring);
$json = $this->performRequest($getfield, $oath['url'], $oath['oauth']);
echo $json;
}
public function setGetfield($string)
{
$search = array('#', ',', '+', ':');
$replace = array('%23', '%2C', '%2B', '%3A');
$string = str_replace($search, $replace, $string);
return $string;
}
public function buildOauth($url, $requestMethod, $qstring)
{
if (!in_array(strtolower($requestMethod), array('post', 'get')))
{
throw new Exception('Request method must be either POST or GET');
}
$consumer_key = $this->consumer_key;
$consumer_secret = $this->consumer_secret;
$oauth_access_token = $this->oauth_access_token;
$oauth_access_token_secret = $this->oauth_access_token_secret;
$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'
);
$getfield = $qstring;
if (!is_null($getfield))
{
$getfields = str_replace('?', '', explode('&', $getfield));
foreach ($getfields as $g)
{
$split = explode('=', $g);
$oauth[$split[0]] = $split[1];
}
}
$base_info = $this->buildBaseString($url, $requestMethod, $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;
$output = array();
$output['url'] = $url;
$output['oauth'] = $oauth;
return $output;
}
public function performRequest($qstring, $url, $oauth){
$header = array($this->buildAuthorizationHeader($oauth), 'Expect:');
$getfield = $qstring;
$options = array(
CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $url.$getfield,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
);
echo'<pre>';print_r($options);echo'</pre>';
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return $json;
}
}
为什么我收到身份验证错误?