0

I am very very new to twitter api, today i first time signup for twitter api in my life. my aim is to post pictures from my website with a message on twitter. I have download its current library from internet, it gives me following 2 files and some examples.

i have include those two files and get this code photo_tweet.php means tweet with photo

here is my code

<?php
require 'tmhOAuth.php';
require 'tmhUtilities.php';

$tmhOAuth = new tmhOAuth(array(
  'consumer_key'    => 'ZlhOWeCWG2MS5Wxxxxxx',
  'consumer_secret' => 'DIPjoKcIWjpGWmw5jGJSKGAOLxxxxxx',
  'user_token'      => 'xxxxx-OGtPyRSUOUaR6XQRLAFVuv14xxxxxx',
  'user_secret'     => 'xxxxxoimsrNFhlz7mPa9h5pyVSjxxxxxxx',
));

// we're using a hardcoded image path here. You can easily replace this with
// an uploaded image - see images.php in the examples folder for how to do this
// 'image = "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",

// this is the jpeg file to upload. It should be in the same directory as this file.
$image = 'a.jpg';

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

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

?>

a.jpg is on my server.

here is my settings on twitter!

Request type: GET Request URI:* https://api.twitter.com/1/

when I upload all this code and access my code it returns blank scree, when i echo $code; it returns 0

its all what i have observed and experienced. I am in need of your Kind help. I shall be very very thank full to you

Note: Event When I removed my consumer key and consumer secret this code does not show any error. just a blank screen. I want this to show errors in case of any problem but no error


Here is the code for status only it work for me

$consumerKey = $consumer_key;
$consumerSecret = $consumer_secret;
$OAuthToken = $user_token;
$OAuthSecret = $user_secret;

include "OAuth.php";
include "twitteroauth.php";

$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret);

$image = 'a.jpg';

$optsArray['status'] = 'Hi';



if (isset($_GET['msg'])) {
    $tweetmsg = $_GET['msg'];
    $tweet->post('statuses/update',$optsArray);

   if($tweet)
     echo "Your message has been sent to Twitter.";
   else
     echo "Your message has not been sent to Twitter.";

} else {
    echo "Your message has not been sent to Twitter.";
}

but it post only status when i use

update_with_media.json

and

$optsArray['@media[]'] = "@{$image}";

it does not work , please help me to figure out this issue

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Hilton Sky
  • 31
  • 1
  • 1
  • 11
  • 1
    have you tried the Twitter to post simple message ? I suggest you first post a simple message to test your code / api keys/ tokens – Arfeen Feb 28 '12 at 07:19
  • i use another simple code to test but it also gives blank screen. I have copied my keys and tokens from twitter settings correctly. – Hilton Sky Feb 28 '12 at 07:27

1 Answers1

1

You might want to check this article out. It walks you through the steps and gives many examples.

The following methods are provided by the API. twitpic logo Post pictures on twitpic API using PHP

METHOD: uploadAndPost (http://twitpic.com/api/uploadAndPost)
METHOD: upload (http://twitpic.com/api/upload)
Error codes
    1001 – Invalid twitter username or password
    1002 – Image not found
    1003 – Invalid image type
    1004 – Image larger than 4MB

Given above is the API description from twitpic’s API page. But you need not to get into those complicated details. Instead you can use a class which will let you post a picture on twitpic and update your twitter status on the same account. This is simple & easy to use. Check out the code below.

<?php
class twitpic
{
  /* 
   * variable declarations
   */
  var $post_url='http://twitpic.com/api/upload';
  var $post_tweet_url='http://twitpic.com/api/uploadAndPost';
  var $url='';
  var $post_data='';
  var $result='';
  var $tweet='';
  var $return='';

/*
* @param1 is the array of data which is to be uploaded
* @param2 if passed true will display result in the XML format, default is false
* @param3 if passed true will update status twitter,default is false
*/

  function __construct($data,$return=false,$tweet=false)
  {
    $this->post_data=$data;
    if(empty($this->post_data) || !is_array($this->post_data)) //validates the data
      $this->throw_error(0);
    $this->display=$return;
    $this->tweet=$tweet;

  }

  function post()
  {
    $this->url=($this->tweet)?$this->post_tweet_url:$this->post_url; //assigns URL for curl request based on the nature of request by user
    $this->makeCurl();
  }
  private function makeCurl()
  {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($curl, CURLOPT_URL, $this->url);
    curl_setopt($curl, CURLOPT_POST, 3);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $this->post_data);
    $this->result = curl_exec($curl);
    curl_close($curl);
    if($this->display)
    {
      header ("content-type: text/xml");
      echo $this->result ;
    }

  }
  private function throw_error($code) //handles few errors, you can add more

  {
    switch($code)
    {
      case 0:
        echo 'Think, you forgot to pass the data';
        break;
      default:
        echo 'Something just broke !!';
        break;
    }
    exit;
  }
} //class ends here

?>

The above PHP class does all the trick to upload picture on twitpic and post status on the twitter. You can use this class with both using HTML form or using a PHP script to automatically upload the image and post the tweet. You can use the following HTML form and PHP script in combination to upload picture by taking input from the user.

// This block of code should be written above the HTML and it will exit after the picture has been uploaded. If //you have turned display on (by passing 3rd param as true) then it will display the success message.
if($_POST)
{

  $file=$_FILES['media'];
  $postfields = array();

  $postfields['username'] = $_POST['username'];

  $postfields['password'] = $_POST['password'];
  $postfields['message'] = $_POST['message'];
  $postfields['media'] = "@$file[tmp_name]";

  $t=new twitpic($postfields,true,true);
  $t->post();
  exit;
}

<style type="text/javascript">
  *{font-family:verdana;}
  span{font-size:12px;color:#393939;}
  h3{font-size:14px;color:#5AAAF7;}
</style>
<body>

  <h3>Upload your pic to twitpic, and post status on twitter</h3>
  <form method="post"  enctype="multipart/form-data" action="<?= $_SERVER[PHP_SELF] ?>"   >
    <p><span style="height:40px;font-weight:bold;margin-right:56px;">Twitter Username :</span><input type="text" name="username" /></p>
    <p><span style="height:40px;font-weight:bold;margin-right:61px;">Twitter Password:</span><input type="password" name="password" /></p>

    <p><span style="vertical-align:text-top;height:40px;font-weight:bold;margin-right:28px;">Message to be posted :</span> <textarea cols="35" rows="2" name="message"></textarea></p>
    <p><span style="vertical-align:text-top;height:40px;font-weight:bold;">Choose an image to upload: </span><input type="file" name="media" /></p>
    <p style="width:250px;text-align:right;margin-top:50px;"><input type="submit" value="Upload&nbsp;&raquo;" /> </p>
  </form>
  <sup>Script powered by <a href="http://www.digimantra.com/">www.digimantra.com</a></sup>
</body>

You can skip posting update to twitter by passing the third argument as false or just by skipping it. If you want to upload image programmatically, without the user input or the form then you can do it using the following code. Make sure the image path is correctly mention, else it will throw an error.

<?php
$file='file_to_be_uploaded.gif';
$postfields = array();

$postfields['username'] = 'twitter_username';

$postfields['password'] = 'twitter_password';
$postfields['message'] = 'Message to be posted' ;
$postfields['media'] = "@$file"; //Be sure to prefix @, else it wont upload

$t=new twitpic($postfields,true,true);
$t->post();
?>

This above class posts the image in the binary format as discussed in Send binary data using curl php. You can change the class to suit your need and you can also enhance it the way you like. Hope this post helped you learn something.

Wth123
  • 46
  • 5
  • 1
    thanks for your kind response, above code works perfectly. but it needs to put username and password. I want to use Oauth. for example if a user come to my website he likes a picture he can share this picture to twitter without giving his username and password on my website. Using twitter API Oauth, – Hilton Sky Feb 29 '12 at 06:44
  • Event When I removed my consumer key and consumer secret and other details this code does not show any error. just a blank screen. I want this to show errors in case of any problem but no error – Hilton Sky Feb 29 '12 at 06:58
  • Not working. Error When I copied same username and password to login twitter on login page, it worked fine. :( – SJSSoft Jun 20 '14 at 14:31