Having no ids of uploaded images in PHP


#1

Hi.I use the PHP SDK by j7mbo to update status with images.
First I send some images to the https://upload.twitter.com/1.1/media/upload.json and get seems to be normal response. I take each id to the array $ids and put them separated by comma to the array $parameters:

$parameters['media_ids'] = implode(',', $ids);

So, all array is:

array('status' => 'TEXT OF STATUS', 'media_ids' => '2423423423434234,34343434343434');

After sending the request to the https://api.twitter.com/1.1/statuses/update.json I get request like I never added images ids (have no extended_entities).

The fulll code looks like:

$twitter = new TwitterAPIExchange($settings);
$images = array('/path/to/pic1', '/path/to/pic2'); 
if(!empty($images)){
        $url = 'https://upload.twitter.com/1.1/media/upload.json';$method='POST';

       
        foreach($images as $img){
            if(!is_file($img))
                continue;
            
            $file2upload = array('media' => "@$img");


            
            $action = $twitter->buildOauth($url, $method)->setPostfields($file2upload)->performRequest();
            $return['images'][] = $answer = json_decode($action);
            if(!is_null($answer) and !isset($answer->error)){
                $ids[] = $answer->media_id_string;
            }
        }

    }
$url = 'https://api.twitter.com/1.1/statuses/update.json';

    $parameters = array(
        'status' => $text,
    );

    switch(count($ids)){
        case 0:
            break;
        case 1:
            $parameters['trim_user'] = 1;$parameters['media_ids'] = $ids[0];break;
        default:
            $parameters['trim_user'] = 1;$parameters['media_ids'] = implode(',', $ids);break;
    }

    $tweetObj = new TwitterAPIExchange($settings);

    $response = $tweetObj->buildOauth($url, $method)->setPostfields($parameters)->performRequest();

    $return['tweet'] = json_decode($response);
    $return['parameters'] = $tweetObj->getPostfields();

In the $return I have responses of each image uploading then resonse of the status updating. After that becomes the postfields that I send with new tweet.

Here the full return:

array(3) {
  ["images"]=>
  array(2) {
    [0]=>
    object(stdClass)#18 (4) {
      ["media_id"]=>
      int(527777203019579392)
      ["media_id_string"]=>
      string(18) "527777203019579392"
      ["size"]=>
      int(59725)
      ["image"]=>
      object(stdClass)#19 (3) {
        ["w"]=>
        int(769)
        ["h"]=>
        int(520)
        ["image_type"]=>
        string(9) "image/png"
      }
    }
    [1]=>
    object(stdClass)#20 (4) {
      ["media_id"]=>
      int(527777211227860992)
      ["media_id_string"]=>
      string(18) "527777211227860992"
      ["size"]=>
      int(111948)
      ["image"]=>
      object(stdClass)#21 (3) {
        ["w"]=>
        int(500)
        ["h"]=>
        int(375)
        ["image_type"]=>
        string(10) "image/jpeg"
      }
    }
  }
  ["tweet"]=>
  object(stdClass)#23 (22) {
    ["created_at"]=>
    string(30) "Thu Oct 30 11:01:08 +0000 2014"
    ["id"]=>
    int(527777215615107072)
    ["id_str"]=>
    string(18) "527777215615107072"
    ["text"]=>
    string(21) "SomeNewTest1414666864"
    ["source"]=>
    string(56) "Style i"
    ["truncated"]=>
    bool(false)
    ["in_reply_to_status_id"]=>
    NULL
    ["in_reply_to_status_id_str"]=>
    NULL
    ["in_reply_to_user_id"]=>
    NULL
    ["in_reply_to_user_id_str"]=>
    NULL
    ["in_reply_to_screen_name"]=>
    NULL
    ["user"]=>
    object(stdClass)#24 (40) {
      ["id"]=>
      int(2837937831)
      ["id_str"]=>
      string(10) "2837937831"
      ["name"]=>
      string(7) "Style i"
      ["screen_name"]=>
      string(9) "Styleiapp"
      ["location"]=>
      string(0) ""
      ["profile_location"]=>
      NULL
      ["description"]=>
      string(0) ""
      ["url"]=>
      NULL
      ["entities"]=>
      object(stdClass)#25 (1) {
        ["description"]=>
        object(stdClass)#26 (1) {
          ["urls"]=>
          array(0) {
          }
        }
      }
      ["protected"]=>
      bool(false)
      ["followers_count"]=>
      int(0)
      ["friends_count"]=>
      int(0)
      ["listed_count"]=>
      int(0)
      ["created_at"]=>
      string(30) "Mon Oct 20 09:30:59 +0000 2014"
      ["favourites_count"]=>
      int(0)
      ["utc_offset"]=>
      NULL
      ["time_zone"]=>
      NULL
      ["geo_enabled"]=>
      bool(false)
      ["verified"]=>
      bool(false)
      ["statuses_count"]=>
      int(81)
      ["lang"]=>
      string(2) "ru"
      ["contributors_enabled"]=>
      bool(false)
      ["is_translator"]=>
      bool(false)
      ["is_translation_enabled"]=>
      bool(false)
      ["profile_background_color"]=>
      string(6) "C0DEED"
      ["profile_background_image_url"]=>
      string(48) "http://abs.twimg.com/images/themes/theme1/bg.png"
      ["profile_background_image_url_https"]=>
      string(49) "https://abs.twimg.com/images/themes/theme1/bg.png"
      ["profile_background_tile"]=>
      bool(false)
      ["profile_image_url"]=>
      string(79) "http://abs.twimg.com/sticky/default_profile_images/default_profile_1_normal.png"
      ["profile_image_url_https"]=>
      string(80) "https://abs.twimg.com/sticky/default_profile_images/default_profile_1_normal.png"
      ["profile_link_color"]=>
      string(6) "0084B4"
      ["profile_sidebar_border_color"]=>
      string(6) "C0DEED"
      ["profile_sidebar_fill_color"]=>
      string(6) "DDEEF6"
      ["profile_text_color"]=>
      string(6) "333333"
      ["profile_use_background_image"]=>
      bool(true)
      ["default_profile"]=>
      bool(true)
      ["default_profile_image"]=>
      bool(true)
      ["following"]=>
      bool(false)
      ["follow_request_sent"]=>
      bool(false)
      ["notifications"]=>
      bool(false)
    }
    ["geo"]=>
    NULL
    ["coordinates"]=>
    NULL
    ["place"]=>
    NULL
    ["contributors"]=>
    NULL
    ["retweet_count"]=>
    int(0)
    ["favorite_count"]=>
    int(0)
    ["entities"]=>
    object(stdClass)#27 (4) {
      ["hashtags"]=>
      array(0) {
      }
      ["symbols"]=>
      array(0) {
      }
      ["user_mentions"]=>
      array(0) {
      }
      ["urls"]=>
      array(0) {
      }
    }
    ["favorited"]=>
    bool(false)
    ["retweeted"]=>
    bool(false)
    ["lang"]=>
    string(2) "en"
  }
  ["parameters"]=>
  array(3) {
    ["status"]=>
    string(21) "SomeNewTest1414666864"
    ["trim_user"]=>
    string(1) "1"
    ["media_ids"]=>
    string(37) "527777203019579392,527777211227860992"
  }
}

Sorry for my English and Thank You


#3

@bonjorik i am also facing the same issue, may i know did you got solution for your issue?

Thanks


#4

awaiting for your response.


#5

Have no solution for now. I’m waiting 4 some Twitter masta)


#6

I think there is a bug in the API, finally i went to single photo upload approach https://dev.twitter.com/rest/public/uploading-media

            $image = "http://imagepath/image.jpg";

            $settings = array(
                'oauth_access_token' => $twitterUserToken,
                'oauth_access_token_secret' => $twitterUserSecret,
                'consumer_key' => $consumer_key,
                'consumer_secret' => $consumer_secret
            );

            $twitter = new TwitterAPIExchange($settings);
            $urlimage = 'https://api.twitter.com/1.1/statuses/update_with_media.json';$method='POST';
            $file2upload = array('media' => file_get_contents($image), 'status' => $twitterMessage);
            $action = $twitter->buildOauth($urlimage, $method)->setPostfields($file2upload)->performRequest();
            return json_decode($action);

this is working fine, hope this helps someone.


#7

yeah, I’ve tried to use this api and it works. thank you!

So, if there’re some news when will multiple uploading work?


#8

no idea, we have to wait Twitter masters to respond


#9

Have you raised this issue with the creator of the PHP library? Is this the only library that is affected?

Can you provide a reproducible example demonstrating that the multiple upload API is not working? I see many tweets that have multiple images attached.


#10

I had the same issue with the “j7mbo” Twitter-Api, the “media_ids” didn’t work.
Uploading Media Multiple Photos works fine with https://github.com/jublonet/codebird-php.


#11

@kithaitaa can you give us the sample working code here


#12

There is a sample code on the Codebird Github page Usage examples -> Uploading media to Twitter


#13

@sforsendil i am using code which you given above

    $image = "http://www.fullwallpaper.in/wp-content/uploads/2014/05/Emotional-wallpapers-with-wording-in-it-2.jpg";
    $twitteroauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['TWITTER_token'], $_SESSION['TWITTER_token_secret']);
    $urlimage = 'https://api.twitter.com/1.1/statuses/update_with_media.json';
$method='POST';
    $file2upload = array('media' => file_get_contents($image), 'status' => $message);
    $action = $twitteroauth->buildOauth($urlimage, $method)->setPostfields($file2upload)->performRequest();
    print_r(json_decode($action));

…It shows error:
Fatal error: Call to undefined method TwitterOAuth::buildOauth() in D:\xamp\htdocs\socialposting\profile\twitterpostingDONE\indexTW.php on line 37

can you please tell why this error come.?


#14

how can you send some images to the https://upload.twitter.com/1.1/media/upload.json .Please give some reference code if you have.Thanks


#15

https://github.com/J7mbo/twitter-api-php/blob/master/TwitterAPIExchange.php use this lib with my code


#16

sir i used your code and lib but it shows :
file_get_contents() expects parameter 1 to be a valid path, string given in D:\xamp\htdocs\socialposting\profile\twitter-api\twitter-api-php-master\index.php on line 19

Code :

echo $twitterMessage="dgaaaaaaaaaaaaf";
echo  $image = file_get_contents("http://www.letsintern.com/blog/wp-content/uploads/2015/02/google-office-design.jpeg");

            $twitter = new TwitterAPIExchange($settings);
            $urlimage = 'https://api.twitter.com/1.1/statuses/update_with_media.json';$method='POST';
            $file2upload = array('media' =>$image, 'status' => $twitterMessage);
            $action = $twitter->buildOauth($urlimage, $method)->setPostfields($file2upload)->performRequest();
			print_r( $action);
            return json_decode($action);