我是PHP新手,所以请保持足够简单^^..
我尝试使用 api 将图像上传到 Twitter。
当文件在我的服务器上时,我已经可以将图像发布到推特(test.jpg),但用户必须能够直接将其上传到推特......
我怎样才能做到这一点 ?
我在“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>
我在'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']);
}