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

Commit e576556

Browse files
committed
Merge pull request #110 from J7mbo/glenscott-master
Fix for media upload / POST methods, Travis CI
2 parents ab76f1c + 90ccc1d commit e576556

8 files changed

+434
-41
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
vendor/
3+
composer.lock

.travis.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
php:
3+
- "5.3"
4+
5+
before_install:
6+
- composer self-update
7+
8+
before_script:
9+
- composer install
10+
11+
script:
12+
- phpunit --configuration phpunit.xml

README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ twitter-api-php
33
Simple PHP Wrapper for Twitter API v1.1 calls
44

55
[![Total Downloads](https://poser.pugx.org/j7mbo/twitter-api-php/downloads.png)](https://packagist.org/packages/j7mbo/twitter-api-php)
6-
6+
[![Build Status](https://travis-ci.org/J7mbo/twitter-api-php.svg?branch=master)](https://packagist.org/packages/j7mbo/twitter-api-php)
77

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

1312
[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.
1413
If you found it useful, please upvote / leave a comment! :)
@@ -23,14 +22,14 @@ The aim of this class is simple. You need to:
2322
- Choose either GET / POST (depending on the request)
2423
- Choose the fields you want to send with the request (example: `array('screen_name' => 'usernameToBlock')`)
2524

26-
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.
25+
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.
2726

2827
Installation
2928
------------
3029

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

33-
**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:
32+
**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:
3433

3534
{
3635
"require": {

TwitterAPIExchange.php

+115-21
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,59 @@
1313
*/
1414
class TwitterAPIExchange
1515
{
16+
/**
17+
* @var string
18+
*/
1619
private $oauth_access_token;
20+
21+
/**
22+
* @var string
23+
*/
1724
private $oauth_access_token_secret;
25+
26+
/**
27+
* @var string
28+
*/
1829
private $consumer_key;
30+
31+
/**
32+
* @var string
33+
*/
1934
private $consumer_secret;
35+
36+
/**
37+
* @var array
38+
*/
2039
private $postfields;
40+
41+
/**
42+
* @var string
43+
*/
2144
private $getfield;
45+
46+
/**
47+
* @var mixed
48+
*/
2249
protected $oauth;
50+
51+
/**
52+
* @var string
53+
*/
2354
public $url;
2455

56+
/**
57+
* @var string
58+
*/
59+
public $requestMethod;
60+
2561
/**
2662
* Create the API access object. Requires an array of settings::
2763
* oauth access token, oauth access token secret, consumer key, consumer secret
2864
* These are all available by creating your own application on dev.twitter.com
2965
* Requires the cURL library
30-
*
66+
*
67+
* @throws \Exception When cURL isn't installed or incorrect settings parameters are provided
68+
*
3169
* @param array $settings
3270
*/
3371
public function __construct(array $settings)
@@ -50,12 +88,14 @@ public function __construct(array $settings)
5088
$this->consumer_key = $settings['consumer_key'];
5189
$this->consumer_secret = $settings['consumer_secret'];
5290
}
53-
91+
5492
/**
5593
* Set postfields array, example: array('screen_name' => 'J7mbo')
56-
*
94+
*
5795
* @param array $array Array of parameters to send to API
58-
*
96+
*
97+
* @throws \Exception When you are trying to set both get and post fields
98+
*
5999
* @return TwitterAPIExchange Instance of self for method chaining
60100
*/
61101
public function setPostfields(array $array)
@@ -72,13 +112,20 @@ public function setPostfields(array $array)
72112

73113
$this->postfields = $array;
74114

115+
// rebuild oAuth
116+
if (isset($this->oauth['oauth_signature'])) {
117+
$this->buildOauth($this->url, $this->requestMethod);
118+
}
119+
75120
return $this;
76121
}
77122

78123
/**
79124
* Set getfield string, example: '?screen_name=J7mbo'
80125
*
81126
* @param string $string Get key and value pairs as string
127+
*
128+
* @throws \Exception
82129
*
83130
* @return \TwitterAPIExchange Instance of self for method chaining
84131
*/
@@ -121,9 +168,12 @@ public function getPostfields()
121168
/**
122169
* Build the Oauth object using params set in construct and additionals
123170
* passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
124-
*
171+
*
125172
* @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json
126173
* @param string $requestMethod Either POST or GET
174+
*
175+
* @throws \Exception
176+
*
127177
* @return \TwitterAPIExchange Instance of self for method chaining
128178
*/
129179
public function buildOauth($url, $requestMethod)
@@ -133,12 +183,12 @@ public function buildOauth($url, $requestMethod)
133183
throw new Exception('Request method must be either POST or GET');
134184
}
135185

136-
$consumer_key = $this->consumer_key;
137-
$consumer_secret = $this->consumer_secret;
138-
$oauth_access_token = $this->oauth_access_token;
186+
$consumer_key = $this->consumer_key;
187+
$consumer_secret = $this->consumer_secret;
188+
$oauth_access_token = $this->oauth_access_token;
139189
$oauth_access_token_secret = $this->oauth_access_token_secret;
140190

141-
$oauth = array(
191+
$oauth = array(
142192
'oauth_consumer_key' => $consumer_key,
143193
'oauth_nonce' => time(),
144194
'oauth_signature_method' => 'HMAC-SHA1',
@@ -152,19 +202,34 @@ public function buildOauth($url, $requestMethod)
152202
if (!is_null($getfield))
153203
{
154204
$getfields = str_replace('?', '', explode('&', $getfield));
205+
155206
foreach ($getfields as $g)
156207
{
157208
$split = explode('=', $g);
158-
$oauth[$split[0]] = $split[1];
209+
210+
/** In case a null is passed through **/
211+
if (isset($split[1]))
212+
{
213+
$oauth[$split[0]] = $split[1];
214+
}
159215
}
160216
}
161217

218+
$postfields = $this->getPostfields();
219+
220+
if (!is_null($postfields)) {
221+
foreach ($postfields as $key => $value) {
222+
$oauth[$key] = $value;
223+
}
224+
}
225+
162226
$base_info = $this->buildBaseString($url, $requestMethod, $oauth);
163227
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
164228
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
165229
$oauth['oauth_signature'] = $oauth_signature;
166230

167231
$this->url = $url;
232+
$this->requestMethod = $requestMethod;
168233
$this->oauth = $oauth;
169234

170235
return $this;
@@ -173,7 +238,9 @@ public function buildOauth($url, $requestMethod)
173238
/**
174239
* Perform the actual data retrieval from the API
175240
*
176-
* @param boolean $return If true, returns data.
241+
* @param boolean $return If true, returns data. This is left in for backward compatibility reasons
242+
*
243+
* @throws \Exception
177244
*
178245
* @return string json If $return param is true, returns json data.
179246
*/
@@ -183,13 +250,13 @@ public function performRequest($return = true)
183250
{
184251
throw new Exception('performRequest parameter must be true or false');
185252
}
186-
187-
$header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
253+
254+
$header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
188255

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

192-
$options = array(
259+
$options = array(
193260
CURLOPT_HTTPHEADER => $header,
194261
CURLOPT_HEADER => false,
195262
CURLOPT_URL => $this->url,
@@ -199,7 +266,7 @@ public function performRequest($return = true)
199266

200267
if (!is_null($postfields))
201268
{
202-
$options[CURLOPT_POSTFIELDS] = $postfields;
269+
$options[CURLOPT_POSTFIELDS] = http_build_query($postfields);
203270
}
204271
else
205272
{
@@ -214,15 +281,15 @@ public function performRequest($return = true)
214281
$json = curl_exec($feed);
215282
curl_close($feed);
216283

217-
if ($return) { return $json; }
284+
return $json;
218285
}
219286

220287
/**
221288
* Private method to generate the base string used by cURL
222289
*
223290
* @param string $baseURI
224291
* @param string $method
225-
* @param array $params
292+
* @param array $params
226293
*
227294
* @return string Built base string
228295
*/
@@ -231,9 +298,9 @@ private function buildBaseString($baseURI, $method, $params)
231298
$return = array();
232299
ksort($params);
233300

234-
foreach($params as $key=>$value)
301+
foreach($params as $key => $value)
235302
{
236-
$return[] = "$key=" . $value;
303+
$return[] = rawurlencode($key) . '=' . rawurlencode($value);
237304
}
238305

239306
return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
@@ -246,18 +313,45 @@ private function buildBaseString($baseURI, $method, $params)
246313
*
247314
* @return string $return Header used by cURL for request
248315
*/
249-
private function buildAuthorizationHeader($oauth)
316+
private function buildAuthorizationHeader(array $oauth)
250317
{
251318
$return = 'Authorization: OAuth ';
252319
$values = array();
253320

254321
foreach($oauth as $key => $value)
255322
{
256-
$values[] = "$key=\"" . rawurlencode($value) . "\"";
323+
if (in_array($key, array('oauth_consumer_key', 'oauth_nonce', 'oauth_signature',
324+
'oauth_signature_method', 'oauth_timestamp', 'oauth_token', 'oauth_version'))) {
325+
$values[] = "$key=\"" . rawurlencode($value) . "\"";
326+
}
257327
}
258328

259329
$return .= implode(', ', $values);
260330
return $return;
261331
}
262332

333+
/**
334+
* Helper method to perform our request
335+
*
336+
* @param string $url
337+
* @param string $method
338+
* @param string $data
339+
*
340+
* @throws \Exception
341+
*
342+
* @return string The json response from the server
343+
*/
344+
public function request($url, $method = 'get', $data = null)
345+
{
346+
if (strtolower($method) === 'get')
347+
{
348+
$this->setGetfield($data);
349+
}
350+
else
351+
{
352+
$this->setPostfields($data);
353+
}
354+
355+
return $this->buildOauth($url, $method)->performRequest();
356+
}
263357
}

composer.json

+25-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
{
2-
"name": "j7mbo/twitter-api-php",
3-
"description": "Simple PHP Wrapper for Twitter API v1.1 calls",
4-
"version": "0.1",
5-
"type": "library",
6-
"keywords": ["twitter", "PHP", "API"],
7-
"homepage": "https://github.com/j7mbo/twitter-api-php",
8-
"license": "GNU Public License",
9-
"authors": [
10-
{
11-
"name": "James Mallison",
12-
"homepage": "https://github.com/j7mbo/twitter-api-php"
2+
"require": {
3+
"ext-curl": "*"
4+
},
5+
"require-dev": {
6+
"phpunit/phpunit": "4.5.1"
7+
},
8+
"name": "j7mbo/twitter-api-php",
9+
"description": "Simple PHP Wrapper for Twitter API v1.1 calls",
10+
"version": "1.0.0",
11+
"type": "library",
12+
"keywords": [
13+
"twitter",
14+
"PHP",
15+
"API"
16+
],
17+
"homepage": "https://github.com/j7mbo/twitter-api-php",
18+
"license": "GNU Public License",
19+
"authors": [
20+
{
21+
"name": "James Mallison",
22+
"homepage": "https://github.com/j7mbo/twitter-api-php"
23+
}
24+
],
25+
"autoload": {
26+
"files": ["TwitterAPIExchange.php"]
1327
}
14-
],
15-
"autoload": {
16-
"files": ["TwitterAPIExchange.php"]
17-
}
1828
}

phpunit.xml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false"
10+
syntaxCheck="false"
11+
bootstrap="./vendor/autoload.php"
12+
>
13+
<testsuites>
14+
<testsuite name="Twitter-Api-PHP Test Suite">
15+
<directory>./test/</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

0 commit comments

Comments
 (0)