Post status + image with TwitterAPIExchange on Twitter

后端 未结 3 952
醉话见心
醉话见心 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: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();
        }
    

提交回复
热议问题