0

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

       require_once('TwitterAPIExchange.php');

        $settings = array(
            'oauth_access_token' => "xxx",
            'oauth_access_token_secret' => "xxx",
            'consumer_key' => "xxx",
            'consumer_secret' => "xxx"
        );

        $url = "https://api.twitter.com/1.1/statuses/update_with_media.json";
        $requestMethod = "POST";

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

        $postfields = array(
            'status' => $tweetmsg,
            'media[]' => "@{$twimage}"
        );
        try {
            $twitter = new TwitterAPIExchange($settings);
            $twitter->buildOauth($url, $requestMethod)
                    ->setPostfields($postfields)
                    ->performRequest();

            echo "Success, you just tweeted!";
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }

The image is right now placed in a folder (Images) which is in my project. I want the user to able to pick an image from his own computer, write a decription and then tweet it. I've got the following simple HTML form for the posting:

<form method="post" action="index.php">
<textarea id="post_text" name="post_text" type="text"></textarea>
<input type="file" name="post_file" id="post_file" multiple accept="image/*" value="Choose a file"/>
<button type="submit" id="btn_submit">Submit</button>
</from>

So do you guys have any tips or guides which could help me solve my problem? Am i thinking in the right way or should I maybe tackle the problem in another way? Thanks!

user2190027
  • 7
  • 1
  • 5

3 Answers3

5

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();
Paul Phillips
  • 1,480
  • 1
  • 15
  • 23
0

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

Sagar Ch
  • 13
  • 6
  • Also your URL,https://api.twitter.com/1.1/statuses/update_with_media.json, is wrong. It should be: https://upload.twitter.com/1.1/statuses/update_with_media.json – Emre Aydin Jul 03 '14 at 12:59
0

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();
    }
user2190027
  • 7
  • 1
  • 5