Post status + image with TwitterAPIExchange on Twitter

后端 未结 3 953
醉话见心
醉话见心 2021-01-26 13:52

I\'ve got the following working code, the code simply posts a status and an image om my twitter page.

       require_once(\'TwitterAPIExchange.php\');

        $         


        
3条回答
  •  深忆病人
    2021-01-26 14:15

    The accepted answer uses a depreciated API endpoint https://dev.twitter.com/rest/reference/post/statuses/update_with_media

    Here is a working solution:

    // send image to Twitter first
    $url = 'https://upload.twitter.com/1.1/media/upload.json';
    $requestMethod = 'POST';
    
    $image = 'full/path/to/image.jpg';
    
    $postfields = array(
      'media_data' => base64_encode(file_get_contents($image))
    );
    
    $response = $twitter->buildOauth($url, $requestMethod)
      ->setPostfields($postfields)
      ->performRequest();
    
    // get the media_id from the API return
    $media_id = json_decode($response)->media_id;
    
    // then send the Tweet along with the media ID
    $url = 'https://api.twitter.com/1.1/statuses/update.json';
    $requestMethod = 'POST';
    
    $postfields = array(
      'status' => 'My amazing tweet'
      'media_ids' => $media_id,
    );
    
    $response = $twitter->buildOauth($url, $requestMethod)
      ->setPostfields($postfields)
      ->performRequest();
    

提交回复
热议问题