如何使用php在twitter中分享图片?我知道它使用的是twitter的图片上传api。但是我不知道如何实现这一点?请提前帮助查找solution.Thanks。
发布于 2011-09-23 04:09:06
这是Doc在twitter上的图片分享。
https://dev.twitter.com/docs/api/1/post/statuses/update_with_media
这是唯一一个支持twitter图片上传的PHP库。
https://github.com/themattharris/tmhOAuth
这是一个图像共享的工作示例。对你的集合进行几次编辑。
<?php
if ($_POST['message'] && $_FILES['image']['tmp_name'])
{
require 'tmhOAuth.php';
list($oauth_token, $oauth_token_secret) = explode('|', $GLOBALS['user']['password']);
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => OAUTH_CONSUMER_KEY,
'consumer_secret' => OAUTH_CONSUMER_SECRET,
'user_token' => $oauth_token,
'user_secret' => $oauth_token_secret,
));
$image = "{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}";
$code = $tmhOAuth->request('POST', 'https://upload.twitter.com/1/statuses/update_with_media.json',
array(
'media[]' => "@{$image}",
'status' => " " . $status //A space is needed because twitter b0rks if first char is an @
),
true, // use auth
true // multipart
);
if ($code == 200) {
$json = json_decode($tmhOAuth->response['response']);
if ($_SERVER['HTTPS'] == "on") {
$image_url = $json->entities->media[0]->media_url_https;
}
else {
$image_url = $json->entities->media[0]->media_url;
}
$text = $json->text;
$content = "<p>Upload success. Image posted to Twitter.</p>
<p><img src=\"" . IMAGE_PROXY_URL . "x50/" . $image_url . "\" alt='' /></p>
<p>". twitter_parse_tags($text) . "</p>";
} else {
$content = "Damn! Something went wrong. Sorry :-("
."<br /> code=" . $code
."<br /> status=" . $status
."<br /> image=" . $image
."<br /> response=<pre>"
. print_r($tmhOAuth->response['response'], TRUE)
. "</pre><br /> info=<pre>"
. print_r($tmhOAuth->response['info'], TRUE)
. "</pre><br /> code=<pre>"
. print_r($tmhOAuth->response['code'], TRUE) . "</pre>";
}
} ?>发布于 2011-09-23 02:47:35
https://dev.twitter.com/docs/api/1/post/statuses/update_with_media
上面的链接有我相信的所有必要的信息。
发布于 2012-07-26 08:56:55
还有另一个库(它是派生的)可用于图像上传,请参阅:TwitterOauth
$path = '/absolute/path/to/background.jpg';
$meta = getimagesize($path);
$raw['image'] = $path.';type='.$meta['mime'];
$param['tile'] = 'true';
$status = $connection->post('account/update_profile_background_image',$param,$raw);https://stackoverflow.com/questions/7519572
复制相似问题