【问题标题】:How to post Image on Twitter using php?如何使用 php 在 Twitter 上发布图片?
【发布时间】:2014-08-31 22:53:08
【问题描述】:

没有图像的文本很容易发布,我发现了很多有用的链接来发布文本,但是当我使用php代码在twitter上搜索如何将图像与文本一起发布时,没有公平的结果,现在我想上传图像用推特上的文字,真的有可能吗? 如果是,该怎么做?我用来发布文本的以下代码:

<?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"));
}

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

【问题讨论】:

标签: php twitter


【解决方案1】:

如果你使用 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 可以是来自您的服务器的图像,也可以是来自 url 的原始图像。 ($image = file_get_content(图片的url)

【讨论】:

  • 看 @jean 现在在本地主机上工作 我有一个和同一个导演的图像我可以这样称呼它 $image="a53.jpg";这是正确的方法吗?
  • 是的,如果图像在同一个文件夹中! 'media[]' => @{$image};type=image/jpeg;filename={$image} 其中 $image 是“yourphoto.jpg”
  • $image = "my_image.jpg" $code = $tmhOAuth->request('POST', 'api.twitter.com/1.1/statuses/update_with_media.json', array('media[]' => @{$image}; type=image/jpeg;filename={$image}, 'status' => "your message" ), true, // 使用 auth true // multipart );并且您需要使用 tmhOAuth 库来使用它。
  • 你能通过这个电子邮件把你的代码发给我吗(aryan.muhammad.qadir@gmail.com),非常感谢,只是这里有点困难。
【解决方案2】:

你可以试试 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

【讨论】:

    【解决方案3】:

    我想你可以试试这个,为 php 下载 Twitter Api 并创建一个函数。

    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

    【讨论】:

      【解决方案4】:

      只需使用“tmhOAuth library for PHP”测试此 CODE sn-p。

      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 是对本地文件名的引用(不是 URL)
      • 检查request()的参数如上

      或者, 参考: http://www.stirring-interactive.com/blog/tweet-images-using-twitter-api/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-23
        • 2013-12-26
        • 1970-01-01
        • 2017-04-06
        • 2012-09-20
        • 1970-01-01
        相关资源
        最近更新 更多