3v4l.org

run code in 300+ PHP versions simultaneously
<?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(); ?>

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).

VersionSystem time (s)User time (s)Memory (MiB)
5.6.120.0070.07720.98
5.6.110.0100.05721.07
5.6.100.0100.08021.04
5.6.90.0070.08021.19
5.6.80.0200.06720.30
5.5.280.0030.04720.83
5.5.270.0030.06020.95
5.5.260.0130.08020.82
5.5.250.0030.06320.76
5.5.240.0000.05320.25
5.4.440.0410.06219.51
5.4.430.0370.06219.44
5.4.420.0410.05719.53
5.4.410.0430.05819.25
5.4.400.0440.05419.06
5.4.390.0470.05119.20
5.4.380.0400.05818.91
5.4.370.0400.05119.07
5.4.360.0380.06019.24
5.4.350.0480.05719.15
5.4.340.0380.05619.07
5.4.320.0400.06119.08
5.4.310.0420.06619.19
5.4.300.0470.06219.10
5.4.290.0440.04619.19
5.4.280.0400.05919.20
5.4.270.0340.05719.14
5.4.260.0460.05319.34
5.4.250.0490.06619.24
5.4.240.0430.06619.05
5.4.230.0400.05919.23
5.4.220.0330.06019.18
5.4.210.0490.05419.15
5.4.200.0330.05216.75
5.4.190.0400.05119.18
5.4.180.0380.05719.07
5.4.170.0370.06019.11
5.4.160.0400.04918.99
5.4.150.0330.06319.02
5.4.140.0370.06416.51
5.4.130.0440.05216.46
5.4.120.0370.05116.50
5.4.110.0380.05016.35
5.4.100.0360.05316.60
5.4.90.0420.04916.67
5.4.80.0390.05316.49
5.4.70.0360.05016.48
5.4.60.0380.04716.55
5.4.50.0410.05116.55
5.4.40.0460.04616.47
5.4.30.0400.05016.54
5.4.20.0380.04916.36
5.4.10.0360.05416.46
5.4.00.0470.05416.17
5.3.290.0410.06214.84
5.3.280.0410.04914.68
5.3.270.0410.05614.72
5.3.260.0360.05714.64
5.3.250.0420.04914.72
5.3.240.0410.04814.76
5.3.230.0430.04914.66
5.3.220.0360.05114.61
5.3.210.0360.05714.60
5.3.200.0370.04914.70
5.3.190.0430.05314.61
5.3.180.0360.05214.59
5.3.170.0310.06014.58
5.3.160.0370.05214.65
5.3.150.0390.04914.65
5.3.140.0370.05214.59
5.3.130.0440.05114.56
5.3.120.0390.05714.59
5.3.110.0400.05414.64
5.3.100.0380.05014.17
5.3.90.0400.05214.09
5.3.80.0400.04914.11
5.3.70.0370.05714.16
5.3.60.0370.05114.11
5.3.50.0390.05714.02
5.3.40.0380.06113.96
5.3.30.0330.06214.00
5.3.20.0440.05613.95
5.3.10.0380.05013.73
5.3.00.0470.05213.66
5.2.170.0340.05011.24
5.2.160.0340.04311.23
5.2.150.0290.04411.17
5.2.140.0310.04411.19
5.2.130.0300.04011.24
5.2.120.0290.04211.24
5.2.110.0330.03811.11
5.2.100.0270.04411.10
5.2.90.0330.04211.12
5.2.80.0400.03211.20
5.2.70.0360.04911.22
5.2.60.0390.03911.28
5.2.50.0300.04211.04
5.2.40.0300.03711.09
5.2.30.0270.04611.02
5.2.20.0230.04011.01
5.2.10.0230.04110.95
5.2.00.0200.04810.87
5.1.60.0260.03410.05
5.1.50.0220.03710.14
5.1.40.0280.03410.13
5.1.30.0240.03810.40
5.1.20.0300.04310.43
5.1.10.0290.03910.11
5.1.00.0260.04110.16
5.0.50.0140.0298.62
5.0.40.0160.0348.43
5.0.30.0180.0398.18
5.0.20.0120.0278.20
5.0.10.0100.0308.11
5.0.00.0140.0448.28
4.4.90.0180.0215.83
4.4.80.0160.0205.87
4.4.70.0120.0235.86
4.4.60.0130.0215.85
4.4.50.0120.0225.88
4.4.40.0170.0325.81
4.4.30.0160.0195.82
4.4.20.0130.0265.87
4.4.10.0160.0245.92
4.4.00.0130.0335.88
4.3.110.0160.0195.82
4.3.100.0100.0235.81
4.3.90.0100.0265.81
4.3.80.0130.0375.81
4.3.70.0140.0215.81
4.3.60.0120.0275.81
4.3.50.0130.0245.81
4.3.40.0080.0405.81
4.3.30.0120.0215.81
4.3.20.0040.0285.81
4.3.10.0070.0235.81
4.3.00.0030.0239.38

preferences:
144.37 ms | 1386 KiB | 7 Q