dsgs8208 2016-05-04 10: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 12: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条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装