Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/post with json body #250

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: php
php:
- "5.3"
- "5.5"

before_install:
- composer self-update
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
8 changes: 8 additions & 0 deletions TwitterAPIExchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '', '&');
Expand Down
24 changes: 24 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]);
4 changes: 2 additions & 2 deletions test/TwitterAPIExchangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down