Skip to content

Commit

Permalink
Merge pull request #110 from J7mbo/glenscott-master
Browse files Browse the repository at this point in the history
Fix for media upload / POST methods, Travis CI
  • Loading branch information
J7mbo committed May 4, 2015
2 parents ab76f1c + 90ccc1d commit e576556
Show file tree
Hide file tree
Showing 8 changed files with 434 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
vendor/
composer.lock
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: php
php:
- "5.3"

before_install:
- composer self-update

before_script:
- composer install

script:
- phpunit --configuration phpunit.xml
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ twitter-api-php
Simple PHP Wrapper for Twitter API v1.1 calls

[![Total Downloads](https://poser.pugx.org/j7mbo/twitter-api-php/downloads.png)](https://packagist.org/packages/j7mbo/twitter-api-php)

[![Build Status](https://travis-ci.org/J7mbo/twitter-api-php.svg?branch=master)](https://packagist.org/packages/j7mbo/twitter-api-php)

**[Changelog](https://github.com/J7mbo/twitter-api-php/wiki/Changelog)** ||
**[Examples](https://github.com/J7mbo/twitter-api-php/wiki/Twitter-API-PHP-Wiki)** ||
**[Wiki](https://github.com/J7mbo/twitter-api-php/wiki)** ||
**[Buy me a beer!](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=KHQYGY4MM3E7J)**
**[Wiki](https://github.com/J7mbo/twitter-api-php/wiki)**

[Instructions in StackOverflow post here](http://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1/15314662#15314662) with examples. This post shows you how to get your tokens and more.
If you found it useful, please upvote / leave a comment! :)
Expand All @@ -23,14 +22,14 @@ The aim of this class is simple. You need to:
- Choose either GET / POST (depending on the request)
- Choose the fields you want to send with the request (example: `array('screen_name' => 'usernameToBlock')`)

You really can't get much simpler than that. Here is an example of how to use the class for a POST request to block a user, and at the bottom is an example of a GET request.
You really can't get much simpler than that. The above bullet points are an example of how to use the class for a POST request to block a user, and at the bottom is an example of a GET request.

Installation
------------

**Normally:** If you *don't* use composer, don't worry - just include TwitterAPIExchange.php in your application.

**Via Composer:** If you *do* use composer, here's what you add to your composer.json file to have TwitterAPIExchange.php automatically imported into your vendor's folder:
**Via Composer:** If you realise it's 2015 now and you *do* use composer, here's what you add to your composer.json file to have TwitterAPIExchange.php automatically imported into your vendor's folder:

{
"require": {
Expand Down
136 changes: 115 additions & 21 deletions TwitterAPIExchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +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 @@ -50,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 All @@ -72,13 +112,20 @@ public function setPostfields(array $array)

$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
*/
Expand Down Expand Up @@ -121,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 @@ -133,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 @@ -152,19 +202,34 @@ 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];
}
}
}

$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;
Expand All @@ -173,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 @@ -183,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 @@ -199,7 +266,7 @@ public function performRequest($return = true)

if (!is_null($postfields))
{
$options[CURLOPT_POSTFIELDS] = $postfields;
$options[CURLOPT_POSTFIELDS] = http_build_query($postfields);
}
else
{
Expand All @@ -214,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 @@ -231,9 +298,9 @@ private function buildBaseString($baseURI, $method, $params)
$return = array();
ksort($params);

foreach($params as $key=>$value)
foreach($params as $key => $value)
{
$return[] = "$key=" . $value;
$return[] = rawurlencode($key) . '=' . rawurlencode($value);
}

return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
Expand All @@ -246,18 +313,45 @@ 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();

foreach($oauth as $key => $value)
{
$values[] = "$key=\"" . rawurlencode($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
*
* @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();
}
}
40 changes: 25 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
{
"name": "j7mbo/twitter-api-php",
"description": "Simple PHP Wrapper for Twitter API v1.1 calls",
"version": "0.1",
"type": "library",
"keywords": ["twitter", "PHP", "API"],
"homepage": "https://github.com/j7mbo/twitter-api-php",
"license": "GNU Public License",
"authors": [
{
"name": "James Mallison",
"homepage": "https://github.com/j7mbo/twitter-api-php"
"require": {
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "4.5.1"
},
"name": "j7mbo/twitter-api-php",
"description": "Simple PHP Wrapper for Twitter API v1.1 calls",
"version": "1.0.0",
"type": "library",
"keywords": [
"twitter",
"PHP",
"API"
],
"homepage": "https://github.com/j7mbo/twitter-api-php",
"license": "GNU Public License",
"authors": [
{
"name": "James Mallison",
"homepage": "https://github.com/j7mbo/twitter-api-php"
}
],
"autoload": {
"files": ["TwitterAPIExchange.php"]
}
],
"autoload": {
"files": ["TwitterAPIExchange.php"]
}
}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./vendor/autoload.php"
>
<testsuites>
<testsuite name="Twitter-Api-PHP Test Suite">
<directory>./test/</directory>
</testsuite>
</testsuites>
</phpunit>
Loading

0 comments on commit e576556

Please sign in to comment.