如何使用php在twitter上分享图像?我知道它使用twitter的图片上传api。但我不知道如何实现这个?请帮助找到解决方案。谢谢。
答案 0 :(得分:3)
这是关于推特图片分享的文件。
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>";
}
} ?>
答案 1 :(得分:0)
https://dev.twitter.com/docs/api/1/post/statuses/update_with_media
以上链接包含我认为的所有必要信息。
答案 2 :(得分:0)
还有一个可用于图片上传的其他库(分叉),请参阅: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);
答案 3 :(得分:-1)
最初,当我提供媒体文件的路径时,我得到了同样的错误。实际上你需要提供字节读取。因此,请按如下方式设置媒体参数,它将起作用:
$filename = "image.jpg";
$handle = fopen($filename, "rb");
$image = fread($handle, filesize($filename));
fclose($handle);
$params = array('media[]' => "{$image};type=image/jpeg;filename={$filename}" ,
'status' => 'my status');
$response =$twitteroauth->post('statuses/update_with_media', $params,true,true);
您可以访问此链接以获取完整参考http://net.tutsplus.com/tutorials/php/how-to-authenticate-users-with-twitter-oauth/