Search code examples
twitter-oauth

Missing or invalid url parameter while posting image with twitter API


I am trying to post image on twitter. Image is already in my server. Here is my code

$tweet_img = '/home/voucherscode/public_html/editsocial/'.$tweet_img;
$returnT = $connection->post('statuses/update_with_media', array(
     'media[]' => file_get_contents($tweet_img),
     'status' => "$tweet_msg" 
 ));

But I am response as

stdClass Object(
[errors] => Array
    (
        [0] => stdClass Object
            (
                [code] => 195
                [message] => Missing or invalid url parameter.
            )
    ))

Please help.


Solution

  • I got the issue. I was using old twitteroauth and on that post function has not multipart parameter.

    I replaced my twitteroauth with https://github.com/tomi-heiskanen/twitteroauth/blob/77795ff40e4ec914bab4604e7063fa70a27077d4/twitteroauth/twitteroauth.php and it works with below code.

    $tweet_img = '/home/voucherscode/public_html/editsocial/'.$tweet_img;
    
    $handle = fopen($tweet_img,'rb');
    $image = fread($handle,filesize($tweet_img));
    fclose($handle);
    
    $parameters = array('media[]' => "{$image};type=image/jpeg;filename={$tweet_img}",'status' => 'Picture time');
    
    
    
    $returnT = $connection->post('statuses/update_with_media', $parameters, true);