Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
Added check for null getfield
Browse files Browse the repository at this point in the history
Added shortcut request method
Added phpdocs
  • Loading branch information
JamesMallison committed May 4, 2015
1 parent ae7e644 commit 44c2a1c
Showing 1 changed file with 94 additions and 18 deletions.
112 changes: 94 additions & 18 deletions TwitterAPIExchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,59 @@
*/
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)
Expand All @@ -51,12 +88,14 @@ public function __construct(array $settings)
$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)
Expand Down Expand Up @@ -85,6 +124,8 @@ public function setPostfields(array $array)
* 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
*/
Expand Down Expand Up @@ -127,9 +168,12 @@ public function getPostfields()
/**
* 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)
Expand All @@ -139,12 +183,12 @@ public function buildOauth($url, $requestMethod)
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;
$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 = array(
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
Expand All @@ -158,10 +202,16 @@ public function buildOauth($url, $requestMethod)
if (!is_null($getfield))
{
$getfields = str_replace('?', '', explode('&', $getfield));

foreach ($getfields as $g)
{
$split = explode('=', $g);
$oauth[$split[0]] = $split[1];

/** In case a null is passed through **/
if (isset($split[1]))
{
$oauth[$split[0]] = $split[1];
}
}
}

Expand All @@ -188,7 +238,9 @@ public function buildOauth($url, $requestMethod)
/**
* Perform the actual data retrieval from the API
*
* @param boolean $return If true, returns data.
* @param boolean $return If true, returns data. This is left in for backward compatibility reasons
*
* @throws \Exception
*
* @return string json If $return param is true, returns json data.
*/
Expand All @@ -198,13 +250,13 @@ public function performRequest($return = true)
{
throw new Exception('performRequest parameter must be true or false');
}
$header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');

$header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');

$getfield = $this->getGetfield();
$postfields = $this->getPostfields();

$options = array(
$options = array(
CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $this->url,
Expand All @@ -229,15 +281,15 @@ public function performRequest($return = true)
$json = curl_exec($feed);
curl_close($feed);

if ($return) { return $json; }
return $json;
}

/**
* Private method to generate the base string used by cURL
*
* @param string $baseURI
* @param string $method
* @param array $params
* @param array $params
*
* @return string Built base string
*/
Expand All @@ -246,7 +298,7 @@ private function buildBaseString($baseURI, $method, $params)
$return = array();
ksort($params);

foreach($params as $key=>$value)
foreach($params as $key => $value)
{
$return[] = rawurlencode($key) . '=' . rawurlencode($value);
}
Expand All @@ -261,7 +313,7 @@ private function buildBaseString($baseURI, $method, $params)
*
* @return string $return Header used by cURL for request
*/
private function buildAuthorizationHeader($oauth)
private function buildAuthorizationHeader(array $oauth)
{
$return = 'Authorization: OAuth ';
$values = array();
Expand All @@ -278,4 +330,28 @@ private function buildAuthorizationHeader($oauth)
return $return;
}

/**
* Helper method to perform our request
*
* @param string $url
* @param string $method
* @param string $data
*
* @throws \Exception
*
* @return string The json response from the server
*/
public function request($url, $method = 'get', $data = null)
{
if (strtolower($method) === 'get')
{
$this->setGetfield($data);
}
else
{
$this->setPostfields($data);
}

return $this->buildOauth($url, $method)->performRequest();
}
}

0 comments on commit 44c2a1c

Please sign in to comment.