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

Commit 443d22c

Browse files
authored
Merge pull request #233 from J7mbo/develop
Put, delete support, overriding of cURL options
2 parents 5a1ac48 + 3242b77 commit 443d22c

File tree

4 files changed

+60
-29
lines changed

4 files changed

+60
-29
lines changed

README.md

+9-15
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,21 @@ You really can't get much simpler than that. The above bullet points are an exam
2929
Installation
3030
------------
3131

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

34-
**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 vendors folder:
34+
```php
35+
require_once('TwitterAPIExchange.php');
36+
```
3537

36-
{
37-
"require": {
38-
"j7mbo/twitter-api-php": "dev-master"
39-
}
40-
}
38+
**Via Composer:**
4139

42-
Of course, you'll then need to run `php composer.phar update`.
40+
```bash
41+
composer require j7mbo/twitter-api-php
42+
```
4343

4444
How To Use
4545
----------
4646

47-
#### Include the class file ####
48-
49-
```php
50-
require_once('TwitterAPIExchange.php');
51-
```
52-
5347
#### Set access tokens ####
5448

5549
```php
@@ -68,7 +62,7 @@ $url = 'https://api.twitter.com/1.1/blocks/create.json';
6862
$requestMethod = 'POST';
6963
```
7064

71-
#### Choose POST fields ####
65+
#### Choose POST fields (or PUT fields if you're using PUT) ####
7266

7367
```php
7468
$postfields = array(

TwitterAPIExchange.php

+17-11
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function setPostfields(array $array)
111111
{
112112
if (!is_null($this->getGetfield()))
113113
{
114-
throw new Exception('You can only choose get OR post fields.');
114+
throw new Exception('You can only choose get OR post fields (post fields include put).');
115115
}
116116

117117
if (isset($array['status']) && substr($array['status'], 0, 1) === '@')
@@ -130,7 +130,8 @@ public function setPostfields(array $array)
130130
$this->postfields = $array;
131131

132132
// rebuild oAuth
133-
if (isset($this->oauth['oauth_signature'])) {
133+
if (isset($this->oauth['oauth_signature']))
134+
{
134135
$this->buildOauth($this->url, $this->requestMethod);
135136
}
136137

@@ -150,7 +151,7 @@ public function setGetfield($string)
150151
{
151152
if (!is_null($this->getPostfields()))
152153
{
153-
throw new Exception('You can only choose get OR post fields.');
154+
throw new Exception('You can only choose get OR post / post fields.');
154155
}
155156

156157
$getfields = preg_replace('/^\?/', '', explode('&', $string));
@@ -165,7 +166,7 @@ public function setGetfield($string)
165166
}
166167
}
167168

168-
$this->getfield = '?' . http_build_query($params);
169+
$this->getfield = '?' . http_build_query($params, '', '&');
169170

170171
return $this;
171172
}
@@ -203,9 +204,9 @@ public function getPostfields()
203204
*/
204205
public function buildOauth($url, $requestMethod)
205206
{
206-
if (!in_array(strtolower($requestMethod), array('post', 'get')))
207+
if (!in_array(strtolower($requestMethod), array('post', 'get', 'put', 'delete')))
207208
{
208-
throw new Exception('Request method must be either POST or GET');
209+
throw new Exception('Request method must be either POST, GET or PUT or DELETE');
209210
}
210211

211212
$consumer_key = $this->consumer_key;
@@ -253,9 +254,9 @@ public function buildOauth($url, $requestMethod)
253254
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
254255
$oauth['oauth_signature'] = $oauth_signature;
255256

256-
$this->url = $url;
257+
$this->url = $url;
257258
$this->requestMethod = $requestMethod;
258-
$this->oauth = $oauth;
259+
$this->oauth = $oauth;
259260

260261
return $this;
261262
}
@@ -282,17 +283,22 @@ public function performRequest($return = true, $curlOptions = array())
282283
$getfield = $this->getGetfield();
283284
$postfields = $this->getPostfields();
284285

285-
$options = array(
286+
if (in_array(strtolower($this->requestMethod), array('put', 'delete')))
287+
{
288+
$curlOptions[CURLOPT_CUSTOMREQUEST] = $this->requestMethod;
289+
}
290+
291+
$options = $curlOptions + array(
286292
CURLOPT_HTTPHEADER => $header,
287293
CURLOPT_HEADER => false,
288294
CURLOPT_URL => $this->url,
289295
CURLOPT_RETURNTRANSFER => true,
290296
CURLOPT_TIMEOUT => 10,
291-
) + $curlOptions;
297+
);
292298

293299
if (!is_null($postfields))
294300
{
295-
$options[CURLOPT_POSTFIELDS] = http_build_query($postfields);
301+
$options[CURLOPT_POSTFIELDS] = http_build_query($postfields, '', '&');
296302
}
297303
else
298304
{

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
}
2323
],
2424
"autoload": {
25-
"files": ["TwitterAPIExchange.php"]
25+
"classmap": ["TwitterAPIExchange.php"]
2626
},
2727
"extra": {
2828
"branch-alias": {

test/TwitterAPIExchangeTest.php

+33-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function testStatusesHomeTimeline()
105105
{
106106
$url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
107107
$method = 'GET';
108-
$params = '?user_id=3232926711&max_id=595155660494471168';
108+
$params = '?user_id=3232926711&max_id=756123701888839681';
109109

110110
$data = $this->exchange->request($url, $method, $params);
111111
$expected = "Test Tweet";
@@ -304,4 +304,35 @@ public function testIssue70()
304304
$data = $this->exchange->request($url, $method, $params);
305305
$this->assertContains('created_at', $data);
306306
}
307-
}
307+
308+
/**
309+
* Thanks to Sharath at eywamedia for bringint this to my attention
310+
*/
311+
public function testPut()
312+
{
313+
$url = 'https://ads-api.twitter.com/1/accounts/hkk5/campaigns/8zwv';
314+
$method = 'PUT';
315+
$params = array (
316+
'name' => 'Important',
317+
'paused' => true
318+
);
319+
320+
$data = $this->exchange->request($url, $method, $params);
321+
322+
/** If we get this back, then it looks like we can support PUT! :-) **/
323+
$this->assertContains('UNAUTHORIZED_CLIENT_APPLICATION', $data);
324+
}
325+
326+
public function testDelete()
327+
{
328+
$params = array();
329+
330+
// foobaa is sandbox Ads account id
331+
$url = 'https://ads-api-sandbox.twitter.com/1/accounts/foobaa';
332+
$method = 'DELETE';
333+
334+
$data = $this->exchange->request($url, $method, $params);
335+
336+
$this->assertContains('UNAUTHORIZED_CLIENT_APPLICATION', $data);
337+
}
338+
}

0 commit comments

Comments
 (0)