3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * TwitterOAuth - https://github.com/ricardoper/TwitterOAuth * PHP library to communicate with Twitter OAuth API version 1.1 * * @author Ricardo Pereira <github@ricardopereira.es> * @copyright 2013 */ namespace TwitterOAuth; use TwitterOAuth\Exception\TwitterException; class TwitterOAuth { protected $url = 'https://api.twitter.com/1.1/'; protected $outputFormats = array('text', 'json', 'array', 'object'); protected $defaultFormat = 'object'; protected $config = array(); protected $call = ''; protected $method = 'GET'; protected $getParams = array(); protected $postParams = array(); /** * Prepare a new conection with Twitter API via OAuth * * @params array $config Configuration array with OAuth access data */ public function __construct(array $config) { $required = array( 'consumer_key' => 'DYIUG3zgoOtujvpDuBNtXw', 'consumer_secret' => 'jIThRGktrPrvjzOGjNYxJMqdemimXQIlBmDMMk77j4', 'oauth_token' => '1358758812-uE8bDrcbTYC3F8eL7pKhuz3yOGo6dkgJlTgc1m4', 'oauth_token_secret' => 'U7JOzRhtbHSwPA1ObCy6amtnJU4ADXLfYxusy85Z6hmwS' ); if (count(array_intersect_key($required, $config)) !== count($required)) { throw new \Exception('Missing parameters in configuration array'); } if (!isset($config['output_format']) || !in_array($config['output_format'], $this->outputFormats)) { $config['output_format'] = $this->defaultFormat; } $this->config = $config; unset($required, $config); } /** * Send a GET call to Twitter API via OAuth * * @param string $call Twitter resource string * @param array $getParams GET parameters to send * @return mixed Output with selected format */ public function get($call, array $getParams = null) { $this->call = $call; if ($getParams !== null && is_array($getParams)) { $this->getParams = $getParams; } return $this->sendRequest(); } /** * Send a POST call to Twitter API via OAuth * * @param string $call Twitter resource string * @param array $postParams POST parameters to send * @param array $getParams GET parameters to send * @return mixed Output with selected format */ public function post($call, array $postParams = null, array $getParams = null) { $this->call = $call; $this->method = 'POST'; if ($postParams !== null && is_array($postParams)) { $this->postParams = $postParams; } if ($getParams !== null && is_array($getParams)) { $this->getParams = $getParams; } return $this->sendRequest(); } /** * Converting parameters array to a single string with encoded values * * @param array $params Input parameters * @return string Single string with encoded values */ protected function getParams(array $params) { $r = ''; ksort($params); foreach ($params as $key => $value) { $r .= '&' . $key . '=' . rawurlencode($value); } unset($params, $key, $value); return trim($r, '&'); } /** * Getting full URL from a Twitter resource * * @param bool $withParams If true then parameters will be outputted * @return string Full URL */ protected function getUrl($withParams = false) { $getParams = ''; if ($withParams === true) { $getParams = $this->getParams($this->getParams); if (!empty($getParams)) { $getParams = '?' . $getParams; } } return $this->url . $this->call . '.json' . $getParams; } /** * Getting OAuth parameters to be used in request headers * * @return array OAuth parameters */ protected function getOauthParameters() { $time = time(); return array( 'oauth_consumer_key' => $this->config['consumer_key'], 'oauth_nonce' => trim(base64_encode($time), '='), 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => $time, 'oauth_token' => $this->config['oauth_token'], 'oauth_version' => '1.0' ); } /** * Converting all parameters arrays to a single string with encoded values * * @return string Single string with encoded values */ protected function getRequestString() { $params = array_merge($this->getParams, $this->postParams, $this->getOauthParameters()); $params = $this->getParams($params); return rawurlencode($params); } /** * Getting OAuth signature base string * * @return string OAuth signature base string */ protected function getSignatureBaseString() { $method = strtoupper($this->method); $url = rawurlencode($this->getUrl()); return $method . '&' . $url . '&' . $this->getRequestString(); } /** * Getting a signing key * * @return string Signing key */ protected function getSigningKey() { return $this->config['consumer_secret'] . '&' . $this->config['oauth_token_secret']; } /** * Calculating the signature * * @return string Signature */ protected function calculateSignature() { return base64_encode(hash_hmac('sha1', $this->getSignatureBaseString(), $this->getSigningKey(), true)); } /** * Converting OAuth parameters array to a single string with encoded values * * @return string Single string with encoded values */ protected function getOauthString() { $oauth = array_merge($this->getOauthParameters(), array('oauth_signature' => $this->calculateSignature())); ksort($oauth); $values = array(); foreach ($oauth as $key => $value) { $values[] = $key . '="' . rawurlencode($value) . '"'; } $oauth = implode(', ', $values); unset($values, $key, $value); return $oauth; } /** * Building request HTTP headers * * @return array HTTP headers */ protected function buildRequestHeader() { return array( 'Authorization: OAuth ' . $this->getOauthString(), 'Expect:' ); } /** * Processing Twitter Exceptions in case of error * * @param string $type Depends of response format (array|object) * @param mixed $ex Exceptions * @throws Exception\TwitterException */ protected function processExceptions($type, $ex) { switch ($type) { case 'array': foreach ($ex['errors'] as $error) { throw new TwitterException($error['message'], $error['code']); } break; default: foreach ($ex->errors as $error) { throw new TwitterException($error->message, $error->code); } } unset($tupe, $ex, $error); } /** * Outputs the response in the selected format * * @param string $response * @return mixed */ protected function processOutput($response) { $format = $this->config['output_format']; switch ($format) { case 'text': if (substr($response, 2, 6) == 'errors') { $response = json_decode($response); $this->processExceptions('object', $response); } break; case 'json': if (!headers_sent()) { header('Cache-Control: no-cache, must-ridate'); header('Expires: Tue, 19 May 1981 00:00:00 GMT'); header('Content-type: application/json'); } if (substr($response, 2, 6) == 'errors') { $response = json_decode($response); $this->processExceptions('object', $response); } break; case 'array': $response = json_decode($response, true); if (isset($response['errors'])) { $this->processExceptions('array', $response); } break; default: $response = json_decode($response); if (isset($response->errors)) { $this->processExceptions('object', $response); } } unset($format); return $response; } /** * Send GET or POST requests to Twitter API * * @throws Exception\TwitterException * @return mixed Response output */ protected function sendRequest() { $url = $this->getUrl(true); $header = $this->buildRequestHeader(); $options = array( CURLOPT_URL => $url, CURLOPT_HEADER => false, CURLOPT_HTTPHEADER => $header, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, ); if (!empty($this->postParams)) { $options[CURLOPT_POST] = count($this->postParams); $options[CURLOPT_POSTFIELDS] = $this->getParams($this->postParams); } $c = curl_init(); curl_setopt_array($c, $options); $response = curl_exec($c); curl_close($c); unset($options, $c); if (!in_array($response[0], array('{', '['))) { throw new TwitterException(str_replace(array("\n", "\r", "\t"), '', strip_tags($response)), 0); } return $this->processOutput($response); } }

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)
8.0.110.0080.00016.88
8.0.100.0000.00816.88
8.0.90.0080.00016.93
8.0.80.0090.01216.99
8.0.70.0040.00416.90
8.0.60.0030.00616.91
8.0.50.0080.00016.73
8.0.30.0120.00816.98
8.0.20.0070.01117.40
8.0.10.0000.00817.04
8.0.00.0120.00816.66
7.4.240.0070.00016.50
7.4.230.0000.00716.47
7.4.220.0090.00916.72
7.4.210.0080.00816.60
7.4.200.0040.00316.54
7.4.190.0030.00316.46
7.4.160.0100.01016.33
7.4.150.0130.00617.40
7.4.140.0060.01217.86
7.4.130.0110.00716.48
7.4.120.0080.01116.59
7.4.110.0130.00616.42
7.4.100.0120.00616.40
7.4.90.0140.00316.52
7.4.80.0090.00919.39
7.4.70.0100.01316.39
7.4.60.0090.00916.47
7.4.50.0030.00616.45
7.4.40.0130.00322.77
7.4.30.0100.00716.47
7.4.10.0040.01415.03
7.4.00.0030.01414.96
7.3.300.0000.00716.32
7.3.290.0030.01016.28
7.3.280.0090.01016.33
7.3.270.0090.00917.40
7.3.260.0150.00418.24
7.3.250.0120.00616.57
7.3.240.0120.00616.56
7.3.230.0100.00716.52
7.3.210.0060.01116.40
7.3.200.0090.01519.39
7.3.190.0100.01516.29
7.3.180.0090.00616.41
7.3.170.0090.01316.50
7.3.160.0140.00916.31
7.3.130.0070.01114.75
7.3.120.0050.01214.84
7.3.110.0130.00614.80
7.3.100.0100.00714.94
7.3.90.0060.00915.08
7.3.80.0080.00014.98
7.3.70.0030.00914.43
7.3.60.0000.01614.62
7.3.50.0040.00814.55
7.3.40.0000.00814.86
7.3.30.0070.00714.91
7.3.20.0040.01116.31
7.3.10.0030.00916.68
7.3.00.0030.00716.56
7.2.330.0060.01216.61
7.2.320.0070.01116.56
7.2.310.0120.00616.58
7.2.300.0120.00616.66
7.2.290.0100.00716.56
7.2.260.0030.01614.61
7.2.250.0040.01414.93
7.2.240.0090.00615.12
7.2.230.0080.00814.70
7.2.220.0040.00814.89
7.2.210.0000.01314.95
7.2.200.0000.01014.91
7.2.190.0040.01114.69
7.2.180.0040.01114.85
7.2.170.0000.01615.04
7.2.160.0060.00914.82
7.2.150.0060.00316.80
7.2.140.0060.00916.93
7.2.130.0040.01016.76
7.2.120.0080.00316.77
7.2.110.0050.00416.66
7.2.100.0040.00916.42
7.2.90.0050.00916.68
7.2.80.0110.00516.57
7.2.70.0050.01116.78
7.2.60.0080.00716.63
7.2.50.0070.00816.84
7.2.40.0090.00716.70
7.2.30.0050.00716.74
7.2.20.0030.01216.67
7.2.10.0060.00616.63
7.2.00.0040.01217.54
7.1.330.0080.00615.73
7.1.320.0040.00815.38
7.1.310.0080.00815.87
7.1.300.0000.01415.78
7.1.290.0050.00315.56
7.1.280.0000.01215.60
7.1.270.0040.00815.69
7.1.260.0110.00315.68
7.1.250.0080.00315.53
7.1.240.0040.01115.63
7.1.230.0110.00015.59
7.1.220.0100.01015.51
7.1.210.0090.00915.50
7.1.200.0110.00415.55
7.1.190.0030.00915.54
7.1.180.0070.00715.63
7.1.170.0110.00015.52
7.1.160.0000.01515.62
7.1.150.0060.00915.40
7.1.140.0030.01415.73
7.1.130.0030.00915.41
7.1.120.0040.01215.21
7.1.110.0070.00715.66
7.1.100.0090.00516.76
7.1.90.0090.00315.49
7.1.80.0060.00915.62
7.1.70.0030.00916.34
7.1.60.0100.00617.29
7.1.50.0080.00616.29
7.1.40.0040.01215.63
7.1.30.0070.00715.21
7.1.20.0030.01315.34
7.1.10.0100.00315.56
7.1.00.0070.02819.02
7.0.330.0140.00015.42
7.0.320.0000.01215.12
7.0.310.0040.00815.42
7.0.300.0030.00615.28
7.0.290.0060.00615.15
7.0.280.0080.00414.86
7.0.270.0060.00615.14
7.0.260.0040.00414.98
7.0.250.0130.00015.12
7.0.240.0040.00815.28
7.0.230.0090.00615.43
7.0.220.0060.00615.46
7.0.210.0040.00815.34
7.0.200.0020.00815.85
7.0.190.0100.00315.21
7.0.180.0000.01315.26
7.0.170.0070.00415.16
7.0.160.0040.01215.50
7.0.150.0030.00615.21
7.0.140.0050.04118.52
7.0.130.0000.01315.41
7.0.120.0060.00915.15
7.0.110.0040.00715.23
7.0.100.0060.02617.63
7.0.90.0060.02717.68
7.0.80.0050.04517.63
7.0.70.0070.04817.74
7.0.60.0080.02517.75
7.0.50.0220.04117.85
7.0.40.0090.04216.66
7.0.30.0130.04016.69
7.0.20.0080.03616.75
7.0.10.0080.04016.68
7.0.00.0080.04816.50
5.6.400.0030.00714.05
5.6.390.0000.01514.30
5.6.380.0070.01114.40
5.6.370.0110.00314.03
5.6.360.0030.00614.25
5.6.350.0080.00414.01
5.6.340.0040.01114.21
5.6.330.0070.00714.09
5.6.320.0060.00913.89
5.6.310.0160.00014.45
5.6.300.0100.00014.25
5.6.290.0080.00814.00
5.6.280.0070.04017.56
5.6.270.0070.01014.36
5.6.260.0030.00913.96
5.6.250.0070.04717.61
5.6.240.0100.03217.46
5.6.230.0070.04317.56
5.6.220.0050.05117.56
5.6.210.0080.04317.45
5.6.200.0060.04917.72
5.6.190.0050.03117.62
5.6.180.0150.03917.57
5.6.170.0030.05017.47
5.6.160.0070.04517.53
5.6.150.0040.04417.82
5.6.140.0090.04217.55
5.6.130.0030.04617.64
5.6.120.0090.02217.71
5.6.110.0090.04517.79
5.6.100.0100.04417.70
5.6.90.0050.04617.62
5.6.80.0080.04217.31
5.6.70.0110.02217.31
5.6.60.0070.04417.32
5.6.50.0070.04617.33
5.6.40.0100.04317.47
5.6.30.0020.04617.28
5.6.20.0080.04317.13
5.6.10.0030.04017.30
5.6.00.0030.04317.29
5.5.380.0080.04217.29
5.5.370.0090.03617.34
5.5.360.0070.04417.36
5.5.350.0070.03517.43
5.5.340.0050.03417.33
5.5.330.0130.02317.56
5.5.320.0070.04017.46
5.5.310.0150.02317.45
5.5.300.0070.04717.54
5.5.290.0140.03717.43
5.5.280.0070.04617.46
5.5.270.0060.02617.57
5.5.260.0050.04917.45
5.5.250.0080.04217.38
5.5.240.0060.04117.31
5.5.230.0050.03817.11
5.5.220.0080.03017.18
5.5.210.0020.04217.21
5.5.200.0120.03717.23
5.5.190.0080.04317.09
5.5.180.0040.04917.08
5.5.170.0030.00914.00
5.5.160.0070.04417.17
5.5.150.0060.04017.17
5.5.140.0070.03217.28
5.5.130.0050.03017.12
5.5.120.0100.04017.37
5.5.110.0120.03517.21
5.5.100.0060.02817.19
5.5.90.0080.02517.16
5.5.80.0090.02217.17
5.5.70.0000.04917.04
5.5.60.0100.03817.12
5.5.50.0080.02917.00
5.5.40.0060.02716.94
5.5.30.0110.03916.90
5.5.20.0100.03517.07
5.5.10.0160.03817.14
5.5.00.0070.04616.92
5.4.450.0050.05215.49
5.4.440.0080.04815.41
5.4.430.0070.04115.44
5.4.420.0070.04015.32
5.4.410.0070.03515.29
5.4.400.0060.04115.24
5.4.390.0070.04315.24
5.4.380.0090.04115.29
5.4.370.0060.04015.31
5.4.360.0090.03215.14
5.4.350.0120.03515.30
5.4.340.0050.03615.30
5.4.330.0000.00711.41
5.4.320.0080.02715.32
5.4.310.0050.04115.32
5.4.300.0100.01815.30
5.4.290.0080.04115.15
5.4.280.0070.04015.30
5.4.270.0060.02515.23
5.4.260.0080.04015.25
5.4.250.0070.04415.27
5.4.240.0020.04215.17
5.4.230.0070.03915.16
5.4.220.0080.02315.28
5.4.210.0000.04515.16
5.4.200.0080.03715.23
5.4.190.0050.04215.31
5.4.180.0060.04515.32
5.4.170.0020.03115.33
5.4.160.0070.03215.13
5.4.150.0050.04215.27
5.4.140.0030.04313.90
5.4.130.0090.03413.91
5.4.120.0070.02013.94
5.4.110.0090.03213.92
5.4.100.0050.03713.98
5.4.90.0060.03613.95
5.4.80.0030.03313.97
5.4.70.0060.01613.95
5.4.60.0020.03613.93
5.4.50.0030.03913.89
5.4.40.0100.01813.97
5.4.30.0020.02513.95
5.4.20.0030.04013.93
5.4.10.0050.02713.91
5.4.00.0070.03713.67
5.3.290.0020.03913.16
5.3.280.0050.04513.06
5.3.270.0050.03813.02
5.3.260.0040.04213.02
5.3.250.0100.03413.13
5.3.240.0000.03713.02
5.3.230.0050.04313.04
5.3.220.0050.03213.05
5.3.210.0070.03613.03
5.3.200.0100.02813.03
5.3.190.0080.03613.04
5.3.180.0050.02713.07
5.3.170.0020.04313.03
5.3.160.0080.01513.06
5.3.150.0030.01913.02
5.3.140.0080.02313.05
5.3.130.0050.03213.09
5.3.120.0080.03213.04
5.3.110.0050.02513.02
5.3.100.0080.03512.78
5.3.90.0020.04012.77
5.3.80.0050.04212.77
5.3.70.0060.04112.76
5.3.60.0000.04312.72
5.3.50.0080.03712.79
5.3.40.0080.03712.73
5.3.30.0040.03112.67
5.3.20.0050.03812.61
5.3.10.0020.03812.54
5.3.00.0050.04212.56

preferences:
36.29 ms | 400 KiB | 5 Q