Post status + image with TwitterAPIExchange on Twitter

后端 未结 3 951
醉话见心
醉话见心 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();
    
    0 讨论(0)
  • 2021-01-26 14:20

    It was much easier than I thought, I just used the $_FILES to store the image in a variable so I could use it in the $postfields array.

    $url_media = "https://api.twitter.com/1.1/statuses/update_with_media.json";
    $requestMethod = "POST";
    
    $tweetmsg = $_POST['post_description'];
    $twimg = $_FILES['pictureFile']['tmp_name'];
    
        $postfields = array(
            'status' => $tweetmsg,
            'media[]' => '@' . $twimg
        );
        try {
            $twitter = new TwitterAPIExchange($settings);
            $twitter->buildOauth($url_media, $requestMethod)
                    ->setPostfields($postfields)
                    ->performRequest();
    
            echo "You just tweeted with an image";
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    
    0 讨论(0)
  • 2021-01-26 14:28

    You have to implement the slight logic here-- Now on click of submit do following 1.store the image in the some folder of your project (Refer the below code to store the image in the folder)

    upload_file.php //upload file code here

    2.store text and upload the image name in the database. 3.Now you have the images in the image folder... and you also have the image name and the corresponding message in your database.

    4. $tweetmsg = $_POST['post_text']; $twimage = "Images/twitter-icon.png"; //before this

       retrive the last inserted row and fetch message and image name 
    
       $tweetmsg = $row['msg'];
       $image = $row['image_name'];
        $twimage = "Images/".$image;
    

    I hope this will be work for you.. Thanks

    0 讨论(0)
提交回复
热议问题