dblanco
dblanco

Reputation: 285

PHP Twitter api New direct messages with event type (change september 2018)

Use the twitter API to send direct messages, with the following method: "POST direct_messages / new" with php, tmhOAuth library.

Since a few days ago they changed (as indicated in migration documentation ) by the method: "POST direct_messages / events / new", where the structure of the post parameters changes, which are json.

Making the changes that indicate, when testing get response code 415: "Callback URL not approved for this client application. Approved callback URLs can be adjusted in your application settings" (twitter help)

Adding a url callback to the app, keeps giving the same error. Did someone have the same problem when they migrated? any suggestions?

I detail the changes implemented, with some code:

    $options = array(
    "event"                 => array(
                                    "type"              => 'message_create',
                                    "message_create"    => array(
                                    "target"        => array('recipient_id'     => $id_usuario),    
                                                                                "message_data"  => array('text'             => $texto),
                                                    ),
                    )
            );
    $json_options = json_encode($options);
    $headers_extra = array('Content-Type' => 'application/json');

    $code = $this->_tmhOAuth->request('POST', $this->_tmhOAuth->url('1.1/direct_messages/events/new'), $json_options, $headers_extra);

1) In "options" the format is changed, by the requested json,

2) "json_encode" function is used before sending in request method.

3) Headers "Content-Type" are added to be taken by the tmhOAuth library.

Thanks for any comments or help

Upvotes: 0

Views: 626

Answers (1)

Pierre de France
Pierre de France

Reputation: 106

tmhOauth is too old and hasn't been updated for a while. I lost some time trying to adjust it and finally switched to https://twitteroauth.com/

I was able to send DM in 1 min and 4 lines of codes just following their example in section JSON data.

Upvotes: 0

Related Questions