diff --git a/README.md b/README.md index 00dd2c1..1e7bdaa 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 @@ -68,7 +62,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..d6e3a58 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)); @@ -165,7 +166,7 @@ public function setGetfield($string) } } - $this->getfield = '?' . http_build_query($params); + $this->getfield = '?' . http_build_query($params, '', '&'); return $this; } @@ -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', 'delete'))) { - throw new Exception('Request method must be either POST or GET'); + throw new Exception('Request method must be either POST, GET or PUT or DELETE'); } $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,17 +283,22 @@ public function performRequest($return = true, $curlOptions = array()) $getfield = $this->getGetfield(); $postfields = $this->getPostfields(); - $options = array( + if (in_array(strtolower($this->requestMethod), array('put', 'delete'))) + { + $curlOptions[CURLOPT_CUSTOMREQUEST] = $this->requestMethod; + } + + $options = $curlOptions + array( CURLOPT_HTTPHEADER => $header, CURLOPT_HEADER => false, CURLOPT_URL => $this->url, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10, - ) + $curlOptions; + ); if (!is_null($postfields)) { - $options[CURLOPT_POSTFIELDS] = http_build_query($postfields); + $options[CURLOPT_POSTFIELDS] = http_build_query($postfields, '', '&'); } else { 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": { diff --git a/test/TwitterAPIExchangeTest.php b/test/TwitterAPIExchangeTest.php index 2363d10..24121d6 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,35 @@ public function testIssue70() $data = $this->exchange->request($url, $method, $params); $this->assertContains('created_at', $data); } -} \ No newline at end of file + + /** + * Thanks to Sharath at eywamedia for bringint this to my attention + */ + public function testPut() + { + $url = 'https://ads-api.twitter.com/1/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); + } + + 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); + } +}