title: PHP Twitter api New direct messages with event type (change september 2018)
body: <p>Use the twitter API to send direct messages, with the following method: "POST direct_messages / new" with php, tmhOAuth library.</p>
  
  <p>Since a few days ago they changed (as indicated in <a href="https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/guides/direct-message-migration.html" rel="nofollow noreferrer">migration documentation</a>
  ) by the method: "POST direct_messages / events / new", where the structure of the post parameters changes, which are json.</p>
  
  <p>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" (<a href="https://developer.twitter.com/en/docs/basics/response-codes.html" rel="nofollow noreferrer">twitter help</a>)</p>
  
  <p>Adding a url callback to the app, keeps giving the same error. Did someone have the same problem when they migrated? any suggestions?</p>
  
  <p>I detail the changes implemented, with some code:</p>
  
  <pre><code>    $options = array(
      "event"                 =&gt; array(
                                      "type"              =&gt; 'message_create',
                                      "message_create"    =&gt; array(
                                      "target"        =&gt; array('recipient_id'     =&gt; $id_usuario),    
                                                                                  "message_data"  =&gt; array('text'             =&gt; $texto),
                                                      ),
                      )
              );
      $json_options = json_encode($options);
      $headers_extra = array('Content-Type' =&gt; 'application/json');
  
      $code = $this-&gt;_tmhOAuth-&gt;request('POST', $this-&gt;_tmhOAuth-&gt;url('1.1/direct_messages/events/new'), $json_options, $headers_extra);
  </code></pre>
  
  <p>1) In "options" the format is changed, by the requested json,</p>
  
  <p>2) "json_encode" function is used before sending in request method.</p>
  
  <p>3) Headers "Content-Type" are added to be taken by the tmhOAuth library.</p>
  
  <p>Thanks for any comments or help</p>