How to post Image on Twitter using php?

痴心易碎 提交于 2019-12-29 10:07:04

问题


It's easy to post Text without image, I found many useful link to post text , but when I searched for how to post Image with Text together on twitter using php code , there was no fair result, now I want to upload image with Text on twitter , does it really possible ? if yes, how to do that? the below code I used to post 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"));
}

THANKS for any help guys please your comment ...


回答1:


If you use the sdk tmhOAuth, you can do it like that!

$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 can be an image from your serveur or a brut image from an url. ($image = file_get_content(url of the image)




回答2:


You can try twitter async library

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;

See this repository Twitter Async




回答3:


I think you can try this, Download Twitter Api for php & created one function.

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;

}

Please refer this repo https://github.com/themattharris/tmhOAuth




回答4:


Just test this CODE snippet using 'tmhOAuth library for PHP'.

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

In the last line code:

  • make sure the $image is a reference to a local filename (NOT URL)
  • check the parameters to request() are like above

Alternatively, Refer: http://www.stirring-interactive.com/blog/tweet-images-using-twitter-api/



来源:https://stackoverflow.com/questions/25409171/how-to-post-image-on-twitter-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!