如何使用PHP在Twitter上发布图像?

时间:2014-08-20 15:50:37

标签: php twitter

很容易发布没有图像的文本,我发现许多有用的链接发布文本,但当我搜索如何使用PHP代码在Twitter上一起发布文本图像时,没有公平的结果,现在我想上传图像在Twitter上使用Text,它真的可能吗? 如果是的话,怎么做?以下代码用于发布文字:

<?php

$consumerKey="SDJFOISDJF4EIFOISDJFOJFOIJSDFJ";
$consumerSecret="KJSFIOERSDJFLKMEROI3JRISDFJSDF";
$oAuthToken="KSDJFOFJIEIOR5343904830948DKFDSLFJSDLKFJSDLKFJ";
$oAuthSecret="ASJDFOIRU3RUIODJFKLSDFOIEJRTOJOIDFJOIEJTROIEJOIDJF";

include ("OAuth.php");
include ("twitteroauth.php");

$twitter=new TwitterOAuth($consumerKey,$consumerSecret,$oAuthToken,$oAuthSecret);


if($_GET['msg']!="")
{
if(isset($_GET['msg']))
{
    $twittMsg=$_GET['msg'];

    $twitter->post('statuses/update',array('status'=>$twittMsg));
    print(json_encode("one")); 

}else
{
    print(json_encode("two")); 
}
}
else
{
    print(json_encode("Three"));
}

感谢任何帮助,请你发表评论......

4 个答案:

答案 0 :(得分:2)

如果您使用sdk tmhOAuth,您可以这样做!

$code = $tmhOAuth->request('POST', 'https://api.twitter.com/1.1/statuses/update_with_media.json',
  array(
    'media[]' => $image,
    'status'   => "your message"
  ),
  true, // use auth
  true  // multipart
);

$ image可以是来自您的服务器的图像,也可以是来自网址的粗略图像。 ($ image = file_get_content(图片的网址)

答案 1 :(得分:1)

您可以尝试 twitter async

define( 'CONSUMER_KEY' , 'your twitter app consumer key');
define( 'CONSUMER_SECRET' , 'your twitter app consumer key secret');
define( 'TOKEN_KEY' , 'your token');
define( 'TOKEN_SECRET' , 'your token secret');

include 'EpiOAuth.php';
include 'EpiTwitter.php';

$twttr = new EpiTwitter(CONSUMER_KEY, CONSUMER_SECRET, TOKEN_KEY, TOKEN_SECRET);

$params = array('@image' => '@/path/to/image.jpg');
$response = $twttr->post_accountUpdate_profile_image($params);
echo $response->responseText;

查看此存储库Twitter Async

答案 2 :(得分:0)

我想你可以试试这个,下载Twitter Api for php&amp;创造了一个功能。

function image_upload(){    

define( 'YOUR_CONSUMER_KEY' , 'your twitter app consumer key');
define( 'YOUR_CONSUMER_SECRET' , 'your twitter app consumer key secret');

require ('twitt/tmhOAuth.php');
require ('twitt/tmhUtilities.php');

$tmhOAuth = new tmhOAuth(array(
         'consumer_key'    => "YOUR_CONSUMER_KEY",
         'consumer_secret' => "YOUR_CONSUMER_SECRET",
         'user_token'      => "YOUR_OAUTH_TOKEN",
         'user_secret'     => "YOUR_OAUTH_TOKEN_SECRET",
));

$image = 'image.jpg';

$code = $tmhOAuth->request( 'POST','https://upload.twitter.com/1/statuses/update_with_media.json',
   array(
        'media[]'  => "@{$image};type=image/jpeg;filename={$image}",
        'status'   => 'message text written here',
   ),
    true, // use auth
    true  // multipart
);

if ($code == 200){
   tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
}else{
   tmhUtilities::pr($tmhOAuth->response['response']);
}
return tmhUtilities;

}

请参阅此回购https://github.com/themattharris/tmhOAuth

答案 3 :(得分:0)

只需使用'tmhOAuth for PHP'测试此CODE代码段。

include '/var/www/apps/Twitter/tmhOAuth.php';

$tmhOAuth = new tmhOAuth(array(
  'consumer_key' => 'x',
  'consumer_secret'=> 'x',
  'token' => 'x',
  'secret' => 'X'
));

$image ='/var/www/images/SpinHistoryRoulette.png';
$status = "Picture posting test bitcoin-roulette.com";


$tmhOAuth->request('POST', 'https://api.twitter.com/1.1/statuses/update_with_media.json', array( 'media[]' => "@{$image}", 'status' => $status), true,  true );

在最后一行代码中:

  • 确保$ image是对本地文件名(NOT URL)的引用
  • 检查请求参数()如上所述

可替换地, 参考: http://www.stirring-interactive.com/blog/tweet-images-using-twitter-api/