From 1dd06d7bc1dac1487ebdadc3e3161d9bd301eb57 Mon Sep 17 00:00:00 2001 From: James Mallison Date: Mon, 25 Jul 2016 18:19:39 +0100 Subject: [PATCH 1/7] Added PUT support (#199) * Removed GET / POST restriction, PUT allowed also. * Fixed failing test * Added PUT support and test * Updated README for PUT --- README.md | 2 +- TwitterAPIExchange.php | 20 +++++++++++++------- test/TwitterAPIExchangeTest.php | 20 +++++++++++++++++++- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 00dd2c1..763795b 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ $url = 'https://api.twitter.com/1.1/blocks/create.json'; $requestMethod = 'POST'; ``` -#### Choose POST fields #### +#### Choose POST fields (or PUT fields if you're using PUT) #### ```php $postfields = array( diff --git a/TwitterAPIExchange.php b/TwitterAPIExchange.php index 7987522..d9aae9d 100755 --- a/TwitterAPIExchange.php +++ b/TwitterAPIExchange.php @@ -111,7 +111,7 @@ public function setPostfields(array $array) { if (!is_null($this->getGetfield())) { - throw new Exception('You can only choose get OR post fields.'); + throw new Exception('You can only choose get OR post fields (post fields include put).'); } if (isset($array['status']) && substr($array['status'], 0, 1) === '@') @@ -130,7 +130,8 @@ public function setPostfields(array $array) $this->postfields = $array; // rebuild oAuth - if (isset($this->oauth['oauth_signature'])) { + if (isset($this->oauth['oauth_signature'])) + { $this->buildOauth($this->url, $this->requestMethod); } @@ -150,7 +151,7 @@ public function setGetfield($string) { if (!is_null($this->getPostfields())) { - throw new Exception('You can only choose get OR post fields.'); + throw new Exception('You can only choose get OR post / post fields.'); } $getfields = preg_replace('/^\?/', '', explode('&', $string)); @@ -203,9 +204,9 @@ public function getPostfields() */ public function buildOauth($url, $requestMethod) { - if (!in_array(strtolower($requestMethod), array('post', 'get'))) + if (!in_array(strtolower($requestMethod), array('post', 'get', 'put'))) { - throw new Exception('Request method must be either POST or GET'); + throw new Exception('Request method must be either POST, GET or PUT'); } $consumer_key = $this->consumer_key; @@ -253,9 +254,9 @@ public function buildOauth($url, $requestMethod) $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true)); $oauth['oauth_signature'] = $oauth_signature; - $this->url = $url; + $this->url = $url; $this->requestMethod = $requestMethod; - $this->oauth = $oauth; + $this->oauth = $oauth; return $this; } @@ -282,6 +283,11 @@ public function performRequest($return = true, $curlOptions = array()) $getfield = $this->getGetfield(); $postfields = $this->getPostfields(); + if (strtolower($this->requestMethod) === 'put') + { + $curlOptions[CURLOPT_CUSTOMREQUEST] = $this->requestMethod; + } + $options = array( CURLOPT_HTTPHEADER => $header, CURLOPT_HEADER => false, diff --git a/test/TwitterAPIExchangeTest.php b/test/TwitterAPIExchangeTest.php index 2363d10..8b8cdca 100644 --- a/test/TwitterAPIExchangeTest.php +++ b/test/TwitterAPIExchangeTest.php @@ -105,7 +105,7 @@ public function testStatusesHomeTimeline() { $url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'; $method = 'GET'; - $params = '?user_id=3232926711&max_id=595155660494471168'; + $params = '?user_id=3232926711&max_id=756123701888839681'; $data = $this->exchange->request($url, $method, $params); $expected = "Test Tweet"; @@ -304,4 +304,22 @@ public function testIssue70() $data = $this->exchange->request($url, $method, $params); $this->assertContains('created_at', $data); } + + /** + * Thanks to Sharath at eywamedia for bringint this to my attention + */ + public function testPut() + { + $url = 'https://ads-api.twitter.com/0/accounts/hkk5/campaigns/8zwv'; + $method = 'PUT'; + $params = array ( + 'name' => 'Important', + 'paused' => true + ); + + $data = $this->exchange->request($url, $method, $params); + + /** If we get this back, then it looks like we can support PUT! :-) **/ + $this->assertContains('UNAUTHORIZED_CLIENT_APPLICATION', $data); + } } \ No newline at end of file From 3889ec754b0ce8ed7008cb330f89cc964a4a4ddd Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 26 Jul 2016 00:07:41 +0300 Subject: [PATCH 2/7] Explicitly use '&' in http_build_query() (#200) --- TwitterAPIExchange.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TwitterAPIExchange.php b/TwitterAPIExchange.php index d9aae9d..299b547 100755 --- a/TwitterAPIExchange.php +++ b/TwitterAPIExchange.php @@ -166,7 +166,7 @@ public function setGetfield($string) } } - $this->getfield = '?' . http_build_query($params); + $this->getfield = '?' . http_build_query($params, '', '&'); return $this; } @@ -298,7 +298,7 @@ public function performRequest($return = true, $curlOptions = array()) if (!is_null($postfields)) { - $options[CURLOPT_POSTFIELDS] = http_build_query($postfields); + $options[CURLOPT_POSTFIELDS] = http_build_query($postfields, '', '&'); } else { From 8ed941d49b2b6bca1adae7693bf55ef3c007b092 Mon Sep 17 00:00:00 2001 From: James Mallison Date: Fri, 29 Jul 2016 20:43:54 +0100 Subject: [PATCH 3/7] Fix classmap composer autoloading (#202) For #201. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0d99c99..2002abe 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ } ], "autoload": { - "files": ["TwitterAPIExchange.php"] + "classmap": ["TwitterAPIExchange.php"] }, "extra": { "branch-alias": { From d2b3b8eb7ef047723394300aeca781cf43fa0ced Mon Sep 17 00:00:00 2001 From: Emmet O'Grady Date: Sun, 31 Jul 2016 16:57:46 +0200 Subject: [PATCH 4/7] Allow overriding of curl options --- TwitterAPIExchange.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TwitterAPIExchange.php b/TwitterAPIExchange.php index 299b547..631b9d1 100755 --- a/TwitterAPIExchange.php +++ b/TwitterAPIExchange.php @@ -288,13 +288,13 @@ public function performRequest($return = true, $curlOptions = array()) $curlOptions[CURLOPT_CUSTOMREQUEST] = $this->requestMethod; } - $options = array( + $options = $curlOptions + array( CURLOPT_HTTPHEADER => $header, CURLOPT_HEADER => false, CURLOPT_URL => $this->url, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10, - ) + $curlOptions; + ); if (!is_null($postfields)) { From 1d109b80f00cc3abc91ad001deb092abe6ae2ab6 Mon Sep 17 00:00:00 2001 From: naoyukik Date: Tue, 2 Aug 2016 23:06:45 +0900 Subject: [PATCH 5/7] Added DELETE Support Added DELETE and test. --- TwitterAPIExchange.php | 6 +++--- test/TwitterAPIExchangeTest.php | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/TwitterAPIExchange.php b/TwitterAPIExchange.php index 299b547..93c28b5 100755 --- a/TwitterAPIExchange.php +++ b/TwitterAPIExchange.php @@ -204,9 +204,9 @@ public function getPostfields() */ public function buildOauth($url, $requestMethod) { - if (!in_array(strtolower($requestMethod), array('post', 'get', 'put'))) + if (!in_array(strtolower($requestMethod), array('post', 'get', 'put', 'delete'))) { - throw new Exception('Request method must be either POST, GET or PUT'); + throw new Exception('Request method must be either POST, GET or PUT or DELETE'); } $consumer_key = $this->consumer_key; @@ -283,7 +283,7 @@ public function performRequest($return = true, $curlOptions = array()) $getfield = $this->getGetfield(); $postfields = $this->getPostfields(); - if (strtolower($this->requestMethod) === 'put') + if (in_array(strtolower($this->requestMethod), array('put', 'delete'))) { $curlOptions[CURLOPT_CUSTOMREQUEST] = $this->requestMethod; } diff --git a/test/TwitterAPIExchangeTest.php b/test/TwitterAPIExchangeTest.php index 8b8cdca..8c87d41 100644 --- a/test/TwitterAPIExchangeTest.php +++ b/test/TwitterAPIExchangeTest.php @@ -322,4 +322,17 @@ public function testPut() /** If we get this back, then it looks like we can support PUT! :-) **/ $this->assertContains('UNAUTHORIZED_CLIENT_APPLICATION', $data); } -} \ No newline at end of file + + public function testDelete() + { + $params = array(); + + // foobaa is sandbox Ads account id + $url = 'https://ads-api-sandbox.twitter.com/1/accounts/foobaa'; + $method = 'DELETE'; + + $data = $this->exchange->request($url, $method, $params); + + $this->assertContains('UNAUTHORIZED_CLIENT_APPLICATION', $data); + } +} From f40b1e86f0f9dc7b95542ad53d671bd66108c9b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Votruba?= Date: Sat, 15 Oct 2016 14:42:49 +0200 Subject: [PATCH 6/7] Readme: simplify composer install --- README.md | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 00dd2c1..10be73b 100644 --- a/README.md +++ b/README.md @@ -29,27 +29,21 @@ You really can't get much simpler than that. The above bullet points are an exam Installation ------------ -**Normally:** If you *don't* use composer, don't worry - just include TwitterAPIExchange.php in your application. +**Normally:** If you *don't* use composer, don't worry - just include TwitterAPIExchange.php in your application. -**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: +```php +require_once('TwitterAPIExchange.php'); +``` - { - "require": { - "j7mbo/twitter-api-php": "dev-master" - } - } +**Via Composer:** -Of course, you'll then need to run `php composer.phar update`. +```bash +composer require j7mbo/twitter-api-php +``` How To Use ---------- -#### Include the class file #### - -```php -require_once('TwitterAPIExchange.php'); -``` - #### Set access tokens #### ```php From 3242b775b3f5e3ae2c769c963def89bc139bb7ae Mon Sep 17 00:00:00 2001 From: J7mbo Date: Mon, 8 May 2017 13:49:16 +0200 Subject: [PATCH 7/7] Updated put test to valid campaign url - PUT request test works now --- test/TwitterAPIExchangeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/TwitterAPIExchangeTest.php b/test/TwitterAPIExchangeTest.php index 8c87d41..24121d6 100644 --- a/test/TwitterAPIExchangeTest.php +++ b/test/TwitterAPIExchangeTest.php @@ -310,7 +310,7 @@ public function testIssue70() */ public function testPut() { - $url = 'https://ads-api.twitter.com/0/accounts/hkk5/campaigns/8zwv'; + $url = 'https://ads-api.twitter.com/1/accounts/hkk5/campaigns/8zwv'; $method = 'PUT'; $params = array ( 'name' => 'Important',