Reputation: 177
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
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:
NOTES:
Good Luck! Lakota Lustig
Upvotes: 1
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