[英]PHP: media posting on twitter?
我一直在尝试使用PHP和twitteroauth.php
将带有简单消息的图像发布到twitter上。
但是,每次运行我的代码时,我只会在$tweetMessage
发布没有任何图像的$tweetMessage
。
我搜索并搜索并阅读了他们自己的文档,但甚至没有让我开始他们自己的文档! 就像有一个梦游的人正在写他们的文档。 只是一堆行话..
关于STO的大部分信息要么过时,要么指向图书馆!
我不想使用任何库,因为我将不得不尝试学习其他人的代码,当然twitter会允许使用他们自己的API发布照片而不使用任何第三方库?!
无论如何,这是我的完整代码:
// Include twitteroauth
require_once('inc/twitteroauth.php');
// Set keys
$consumerKey = 'xxxxxxxxxxxxxxxxxxx';
$consumerSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$accessToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$accessTokenSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// Create object
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
// Set status message
$tweetMessage = 'This is a tweet to my Twitter account via PHP.';
$image_path="https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
$handle = fopen($image_path,'rb');
$image = fread($handle,filesize($image_path));
fclose($handle);
// Check for 140 characters
if(strlen($tweetMessage) <= 140)
{
// Post the status message
$tweet->post('statuses/update', array('media[]' => "{$image};type=image/jpeg;filename={$image_path}", 'status' => $tweetMessage));
}
有人可以就这个问题提出建议吗?
提前致谢。
编辑:
我已将代码更改为以下内容,并且出现此错误:
{"errors":[{"code":195,"message":"Missing or invalid url parameter."}]}
但我确定图像在指定的URL /目录上!
这是代码:
require_once 'inc/twitteroauth.php';
define("CONSUMER_KEY", "xxxxxxxxxxxxxxxxx");
define("CONSUMER_SECRET", "xxxxxxxxxxxxxxxxxxxxxxxxxx");
define("OAUTH_TOKEN", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
define("OAUTH_SECRET", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$content = $connection->get('images/sign-in-with-twitter-l.png');
$image = 'images/sign-in-with-twitter-l.png';
$status_message = 'Attaching an image to a tweet';
$status = $connection->post('statuses/update_with_media', array('status' => $status_message, 'media[]' => file_get_contents($image)));
echo json_encode($status);
知道为什么会出现这个错误吗?
将媒体上传到Twitter有点复杂。 从本质上讲,这是一个三阶段的过程。
media_id
。 status
和media_id
到Twitter。 这在https://dev.twitter.com/rest/reference/post/media/upload中有详细描述
一般来说,使用像CodeBird这样的库更容易,因为他们已经完成了查找所有边缘情况的艰苦工作。
但是,假设你不想那样做......
/1.1/media/upload.json
{ "media_id": 553656900508606464, "media_id_string": "553656900508606464", "size": 998865, "image": { "w": 2234, "h": 1873, "image_type": "image/jpeg" } }
*发布状态时使用该media_id_string
。 例如
tweet->post('statuses/update', array('media_ids' => $media_id_string, 'status' => $tweetMessage));
希望这能让你足够了解正在发生的事情。
我这样解决了:
$tweet_img = 'Path/to/image';
$handle = fopen($tweet_img,'rb');
$image = fread($handle,filesize($tweet_img));
fclose($handle);
$parameters = array('media[]' => "{$image};type=image/jpeg;filename={$tweet_img}",'status' => 'Picture time');
$returnT = $connection->post('statuses/update_with_media', $parameters, true);
糟糕的Twitter API文档需要改进!! 它需要由人类编写,而不是一群梦游的僵尸!
这是一个非常令人沮丧的情况,他们在我们尝试使用他们的API时将它们放入...
他们要么需要停止他们的API支持并将其全部从公众中删除,要么只是改进他们的文档并为公众编写,而不仅仅是为了自己使用术语。
无论如何,上面的代码使用最新的twitteroauth工作得很好
我希望在我的情况下这有助于其他人。 我觉得我浪费了5个小时的时间,应该清楚,并在他们的网站上用简单的英语提到!
咆哮和回答和好运.. :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.