dsgs8208 2016-05-04 02:40
浏览 54
已采纳

PHP:在Twitter上发布媒体?

I've been trying to post an image with a simple message onto twitter using PHP and twitteroauth.php.

However, every time I run my code, I only get the $tweetMessage published on the twitter feed without any image.

I searched and searched and read their own documentation but don't even get me started on their own documentation! its like someone who's had a sleepwalk was writing their documentation. Just a bunch of jargon..

And most of the information on STO is either outdated or pointing to a library!

I do not want to use any library as I will have to try to learn someone else's code as well and Surely twitter would allow publishing photo's using their own API without the use of any third party Library?!

Any way, This is my full code:

// 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));
}

Could someone please advise on this issue?

Thanks in advance.

EDIT:

I've changed my code to the following and I get this error:

{"errors":[{"code":195,"message":"Missing or invalid url parameter."}]}

But I'm sure the image is on the specified URL/directory!

This is the code:

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);

Any idea why this error is being shown?

展开全部

  • 写回答

2条回答 默认 最新

  • duanbichou4942 2016-05-04 04:44
    关注

    I solved it like this:

    $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);
    

    Horrible twitter API documentation needs improving!! it needs to be written by humans as opposed to a bunch of sleepwalking zombies!!!

    This is a very frustrating situation that they put us in when we try to use their API...

    They either need stop their API support and remove it all from the public or simply improve their documentation and write it for the public and not just for their own use using jargon words.

    Any way, the above code works just fine using the latest twitteroauth

    I hope this helps others in my situation. I feel like i wasted 5 hours for something that should be clear and mentioned in plain English on their site!!!

    Rant and Answer over & good luck.. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部