@ 2015-09-25T12:47:32Z <?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
* @version 1.0.4
* @link http://github.com/j7mbo/twitter-api-php
*/
class TwitterAPIExchange
{
/**
* @var string
*/
private $oauth_access_token;
/**
* @var string
*/
private $oauth_access_token_secret;
/**
* @var string
*/
private $consumer_key;
/**
* @var string
*/
private $consumer_secret;
/**
* @var array
*/
private $postfields;
/**
* @var string
*/
private $getfield;
/**
* @var mixed
*/
protected $oauth;
/**
* @var string
*/
public $url;
/**
* @var string
*/
public $requestMethod;
/**
* 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
*
* @throws \Exception When cURL isn't installed or incorrect settings parameters are provided
*
* @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
*
* @throws \Exception When you are trying to set both get and post fields
*
* @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']);
}
foreach ($array as $key => &$value)
{
if (is_bool($value))
{
$value = ($value === true) ? 'true' : 'false';
}
}
$this->postfields = $array;
// rebuild oAuth
if (isset($this->oauth['oauth_signature'])) {
$this->buildOauth($this->url, $this->requestMethod);
}
return $this;
}
/**
* Set getfield string, example: '?screen_name=J7mbo'
*
* @param string $string Get key and value pairs as string
*
* @throws \Exception
*
* @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.');
}
$getfields = preg_replace('/^\?/', '', explode('&', $string));
$params = array();
foreach ($getfields as $field)
{
if ($field !== '')
{
list($key, $value) = explode('=', $field);
$params[$key] = $value;
}
}
$this->getfield = '?' . http_build_query($params);
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
*
* @throws \Exception
*
* @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);
/** In case a null is passed through **/
if (isset($split[1]))
{
$oauth[$split[0]] = urldecode($split[1]);
}
}
}
$postfields = $this->getPostfields();
if (!is_null($postfields)) {
foreach ($postfields as $key => $value) {
$oauth[$key] = $value;
}
}
$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->requestMethod = $requestMethod;
$this->oauth = $oauth;
return $this;
}
/**
* Perform the actual data retrieval from the API
*
* @param boolean $return If true, returns data. This is left in for backward compatibility reasons
* @param array $curlOptions Additional Curl options for this request
*
* @throws \Exception
*
* @return string json If $return param is true, returns json data.
*/
public function performRequest($return = true, $curlOptions = array())
{
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,
) + $curlOptions;
if (!is_null($postfields))
{
$options[CURLOPT_POSTFIELDS] = http_build_query($postfields);
}
else
{
if ($getfield !== '')
{
$options[CURLOPT_URL] .= $getfield;
}
}
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
if (($error = curl_error($feed)) !== '')
{
curl_close($feed);
throw new \Exception($error);
}
curl_close($feed);
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[] = rawurlencode($key) . '=' . rawurlencode($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(array $oauth)
{
$return = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key => $value)
{
if (in_array($key, array('oauth_consumer_key', 'oauth_nonce', 'oauth_signature',
'oauth_signature_method', 'oauth_timestamp', 'oauth_token', 'oauth_version'))) {
$values[] = "$key=\"" . rawurlencode($value) . "\"";
}
}
$return .= implode(', ', $values);
return $return;
}
/**
* Helper method to perform our request
*
* @param string $url
* @param string $method
* @param string $data
* @param array $curlOptions
*
* @throws \Exception
*
* @return string The json response from the server
*/
public function request($url, $method = 'get', $data = null, $curlOptions = array())
{
if (strtolower($method) === 'get')
{
$this->setGetfield($data);
}
else
{
$this->setPostfields($data);
}
return $this->buildOauth($url, $method)->performRequest(true, $curlOptions);
}
}
?>
<?php
$settings = array(
'oauth_access_token' => "1032938784-gWkkFXRJFymiTSdqqrzqMB15RqRDrsCbDVfJCwY",
'oauth_access_token_secret' => "oDXholwkD3Cqc5nLQsIWeB0OhcK9Ui6FioAL6SB0XYsCX",
'consumer_key' => "VFecSGxEI0Agxm9hfbbkVmUhs",
'consumer_secret' => "2buDB5f4Emx1YgPKWXn2htwrnw1Yl2gYTtqdPXX9iR48XU1SXg"
);
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=a2zwebhelp';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
/* Here you will get all info from user timeline */
var_dump(json_decode($response));
?>
<?php
$valid_data = json_decode($response); //JSON data to PHP.
print "<ul>";
foreach ($valid_data as $key=>$value) {
print "<li>";
print $value->text;
print "</li>";
}
print "</ul>";
?>
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) 7.4.1 0.007 0.011 15.02 7.4.0 0.008 0.009 15.20 7.3.13 0.007 0.013 14.94 7.3.12 0.005 0.011 14.97 7.3.11 0.010 0.008 15.13 7.3.10 0.009 0.008 14.89 7.3.9 0.009 0.006 14.93 7.3.8 0.002 0.011 14.89 7.3.7 0.005 0.010 14.95 7.3.6 0.007 0.006 14.91 7.3.5 0.008 0.005 14.91 7.3.4 0.005 0.010 14.88 7.3.3 0.004 0.009 14.83 7.3.2 0.008 0.008 16.59 7.3.1 0.003 0.008 16.67 7.3.0 0.006 0.007 16.76 7.2.26 0.006 0.012 15.02 7.2.25 0.005 0.013 14.97 7.2.24 0.007 0.011 15.21 7.2.23 0.000 0.017 15.30 7.2.22 0.006 0.008 15.12 7.2.21 0.005 0.011 15.13 7.2.20 0.003 0.012 15.25 7.2.19 0.006 0.006 15.13 7.2.18 0.003 0.008 15.29 7.2.17 0.006 0.009 14.88 7.2.16 0.009 0.005 15.14 7.2.15 0.005 0.010 16.81 7.2.14 0.001 0.015 16.89 7.2.13 0.006 0.010 16.59 7.2.12 0.004 0.009 16.97 7.2.11 0.002 0.013 16.89 7.2.10 0.007 0.008 16.76 7.2.9 0.006 0.007 16.94 7.2.8 0.003 0.008 16.90 7.2.7 0.006 0.010 16.88 7.2.6 0.009 0.009 16.75 7.2.5 0.009 0.008 16.57 7.2.4 0.000 0.013 16.85 7.2.3 0.004 0.012 16.70 7.2.2 0.009 0.003 16.71 7.2.1 0.002 0.011 16.89 7.2.0 0.007 0.008 17.78 7.1.33 0.005 0.009 15.78 7.1.32 0.007 0.004 15.73 7.1.31 0.003 0.010 15.69 7.1.30 0.007 0.007 15.79 7.1.29 0.005 0.007 15.77 7.1.28 0.005 0.011 15.61 7.1.27 0.007 0.007 15.54 7.1.26 0.006 0.009 15.68 7.1.25 0.010 0.004 15.64 7.1.24 0.003 0.013 15.69 7.1.23 0.003 0.010 15.65 7.1.22 0.008 0.004 15.58 7.1.21 0.004 0.008 15.56 7.1.20 0.006 0.008 15.76 7.1.19 0.010 0.008 15.52 7.1.18 0.005 0.013 15.75 7.1.17 0.005 0.008 15.87 7.1.16 0.006 0.007 15.70 7.1.15 0.003 0.010 15.77 7.1.14 0.006 0.006 15.74 7.1.13 0.002 0.012 15.63 7.1.12 0.005 0.010 15.59 7.1.11 0.011 0.002 15.84 7.1.10 0.005 0.006 16.52 7.1.9 0.003 0.012 15.80 7.1.8 0.005 0.008 15.65 7.1.7 0.010 0.002 16.07 7.1.6 0.004 0.012 16.82 7.1.5 0.008 0.009 16.00 7.1.4 0.009 0.007 15.69 7.1.3 0.011 0.003 15.79 7.1.2 0.005 0.009 15.87 7.1.1 0.007 0.003 15.67 7.1.0 0.006 0.027 17.94 7.0.33 0.003 0.010 15.29 7.0.32 0.008 0.002 15.37 7.0.31 0.006 0.002 15.52 7.0.30 0.006 0.005 15.40 7.0.29 0.006 0.010 15.47 7.0.28 0.006 0.006 15.32 7.0.27 0.006 0.009 15.30 7.0.26 0.003 0.008 15.50 7.0.25 0.002 0.010 15.35 7.0.24 0.002 0.012 15.39 7.0.23 0.008 0.008 15.37 7.0.22 0.000 0.011 15.36 7.0.21 0.010 0.006 15.26 7.0.20 0.012 0.008 15.10 7.0.19 0.002 0.012 15.36 7.0.18 0.012 0.002 15.38 7.0.17 0.003 0.011 15.57 7.0.16 0.008 0.007 15.27 7.0.15 0.009 0.003 15.34 7.0.14 0.006 0.005 15.28 7.0.13 0.005 0.008 15.32 7.0.12 0.003 0.009 15.44 7.0.11 0.007 0.005 15.43 7.0.10 0.006 0.008 15.38 7.0.9 0.008 0.008 15.30 7.0.8 0.007 0.031 17.00 7.0.7 0.015 0.022 16.93 7.0.6 0.007 0.021 17.02 7.0.5 0.007 0.022 17.12 7.0.4 0.008 0.016 15.58 7.0.3 0.006 0.017 15.48 7.0.2 0.005 0.018 15.52 7.0.1 0.004 0.020 15.71 7.0.0 0.005 0.018 15.74 5.6.40 0.007 0.010 14.30 5.6.39 0.002 0.010 14.38 5.6.38 0.003 0.009 14.32 5.6.37 0.008 0.007 14.43 5.6.36 0.006 0.008 14.32 5.6.35 0.004 0.005 14.36 5.6.34 0.000 0.015 14.29 5.6.33 0.007 0.006 14.41 5.6.32 0.005 0.007 14.31 5.6.31 0.007 0.005 14.45 5.6.30 0.005 0.007 14.45 5.6.29 0.006 0.005 14.50 5.6.28 0.008 0.023 16.43 5.6.27 0.006 0.007 14.40 5.6.26 0.005 0.010 14.43 5.6.25 0.008 0.005 14.30 5.6.24 0.006 0.010 14.44 5.6.23 0.005 0.019 16.48 5.6.22 0.003 0.021 16.48 5.6.21 0.008 0.017 16.35 5.6.20 0.003 0.025 16.63 5.6.19 0.008 0.023 16.60 5.6.18 0.002 0.021 16.68 5.6.17 0.005 0.020 16.69 5.6.16 0.007 0.017 16.64 5.6.15 0.004 0.018 16.70 5.6.14 0.006 0.016 16.53 5.6.13 0.004 0.016 16.61 5.6.12 0.005 0.017 16.58 5.6.11 0.008 0.019 16.70 5.6.10 0.005 0.019 16.69 5.6.9 0.005 0.016 16.48 5.6.8 0.011 0.025 16.24 5.6.7 0.008 0.029 16.24 5.6.6 0.003 0.023 16.21 5.6.5 0.009 0.030 16.36 5.6.4 0.010 0.028 16.42 5.6.3 0.004 0.025 16.44 5.6.2 0.002 0.030 16.37 5.6.1 0.007 0.031 16.38 5.6.0 0.006 0.032 16.34 5.5.38 0.000 0.014 14.37 5.5.37 0.007 0.016 16.57 5.5.36 0.007 0.021 16.36 5.5.35 0.006 0.020 16.25 5.5.34 0.002 0.024 16.51 5.5.33 0.004 0.022 16.59 5.5.32 0.005 0.023 16.62 5.5.31 0.011 0.017 16.47 5.5.30 0.006 0.015 16.41 5.5.29 0.006 0.022 16.32 5.5.28 0.005 0.018 16.61 5.5.27 0.007 0.019 16.48 5.5.26 0.006 0.018 16.49 5.5.25 0.006 0.022 16.57 5.5.24 0.008 0.028 16.35 5.5.23 0.006 0.034 16.31 5.5.22 0.004 0.033 16.30 5.5.21 0.008 0.024 16.14 5.5.20 0.007 0.022 16.39 5.5.19 0.005 0.035 16.32 5.5.18 0.010 0.029 16.23 5.5.17 0.008 0.005 14.23 5.5.16 0.006 0.033 16.28 5.5.15 0.006 0.034 16.19 5.5.14 0.008 0.023 16.21 5.5.13 0.006 0.018 16.22 5.5.12 0.002 0.024 16.37 5.5.11 0.007 0.028 16.12 5.5.10 0.005 0.024 16.24 5.5.9 0.012 0.025 16.23 5.5.8 0.010 0.029 16.23 5.5.7 0.010 0.031 16.34 5.5.6 0.009 0.029 16.08 5.5.5 0.006 0.028 16.08 5.5.4 0.006 0.024 15.95 5.5.3 0.014 0.024 16.08 5.5.2 0.010 0.018 16.23 5.5.1 0.008 0.016 16.18 5.5.0 0.009 0.029 16.23 5.4.45 0.005 0.017 14.25 5.4.44 0.004 0.018 14.17 5.4.43 0.003 0.021 14.22 5.4.42 0.002 0.019 14.38 5.4.41 0.003 0.020 14.19 5.4.40 0.003 0.024 14.08 5.4.39 0.006 0.028 14.07 5.4.38 0.007 0.022 14.09 5.4.37 0.009 0.027 14.05 5.4.36 0.005 0.022 14.16 5.4.35 0.008 0.028 14.18 5.4.34 0.002 0.033 14.05 5.4.33 0.003 0.008 11.66 5.4.32 0.007 0.029 14.15 5.4.31 0.007 0.030 14.14 5.4.30 0.008 0.022 14.12 5.4.29 0.007 0.018 13.98 5.4.28 0.004 0.032 14.06 5.4.27 0.006 0.030 14.04 5.4.26 0.004 0.019 14.01 5.4.25 0.006 0.017 14.11 5.4.24 0.002 0.023 14.16 5.4.23 0.006 0.030 14.09 5.4.22 0.006 0.027 14.11 5.4.21 0.006 0.028 14.21 5.4.20 0.004 0.021 14.11 5.4.19 0.005 0.019 14.15 5.4.18 0.005 0.020 14.12 5.4.17 0.007 0.030 14.15 5.4.16 0.004 0.029 14.09 5.4.15 0.002 0.032 14.09 5.4.14 0.010 0.020 13.25 5.4.13 0.001 0.031 13.28 5.4.12 0.003 0.023 13.24 5.4.11 0.008 0.028 13.27 5.4.10 0.005 0.024 13.31 5.4.9 0.008 0.018 13.32 5.4.8 0.003 0.020 13.35 5.4.7 0.007 0.027 13.20 5.4.6 0.005 0.021 13.30 5.4.5 0.009 0.018 13.17 5.4.4 0.004 0.031 13.32 5.4.3 0.002 0.033 13.23 5.4.2 0.004 0.025 13.23 5.4.1 0.007 0.026 13.28 5.4.0 0.002 0.022 13.13
preferences:dark mode live preview
47.86 ms | 401 KiB | 5 Q