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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?