@ 2015-12-19T06:47:25Z <?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;
?>
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.0 0.008 0.008 14.87 7.3.12 0.013 0.006 14.93 7.3.11 0.014 0.003 14.96 7.3.10 0.003 0.010 15.03 7.3.9 0.009 0.006 14.69 7.3.8 0.012 0.003 14.96 7.3.7 0.003 0.010 15.07 7.3.6 0.009 0.003 14.85 7.3.5 0.006 0.010 14.85 7.3.4 0.006 0.003 15.02 7.3.3 0.007 0.007 14.93 7.3.2 0.004 0.011 16.55 7.3.1 0.010 0.007 16.90 7.3.0 0.009 0.006 16.75 7.2.25 0.007 0.010 15.07 7.2.24 0.012 0.008 14.97 7.2.23 0.013 0.003 15.25 7.2.22 0.008 0.006 15.21 7.2.21 0.012 0.003 15.15 7.2.20 0.004 0.008 15.05 7.2.19 0.011 0.004 15.17 7.2.18 0.014 0.004 15.22 7.2.17 0.006 0.006 15.03 7.2.6 0.009 0.006 16.83 7.2.0 0.000 0.015 19.47 7.1.33 0.004 0.011 15.77 7.1.32 0.010 0.006 15.72 7.1.31 0.006 0.008 16.09 7.1.30 0.000 0.013 15.54 7.1.29 0.013 0.003 15.68 7.1.28 0.000 0.010 15.77 7.1.27 0.016 0.000 15.60 7.1.26 0.006 0.003 15.50 7.1.20 0.007 0.010 15.92 7.1.10 0.009 0.009 17.93 7.1.7 0.004 0.004 17.38 7.1.6 0.007 0.016 19.27 7.1.5 0.003 0.017 16.75 7.1.0 0.007 0.073 22.44 7.0.20 0.000 0.010 16.82 7.0.10 0.013 0.073 20.03 7.0.9 0.030 0.073 19.95 7.0.8 0.003 0.050 19.94 7.0.7 0.007 0.047 20.02 7.0.6 0.007 0.083 20.11 7.0.5 0.003 0.077 20.43 7.0.4 0.007 0.080 19.99 7.0.3 0.010 0.057 20.13 7.0.2 0.007 0.063 20.08 7.0.1 0.007 0.090 20.08 7.0.0 0.010 0.083 20.08 5.6.28 0.003 0.063 21.22 5.6.25 0.020 0.047 20.75 5.6.24 0.010 0.043 20.77 5.6.23 0.010 0.077 20.70 5.6.22 0.010 0.080 20.55 5.6.21 0.003 0.087 20.70 5.6.20 0.003 0.053 21.14 5.6.19 0.017 0.070 21.02 5.6.18 0.010 0.053 21.07 5.6.17 0.020 0.077 21.05 5.6.16 0.003 0.083 21.11 5.6.15 0.013 0.077 21.09 5.6.14 0.003 0.060 21.20 5.6.13 0.007 0.057 21.11 5.6.12 0.017 0.067 21.23 5.6.11 0.017 0.077 21.11 5.6.10 0.003 0.043 21.03 5.6.9 0.003 0.087 20.97 5.6.8 0.003 0.090 20.46 5.6.7 0.013 0.077 20.41 5.6.6 0.007 0.073 20.55 5.6.5 0.003 0.047 20.54 5.6.4 0.007 0.087 20.55 5.6.3 0.007 0.070 20.56 5.6.2 0.023 0.060 20.38 5.6.1 0.003 0.080 20.37 5.6.0 0.010 0.060 20.37 5.5.38 0.003 0.047 20.37 5.5.37 0.013 0.073 20.54 5.5.36 0.013 0.070 20.37 5.5.35 0.010 0.067 20.43 5.5.34 0.010 0.060 20.95 5.5.33 0.010 0.080 20.90 5.5.32 0.000 0.083 20.90 5.5.31 0.013 0.077 20.85 5.5.30 0.017 0.077 20.98 5.5.29 0.007 0.090 20.79 5.5.28 0.007 0.050 20.90 5.5.27 0.013 0.080 20.93 5.5.26 0.007 0.043 20.96 5.5.25 0.010 0.073 20.68 5.5.24 0.007 0.050 20.21 5.5.23 0.000 0.047 20.30 5.5.22 0.007 0.067 20.25 5.5.21 0.003 0.053 20.34 5.5.20 0.010 0.070 20.06 5.5.19 0.007 0.080 20.29 5.5.18 0.010 0.077 20.29 5.5.16 0.003 0.083 20.21 5.5.15 0.007 0.063 20.30 5.5.14 0.013 0.073 20.30 5.5.13 0.017 0.067 20.05 5.5.12 0.010 0.047 20.34 5.5.11 0.003 0.067 20.24 5.5.10 0.003 0.070 20.17 5.5.9 0.017 0.040 20.11 5.5.8 0.007 0.077 20.16 5.5.7 0.013 0.050 20.16 5.5.6 0.000 0.073 20.21 5.5.5 0.010 0.043 20.03 5.5.4 0.017 0.073 20.12 5.5.3 0.010 0.043 20.00 5.5.2 0.017 0.067 20.06 5.5.1 0.010 0.060 20.20 5.5.0 0.003 0.050 20.19
preferences:dark mode live preview
35.61 ms | 400 KiB | 5 Q