diff --git a/.travis.yml b/.travis.yml index abac424..8ec6faf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: php php: - - "5.3" + - "5.5" before_install: - composer self-update diff --git a/README.md b/README.md index 1e7bdaa..f0ae89f 100644 --- a/README.md +++ b/README.md @@ -96,4 +96,34 @@ echo $twitter->setGetfield($getfield) ->performRequest(); ``` +POST with JSON Request Example +------------------- + +Set request header when calling performRequest(); + +```php +$url = 'https://api.twitter.com/1.1/direct_messages/events/new.json'; +$postJson = [ + "event" => [ + "type" => "message_create", + "message_create" => [ + "target" => [ + "recipient_id" => "TWITTER_ID" + ], + "message_data" => [ + "text" => "Hello World!", + ] + ] + ] +]; +$requestMethod = 'POST'; + +$twitter = new TwitterAPIExchange($settings); +echo $twitter->buildOauth($url, $requestMethod) + ->performRequest(true,[ + CURLOPT_HTTPHEADER => array('Content-Type:application/json'), + CURLOPT_POSTFIELDS => json_encode($postJson) + ]); +``` + That is it! Really simple, works great with the 1.1 API. Thanks to @lackovic10 and @rivers on SO! diff --git a/TwitterAPIExchange.php b/TwitterAPIExchange.php index d6e3a58..6ad95f8 100755 --- a/TwitterAPIExchange.php +++ b/TwitterAPIExchange.php @@ -296,6 +296,14 @@ public function performRequest($return = true, $curlOptions = array()) CURLOPT_TIMEOUT => 10, ); + + if(isset($curlOptions[CURLOPT_HTTPHEADER])){ + $options[CURLOPT_HTTPHEADER] = array_merge( + $curlOptions[CURLOPT_HTTPHEADER], + $header + ); + } + if (!is_null($postfields)) { $options[CURLOPT_POSTFIELDS] = http_build_query($postfields, '', '&'); diff --git a/index.php b/index.php index ab0f790..eb3c49c 100755 --- a/index.php +++ b/index.php @@ -35,3 +35,27 @@ echo $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest(); + +// /** Perform a POST request with content type "application/json" and echo the response **/ +// /** Note: `DO NOT` Set the POST field. Use curl options in performRequest instead **/ +$url = 'https://api.twitter.com/1.1/direct_messages/events/new.json'; +$postJson = [ + "event" => [ + "type" => "message_create", + "message_create" => [ + "target" => [ + "recipient_id" => "" // Twitter ID + ], + "message_data" => [ + "text" => "Hello World!", + ] + ] + ] +]; +$requestMethod = 'POST'; +$twitter = new TwitterAPIExchange($settings); +echo $twitter->buildOauth($url, $requestMethod) + ->performRequest(true,[ + CURLOPT_HTTPHEADER => array('Content-Type:application/json'), + CURLOPT_POSTFIELDS => json_encode($postJson) + ]); diff --git a/test/TwitterAPIExchangeTest.php b/test/TwitterAPIExchangeTest.php index 24121d6..329ff89 100644 --- a/test/TwitterAPIExchangeTest.php +++ b/test/TwitterAPIExchangeTest.php @@ -105,10 +105,10 @@ public function testStatusesHomeTimeline() { $url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'; $method = 'GET'; - $params = '?user_id=3232926711&max_id=756123701888839681'; + $params = '?user_id=3232926711&max_id=787407826176110600'; $data = $this->exchange->request($url, $method, $params); - $expected = "Test Tweet"; + $expected = "TEST TWEET"; $this->assertContains($expected, $data); }