Skip to content

Commit

Permalink
Merge pull request #233 from J7mbo/develop
Browse files Browse the repository at this point in the history
Put, delete support, overriding of cURL options
  • Loading branch information
J7mbo committed May 8, 2017
2 parents 5a1ac48 + 3242b77 commit 443d22c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 deletions.
24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
28 changes: 17 additions & 11 deletions TwitterAPIExchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) === '@')
Expand All @@ -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);
}

Expand All @@ -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));
Expand All @@ -165,7 +166,7 @@ public function setGetfield($string)
}
}

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

return $this;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
],
"autoload": {
"files": ["TwitterAPIExchange.php"]
"classmap": ["TwitterAPIExchange.php"]
},
"extra": {
"branch-alias": {
Expand Down
35 changes: 33 additions & 2 deletions test/TwitterAPIExchangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -304,4 +304,35 @@ 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/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);
}
}

0 comments on commit 443d22c

Please sign in to comment.