Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to post an image to Twitter with PHP?
    primarykey
    data
    text
    <p>I'm trying to post a message with an image to Twitter through PHP and cURL. It's working for the message but I don't know how to add the image to it.</p> <p>From the <a href="https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media" rel="nofollow">documentation</a>: </p> <blockquote> <p>Unlike POST statuses/update, this method expects raw multipart data. Your POST request's Content-Type should be set to multipart/form-data with the media[] parameter</p> </blockquote> <p>This is the PHP:</p> <pre><code>&lt;?php class Tweet { public $url = 'https://api.twitter.com/1.1/statuses/update_with_media.json'; function the_nonce(){ $nonce = base64_encode(uniqid()); $nonce = preg_replace('~[\W]~','',$nonce); return $nonce; } function get_DST($status){ $url = $this-&gt;url; $consumer_key = "[removed]"; $nonce = $this-&gt;the_nonce(); $sig_method = 'HMAC-SHA1'; $timestamp = time(); $version = "1.0"; $token = "[removed]"; $access_secret = "[removed]"; $consumer_secret = "[removed]"; $param_string = 'oauth_consumer_key='.$consumer_key. '&amp;oauth_nonce='.$nonce. '&amp;oauth_signature_method='.$sig_method. '&amp;oauth_timestamp='.$timestamp. '&amp;oauth_token='.$token. '&amp;oauth_version='.$version. '&amp;status='.rawurlencode($status); $sig_base_string = 'POST&amp;'.rawurlencode($url).'&amp;'.rawurlencode($param_string); $sig_key = rawurlencode($consumer_secret).'&amp;'.rawurlencode($access_secret); $tweet_sig = base64_encode(hash_hmac('sha1', $sig_base_string, $sig_key, true)); $DST = 'OAuth oauth_consumer_key="'.rawurlencode($consumer_key).'",'. 'oauth_nonce="'.rawurlencode($nonce).'",'. 'oauth_signature="'.rawurlencode($tweet_sig).'",'. 'oauth_signature_method="HMAC-SHA1",'. 'oauth_timestamp="'.rawurlencode($timestamp).'",'. 'oauth_token="'.rawurlencode($token).'",'. 'oauth_version="1.0"'; return $DST; } function set($status){ $url = $this-&gt;url; $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $this-&gt;get_DST($status))); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, 'status='.rawurlencode($status)); curl_setopt($ch, CURLOPT_URL, $url); $result = json_decode(curl_exec($ch)); $resultString = print_r($result,true);; curl_close($ch); } $status-&gt;set("hello"); ?&gt; </code></pre> <p>How to add the <em>media[]</em> part to it? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload