@@ -32,6 +32,15 @@ class Twitter
3232 */
3333 const OAUTH_BASE_URI = 'https://api.twitter.com/oauth ' ;
3434
35+ /**
36+ * Paths that use JSON payloads (vs form-encoded)
37+ */
38+ const PATHS_JSON_PAYLOAD = [
39+ 'direct_messages/events/new ' ,
40+ 'direct_messages/welcome_messages/new ' ,
41+ 'direct_messages/welcome_messages/rules/new ' ,
42+ ];
43+
3544 /**
3645 * 246 is the current limit for a status message, 140 characters are displayed
3746 * initially, with the remainder linked from the web UI or client. The limit is
@@ -402,7 +411,12 @@ public function post(string $path, $data = null) : Response
402411 {
403412 $ client = $ this ->getHttpClient ();
404413 $ this ->init ($ path , $ client );
405- $ response = $ this ->performPost (Http \Request::METHOD_POST , $ data , $ client );
414+ $ response = $ this ->performPost (
415+ Http \Request::METHOD_POST ,
416+ $ data ,
417+ $ client ,
418+ in_array ($ path , self ::PATHS_JSON_PAYLOAD , true )
419+ );
406420 return new Response ($ response );
407421 }
408422
@@ -1272,6 +1286,7 @@ public function statusesUpdate(string $status, $inReplyToStatusId = null, $extra
12721286 if ($ inReplyToStatusId ) {
12731287 $ params ['in_reply_to_status_id ' ] = $ inReplyToStatusId ;
12741288 }
1289+
12751290 return $ this ->post ($ path , $ params );
12761291 }
12771292
@@ -1499,21 +1514,17 @@ protected function validateScreenName(string $name) : string
14991514 * is JSON-encoded before being passed to the request body.
15001515 *
15011516 * @param null|string|array|\stdClass $data Raw data to send
1517+ * @param bool $asJson Whether or not the data should be submitted as JSON
1518+ * (vs form urlencoded, which is the default)
15021519 */
1503- protected function performPost (string $ method , $ data , Http \Client $ client ) : Http \Response
1520+ protected function performPost (string $ method , $ data , Http \Client $ client, bool $ asJson ) : Http \Response
15041521 {
1505- if (is_array ($ data ) || is_object ($ data )) {
1506- $ data = json_encode ($ data , $ this ->jsonFlags );
1507- }
1522+ $ client ->setMethod ($ method );
15081523
1509- if (! empty ($ data )) {
1510- $ client ->setRawBody ($ data );
1511- $ client ->getRequest ()
1512- ->getHeaders ()
1513- ->addHeaderLine ('Content-Type ' , 'application/json ' );
1514- }
1524+ $ asJson
1525+ ? $ this ->prepareJsonPayloadForClient ($ client , $ data )
1526+ : $ this ->prepareFormPayloadForClient ($ client , $ data );
15151527
1516- $ client ->setMethod ($ method );
15171528 return $ client ->send ();
15181529 }
15191530
@@ -1629,4 +1640,36 @@ protected function createUserListParameter($ids, array $params, string $context)
16291640 $ params ['screen_name ' ] = implode (', ' , $ ids );
16301641 return $ params ;
16311642 }
1643+
1644+ /**
1645+ * Prepare a JSON payload for the HTTP client.
1646+ */
1647+ private function prepareJsonPayloadForClient (Http \Client $ client , $ data )
1648+ {
1649+ if (is_array ($ data ) || is_object ($ data )) {
1650+ $ data = json_encode ($ data , $ this ->jsonFlags );
1651+ }
1652+
1653+ if (empty ($ data ) || ! is_string ($ data )) {
1654+ return ;
1655+ }
1656+
1657+ $ client ->getRequest ()
1658+ ->getHeaders ()
1659+ ->addHeaderLine ('Content-Type ' , 'application/json ' );
1660+
1661+ $ client ->setRawBody ($ data );
1662+ }
1663+
1664+ /**
1665+ * Prepare a form-url-encoded payload for the HTTP client.
1666+ */
1667+ private function prepareFormPayloadForClient (Http \Client $ client , $ data )
1668+ {
1669+ if (! is_array ($ data )) {
1670+ return ;
1671+ }
1672+
1673+ $ client ->setParameterPost ($ data );
1674+ }
16321675}
0 commit comments