在我的控制器中使用以下代码,我能够将OAuth访问权限传递到J7mbo's TwitterAPIExchange并对Twitter API版本1.1执行查询:
// Set access tokens here
$settings = array(
'oauth_access_token' => "My Oauth Access Token",
'oauth_access_token_secret' => "My Oauth Access Token Secret",
'consumer_key' => "My Consumer Key",
'consumer_secret' => "My Consumer Secret"
);
$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?username=somename';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
但是,出于安全原因,我读到使用者密钥不应该在应用程序中进行人工可更改。所以我将我的OAuth访问权限从我的控制器(见上文)移到了以下位置(见下文):
应用\设置\ twitter_settings.php
<?php if( ! defined('BASEPATH'))exit('No direct script access allowed');
// Set access tokens here
$config['oauth_access_token'] = "My access token";
$config['oauth_access_token_secret'] = "My access secret";
$config['consumer_key'] = "My consumer key";
$config['consumer_secret'] = "My ENCRYPTED consumer_secret";
/* End of file Settings.php */
并尝试从我的控制器加载我的访问权限
$this->config->load('twitter_settings');
$this->load->library('/twitter/TwitterAPIExchange');
现在我收到以下错误,但我不确定如何将我的OAuth访问权限传递给J7mbo's TwitterAPIExchange:
遇到PHP错误
严重程度:4096
消息:传递给TwitterAPIExchange :: __ construct()的参数1必须 是一个数组,没有给出,被称为 C:\ wamp \ www \ twitter_app \ theworks \ core \ Loader.php在1099行和 定义
文件名:twitter / TwitterAPIExchange.php
行号:33
确保传递正确的参数
这是我的控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Search extends CI_Controller {
public function index()
{
$this->config->load('twitter_settings');
//$this->load->library('encrypt');
$this->load->library('/twitter/TwitterAPIExchange');
$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?username=stackexchange';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange(/* I need to pass the OAuth tokens here */);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
}
}
/* End of file search.php */
/* Location: ./application/controllers/search.php */
这里是J7mbo's TwitterAPIExchange代码:
<?php
/**
* Twitter-API-PHP : Simple PHP wrapper for the v1.1 API
*
* PHP version 5.3.10
*
* @category Awesomeness
* @package Twitter-API-PHP
* @author James Mallison <me@j7mbo.co.uk>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @link http://github.com/j7mbo/twitter-api-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;
/**
* Create the API access object. Requires an array of settings::
* oauth access token, oauth access token secret, consumer key, consumer secret
* These are all available by creating your own application on dev.twitter.com
* Requires the cURL library
*
* @param array $settings
*/
public function __construct(array $settings)
{
if (!in_array('curl', get_loaded_extensions()))
{
exit('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
}
if (!isset($settings['oauth_access_token'])
|| !isset($settings['oauth_access_token_secret'])
|| !isset($settings['consumer_key'])
|| !isset($settings['consumer_secret']))
{
exit('Make sure you are passing in the correct parameters');
}
$this->oauth_access_token = $settings['oauth_access_token'];
$this->oauth_access_token_secret = $settings['oauth_access_token_secret'];
$this->consumer_key = $settings['consumer_key'];
$this->consumer_secret = $settings['consumer_secret'];
}
/**
* Set postfields array, example: array('screen_name' => 'J7mbo')
*
* @param array $array Array of parameters to send to API
* @return \TwitterAPIExchange Instance of self for method chaining
*/
public function setPostfields(array $array)
{
if (!is_null($this->getGetfield()))
{
exit('You can only choose get OR post fields.');
}
$this->postfields = $array;
return $this;
}
/**
* Set getfield string, example: '?screen_name=J7mbo'
*
* @param string $string Get key and value pairs as string
* @return \TwitterAPIExchange Instance of self for method chaining
*/
public function setGetfield($string)
{
if (!is_null($this->getPostfields()))
{
exit('You can only choose get OR post fields.');
}
$this->getfield = $string;
return $this;
}
/**
* Get getfield string (simple getter)
*
* @return string $this->getfields
*/
public function getGetfield()
{
return $this->getfield;
}
/**
* Get postfields array (simple getter)
*
* @return array $this->postfields
*/
public function getPostfields()
{
return $this->postfields;
}
/**
* Build the Oauth object using params set in construct and additionals
* passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
*
* @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json
* @param string $requestMethod Either POST or GET
* @return \TwitterAPIExchange Instance of self for method chaining
*/
public function buildOauth($url, $requestMethod)
{
if (strtolower($requestMethod) !== 'post' && strtolower($requestMethod) !== 'get')
{
exit('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 = $this->getGetfield();
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;
$this->url = $url;
$this->oauth = $oauth;
return $this;
}
/**
* Perform the acual data retrieval from the API
*
* @param boolean optional $return If true, returns data.
* @return json If $return param is true, returns json data.
*/
public function performRequest($return = true)
{
if (!is_bool($return))
{
exit('performRequest parameter must be true or false');
}
$header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
$getfield = $this->getGetfield();
$postfields = $this->getPostfields();
$options = array(
CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
);
if (!is_null($postfields))
{
$options[CURLOPT_POSTFIELDS] = $postfields;
}
else
{
if ($getfield !== '')
{
$options[CURLOPT_URL] .= $getfield;
}
}
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
if ($return) { return $json; }
}
/**
* Private method to generate the base string used by cURL
*
* @param string $baseURI
* @param string $method
* @param string $params
* @return string Built base string
*/
private function buildBaseString($baseURI, $method, $params)
{
$return = array();
ksort($params);
foreach($params as $key=>$value)
{
$return[] = "$key=" . $value;
}
return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
}
/**
* Private method to generate authorization header used by cURL
*
* @param array $oauth Array of oauth data generated by buildOauth()
* @return string $return Header used by cURL for request
*/
private function buildAuthorizationHeader($oauth)
{
$return = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key => $value)
{
$values[] = "$key=\"" . rawurlencode($value) . "\"";
}
$return .= implode(', ', $values);
return $return;
}
}
感谢您对Stormdrain的回复。我重写了控制器如下,但我再次遇到同样的错误
$this->load->library('encrypt');
$this->config->load('twitter_settings');
$this->load->library('/twitter/TwitterAPIExchange');
$settings = array(
'oauth_access_token' => $this->config->item('oauth_access_token'),
'oauth_access_token_secret' => $this->config->item('oauth_access_token_secret'),
'consumer_key' => $this->config->item('consumer_key'),
'consumer_secret' => $this->encrypt->decode($this->config->item('consumer_secret'))
);
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
这是错误:
遇到PHP错误
严重程度:4096
消息:传递给TwitterAPIExchange :: __ construct()的参数1必须 是一个数组,没有给出,被称为 C:\ wamp \ www \ twitter_app \ theworks \ core \ Loader.php在1099行和 定义
文件名:twitter / TwitterAPIExchange.php
行号:33
确保传递正确的参数
所以,当我查看twitter / TwitterAPIExchange.php的第33行时,我发现了这个:
/**
* Create the API access object. Requires an array of settings::
* oauth access token, oauth access token secret, consumer key, consumer secret
* These are all available by creating your own application on dev.twitter.com
* Requires the cURL library
*
* @param array $settings
*/
public function __construct(array $settings)
{ //...
答案 0 :(得分:2)
只需使用配置项重做$settings
$this->config->load('twitter_settings');
$this->load->library('/twitter/TwitterAPIExchange');
$settings = array(
'oauth_access_token' => $this->config->('oauth_access_token'),
'oauth_access_token_secret' => $this->config->('oauth_access_token_secret'),
'consumer_key' => $this->config->('consumer_key'),
'consumer_secret' => $this->config->('consumer_secret')
);
$twitter = new TwitterAPIExchange($settings);
答案 1 :(得分:0)
更改库载重线。删除它:
$this->load->library('/twitter/TwitterAPIExchange');
并添加如下库:
require_once APPPATH.'libraries/twitter/TwitterAPIExchange.php';
class Twitter extends CI_Controller{
...
}
这解决了我的问题。我在php codeigniter中使用过它。