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
from
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+65 −3
Diff settings

Always

Just for now

Copy path View file
@@ -1,6 +1,6 @@
language: php
php:
- "5.3"
- "5.5"

before_install:
- composer self-update
Copy path View file
@@ -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!
Copy path View file
@@ -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, '', '&');
Copy path View file
@@ -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)
]);
@@ -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);
}
ProTip! Use n and p to navigate between commits in a pull request.