@ 2014-08-05T06:31:55Z <?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 MIT 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()))
{
throw new Exception('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']))
{
throw new Exception('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()))
{
throw new Exception('You can only choose get OR post fields.');
}
if (isset($array['status']) && substr($array['status'], 0, 1) === '@')
{
$array['status'] = sprintf("\0%s", $array['status']);
}
$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()))
{
throw new Exception('You can only choose get OR post fields.');
}
$search = array('#', ',', '+', ':');
$replace = array('%23', '%2C', '%2B', '%3A');
$string = str_replace($search, $replace, $string);
$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 (!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 = $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 actual data retrieval from the API
*
* @param boolean $return If true, returns data.
*
* @return string json If $return param is true, returns json data.
*/
public function performRequest($return = true)
{
if (!is_bool($return))
{
throw new Exception('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_TIMEOUT => 10,
);
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 array $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;
}
}
class Tweet extends TwitterAPIExchange {
//Enter your details here
const oauth_token = "16127841-XhE5Xlw8bArFmSyouf4y9AHx9KUJ2SrpL95994QV0";
const oauth_secret = "wtmbZlSntIXpKqiq4mIDmY1JqqmsIP6FCy1UyHRI9DuJ6";
const consumer_key = "hccV6YwzcYC9bjaTnbC6W6h2D";
const consumer_secret = "OucJEZ0GR6PgGSHZje0DLSmGdSmyXx9b0CWiSGVwla1jzApSdM";
public $json;
public $get;
private $twitter;
public function __construct() {
$settings = array('oauth_access_token' => self::oauth_token,
'oauth_access_token_secret' => self::oauth_secret,
'consumer_key' => self::consumer_key,
'consumer_secret' => self::consumer_secret);
$this->twitter = new TwitterAPIExchange($settings);
return $this;
}
public function set($json, $get) {
$this->json = "https://api.twitter.com/1.1/".$json.".json";
if(is_array($get)) {
$this->get = http_build_query($get);
} else {
$this->get = $get;
}
return $this;
}
public function call() {
$response=$this->twitter->setGetfield("?".$this->get)
->buildOauth($this->json, "GET")
->performRequest();
$response=json_decode($response, true);
return (array) $response;
}
}
echo new Tweet->set("users/show", array("screen_name"=>$user))->call();
?>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 5.6.12 0.007 0.077 20.98 5.6.11 0.010 0.057 21.07 5.6.10 0.010 0.080 21.04 5.6.9 0.007 0.080 21.19 5.6.8 0.020 0.067 20.30 5.5.28 0.003 0.047 20.83 5.5.27 0.003 0.060 20.95 5.5.26 0.013 0.080 20.82 5.5.25 0.003 0.063 20.76 5.5.24 0.000 0.053 20.25 5.4.44 0.041 0.062 19.51 5.4.43 0.037 0.062 19.44 5.4.42 0.041 0.057 19.53 5.4.41 0.043 0.058 19.25 5.4.40 0.044 0.054 19.06 5.4.39 0.047 0.051 19.20 5.4.38 0.040 0.058 18.91 5.4.37 0.040 0.051 19.07 5.4.36 0.038 0.060 19.24 5.4.35 0.048 0.057 19.15 5.4.34 0.038 0.056 19.07 5.4.32 0.040 0.061 19.08 5.4.31 0.042 0.066 19.19 5.4.30 0.047 0.062 19.10 5.4.29 0.044 0.046 19.19 5.4.28 0.040 0.059 19.20 5.4.27 0.034 0.057 19.14 5.4.26 0.046 0.053 19.34 5.4.25 0.049 0.066 19.24 5.4.24 0.043 0.066 19.05 5.4.23 0.040 0.059 19.23 5.4.22 0.033 0.060 19.18 5.4.21 0.049 0.054 19.15 5.4.20 0.033 0.052 16.75 5.4.19 0.040 0.051 19.18 5.4.18 0.038 0.057 19.07 5.4.17 0.037 0.060 19.11 5.4.16 0.040 0.049 18.99 5.4.15 0.033 0.063 19.02 5.4.14 0.037 0.064 16.51 5.4.13 0.044 0.052 16.46 5.4.12 0.037 0.051 16.50 5.4.11 0.038 0.050 16.35 5.4.10 0.036 0.053 16.60 5.4.9 0.042 0.049 16.67 5.4.8 0.039 0.053 16.49 5.4.7 0.036 0.050 16.48 5.4.6 0.038 0.047 16.55 5.4.5 0.041 0.051 16.55 5.4.4 0.046 0.046 16.47 5.4.3 0.040 0.050 16.54 5.4.2 0.038 0.049 16.36 5.4.1 0.036 0.054 16.46 5.4.0 0.047 0.054 16.17 5.3.29 0.041 0.062 14.84 5.3.28 0.041 0.049 14.68 5.3.27 0.041 0.056 14.72 5.3.26 0.036 0.057 14.64 5.3.25 0.042 0.049 14.72 5.3.24 0.041 0.048 14.76 5.3.23 0.043 0.049 14.66 5.3.22 0.036 0.051 14.61 5.3.21 0.036 0.057 14.60 5.3.20 0.037 0.049 14.70 5.3.19 0.043 0.053 14.61 5.3.18 0.036 0.052 14.59 5.3.17 0.031 0.060 14.58 5.3.16 0.037 0.052 14.65 5.3.15 0.039 0.049 14.65 5.3.14 0.037 0.052 14.59 5.3.13 0.044 0.051 14.56 5.3.12 0.039 0.057 14.59 5.3.11 0.040 0.054 14.64 5.3.10 0.038 0.050 14.17 5.3.9 0.040 0.052 14.09 5.3.8 0.040 0.049 14.11 5.3.7 0.037 0.057 14.16 5.3.6 0.037 0.051 14.11 5.3.5 0.039 0.057 14.02 5.3.4 0.038 0.061 13.96 5.3.3 0.033 0.062 14.00 5.3.2 0.044 0.056 13.95 5.3.1 0.038 0.050 13.73 5.3.0 0.047 0.052 13.66 5.2.17 0.034 0.050 11.24 5.2.16 0.034 0.043 11.23 5.2.15 0.029 0.044 11.17 5.2.14 0.031 0.044 11.19 5.2.13 0.030 0.040 11.24 5.2.12 0.029 0.042 11.24 5.2.11 0.033 0.038 11.11 5.2.10 0.027 0.044 11.10 5.2.9 0.033 0.042 11.12 5.2.8 0.040 0.032 11.20 5.2.7 0.036 0.049 11.22 5.2.6 0.039 0.039 11.28 5.2.5 0.030 0.042 11.04 5.2.4 0.030 0.037 11.09 5.2.3 0.027 0.046 11.02 5.2.2 0.023 0.040 11.01 5.2.1 0.023 0.041 10.95 5.2.0 0.020 0.048 10.87 5.1.6 0.026 0.034 10.05 5.1.5 0.022 0.037 10.14 5.1.4 0.028 0.034 10.13 5.1.3 0.024 0.038 10.40 5.1.2 0.030 0.043 10.43 5.1.1 0.029 0.039 10.11 5.1.0 0.026 0.041 10.16 5.0.5 0.014 0.029 8.62 5.0.4 0.016 0.034 8.43 5.0.3 0.018 0.039 8.18 5.0.2 0.012 0.027 8.20 5.0.1 0.010 0.030 8.11 5.0.0 0.014 0.044 8.28 4.4.9 0.018 0.021 5.83 4.4.8 0.016 0.020 5.87 4.4.7 0.012 0.023 5.86 4.4.6 0.013 0.021 5.85 4.4.5 0.012 0.022 5.88 4.4.4 0.017 0.032 5.81 4.4.3 0.016 0.019 5.82 4.4.2 0.013 0.026 5.87 4.4.1 0.016 0.024 5.92 4.4.0 0.013 0.033 5.88 4.3.11 0.016 0.019 5.82 4.3.10 0.010 0.023 5.81 4.3.9 0.010 0.026 5.81 4.3.8 0.013 0.037 5.81 4.3.7 0.014 0.021 5.81 4.3.6 0.012 0.027 5.81 4.3.5 0.013 0.024 5.81 4.3.4 0.008 0.040 5.81 4.3.3 0.012 0.021 5.81 4.3.2 0.004 0.028 5.81 4.3.1 0.007 0.023 5.81 4.3.0 0.003 0.023 9.38
preferences:dark mode live preview
144.37 ms | 1386 KiB | 7 Q