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 * @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); } } function sendTweet($mensaje){ ini_set('display_errors', 1); /** Set access tokens here - see: https://dev.twitter.com/apps/ **/ $settings = array( 'oauth_access_token' => "4504615637-kc5wZ8R2J4STqx0EV1K1yDQeZlsDxUwX0dFmsN8", 'oauth_access_token_secret' => "ZMoySIqNzs4xtFrwjCSflxqhQZm27UG7Tn1irVDmecKu5", 'consumer_key' => "5owc9irNZhx6wbJbY2NyYZi6X", 'consumer_secret' => "FNtmNO4TCQcCmcaIOm42FDvEsttWD6bhXW4QrEuY6Hcj9IFuAT" ); $url = 'https://api.twitter.com/1.1/statuses/update.json'; $requestMethod = 'POST'; /** POST fields required by the URL above. See relevant docs as above **/ $postfields = array( 'status' => $mensaje, ); /** Perform a POST request and echo the response **/ $twitter = new TwitterAPIExchange($settings); return $twitter->buildOauth($url, $requestMethod)->setPostfields($postfields)->performRequest(); } $mensaje = "Tutorial realizado con éxito en @GeekyTheory. #PHP + #Twitter: Cómo enviar tweets desde PHP | http://geekytheory.com/php-twitter-como-enviar-tweets-desde-php"; $respuesta = sendTweet($mensaje); $json = json_decode($respuesta); echo '<meta charset="utf-8">'; echo "Tweet Enviado por: ".$json->user->name." (@".$json->user->screen_name.")"; echo "<br>"; echo "Tweet: ".$json->text; echo "<br>"; echo "Tweet ID: ".$json->id_str; echo "<br>"; echo "Fecha Envio: ".$json->created_at; ?>

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)
7.4.00.0080.00814.87
7.3.120.0130.00614.93
7.3.110.0140.00314.96
7.3.100.0030.01015.03
7.3.90.0090.00614.69
7.3.80.0120.00314.96
7.3.70.0030.01015.07
7.3.60.0090.00314.85
7.3.50.0060.01014.85
7.3.40.0060.00315.02
7.3.30.0070.00714.93
7.3.20.0040.01116.55
7.3.10.0100.00716.90
7.3.00.0090.00616.75
7.2.250.0070.01015.07
7.2.240.0120.00814.97
7.2.230.0130.00315.25
7.2.220.0080.00615.21
7.2.210.0120.00315.15
7.2.200.0040.00815.05
7.2.190.0110.00415.17
7.2.180.0140.00415.22
7.2.170.0060.00615.03
7.2.60.0090.00616.83
7.2.00.0000.01519.47
7.1.330.0040.01115.77
7.1.320.0100.00615.72
7.1.310.0060.00816.09
7.1.300.0000.01315.54
7.1.290.0130.00315.68
7.1.280.0000.01015.77
7.1.270.0160.00015.60
7.1.260.0060.00315.50
7.1.200.0070.01015.92
7.1.100.0090.00917.93
7.1.70.0040.00417.38
7.1.60.0070.01619.27
7.1.50.0030.01716.75
7.1.00.0070.07322.44
7.0.200.0000.01016.82
7.0.100.0130.07320.03
7.0.90.0300.07319.95
7.0.80.0030.05019.94
7.0.70.0070.04720.02
7.0.60.0070.08320.11
7.0.50.0030.07720.43
7.0.40.0070.08019.99
7.0.30.0100.05720.13
7.0.20.0070.06320.08
7.0.10.0070.09020.08
7.0.00.0100.08320.08
5.6.280.0030.06321.22
5.6.250.0200.04720.75
5.6.240.0100.04320.77
5.6.230.0100.07720.70
5.6.220.0100.08020.55
5.6.210.0030.08720.70
5.6.200.0030.05321.14
5.6.190.0170.07021.02
5.6.180.0100.05321.07
5.6.170.0200.07721.05
5.6.160.0030.08321.11
5.6.150.0130.07721.09
5.6.140.0030.06021.20
5.6.130.0070.05721.11
5.6.120.0170.06721.23
5.6.110.0170.07721.11
5.6.100.0030.04321.03
5.6.90.0030.08720.97
5.6.80.0030.09020.46
5.6.70.0130.07720.41
5.6.60.0070.07320.55
5.6.50.0030.04720.54
5.6.40.0070.08720.55
5.6.30.0070.07020.56
5.6.20.0230.06020.38
5.6.10.0030.08020.37
5.6.00.0100.06020.37
5.5.380.0030.04720.37
5.5.370.0130.07320.54
5.5.360.0130.07020.37
5.5.350.0100.06720.43
5.5.340.0100.06020.95
5.5.330.0100.08020.90
5.5.320.0000.08320.90
5.5.310.0130.07720.85
5.5.300.0170.07720.98
5.5.290.0070.09020.79
5.5.280.0070.05020.90
5.5.270.0130.08020.93
5.5.260.0070.04320.96
5.5.250.0100.07320.68
5.5.240.0070.05020.21
5.5.230.0000.04720.30
5.5.220.0070.06720.25
5.5.210.0030.05320.34
5.5.200.0100.07020.06
5.5.190.0070.08020.29
5.5.180.0100.07720.29
5.5.160.0030.08320.21
5.5.150.0070.06320.30
5.5.140.0130.07320.30
5.5.130.0170.06720.05
5.5.120.0100.04720.34
5.5.110.0030.06720.24
5.5.100.0030.07020.17
5.5.90.0170.04020.11
5.5.80.0070.07720.16
5.5.70.0130.05020.16
5.5.60.0000.07320.21
5.5.50.0100.04320.03
5.5.40.0170.07320.12
5.5.30.0100.04320.00
5.5.20.0170.06720.06
5.5.10.0100.06020.20
5.5.00.0030.05020.19

preferences:
35.61 ms | 400 KiB | 5 Q