Evert De Spiegeleer
Evert De Spiegeleer

Reputation: 177

Upload image PHP to twitter (api)

i'm new to PHP, so please keep it simple enough ^^..

I try to upload an image to twitter with the api.

I can already post an image to twitter (test.jpg), when the file is on my server, but the user has to be able to direct upload it to twitter...

How can i do this ?

I have this code in 'tweet.html'

<form action="photo_tweet.php" method="post">
    <p>tweet: <input type="text" name="tweet" /></p>
    <input type="file" name="image" id="image"/>
    <input type="submit" name="submit" value="Submit" />
</form>

And i have this code in 'photo_tweet.php'

<?php

/**
 */

require '../tmhOAuth.php';
require '../tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
  'consumer_key'    => 'xxxxxxxxxxxxxx',
  'consumer_secret' => 'xxxxxxxxxxxxxx',
  'user_token'      => 'xxxxxxxxxxxxxx',
  'user_secret'     => 'xxxxxxxxxxxxxx',
));

// we're using a hardcoded image path here. You can easily replace this with
// an uploaded image - see images.php in the examples folder for how to do this
// 'image = "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",

// this is the jpeg file to upload. It should be in the same directory as this file.
    $tweetmessage = $_POST['tweet'];
    $image = 'test.jpg';

$code = $tmhOAuth->request(
  'POST',
  'https://upload.twitter.com/1/statuses/update_with_media.json',
  array(
    'media[]'  => "@{$image};type=image/jpeg;base64;filename={$image}",
    'status'   => $tweetmessage ,
  ),
  true, // use auth
  true  // multipart
);

if ($code == 200) {
  tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
} else {
  tmhUtilities::pr($tmhOAuth->response['response']);
}

Upvotes: 2

Views: 3453

Answers (2)

LakotaLustig
LakotaLustig

Reputation: 11

What I'm thinking Evert, is the easiest way for you to do this, would be the following. Have an upload image page. This page TEMPORARILY uploads the image to your server, since after it uploads to your server, it redirects with the file name of the image stored in a variable. Then it uploads dynamically to Twitter and after 5 minutes, deletes it from your site.

MAIN POINTS:

  • User upload TEMPORARILY to your site
  • Redirect to Twitter upload page on your site
  • Upload the $image you had before
  • Then, have it timed so it deletes from your site within 5 minutes.

NOTES:

  • If you want to have a GREAT Twitter client, keep all the images uploaded. Better security.
  • Also, I don't know if you know, but if your Twitter client posts a direct link to a .jpg, it will work the same as an image on pic.twitter.com
  • Lastly, I think you have the hang of it, but as a starting PHP developer, I'd suggest going for a bit smaller of a project;)

Good Luck! Lakota Lustig

Upvotes: 1

Patrick Evans
Patrick Evans

Reputation: 42746

if you mean to bypass the image being uploaded to your server first then you will need to use their javascript api if they have one that allows posting images through it.

otherwise it tells you in the comment how to do it.

// we're using a hardcoded image path here. You can easily replace this with
// an uploaded image - see images.php in the examples folder for how to do this
// 'image = "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",

$_FILES is the global variable that holds information about the uploaded image that the user submits through your tweet form.

Upvotes: 0

Related Questions