Posting a file / image to twitpic.com using PHP and cUrl : GET POST : Development PHP Source Code


PHP Source Code » Development » GET POST »

 

Posting a file / image to twitpic.com using PHP and cUrl


Oficial documentation for API API DOCUMENTATION
- METHOD: uploadAndPost
- METHOD: upload
- Error codes
- Image Thumbnails


METHOD: http://twitpic.com/api/uploadAndPost

Use this method to upload an image to TwitPic and to send it as a status update to Twitter.

Fields to post in
(post data should be formatted as multipart/form-data):
- media (required) - Binary image data
- username (required) - Twitter username
- password (required) - Twitter password
- message (optional) - Message to post to twitter. The URL of the image is automatically added.

Sample response:
<?xml version="1.0" encoding="UTF-8"?>
<rsp status="ok">
 <statusid>1111</statusid>
 <userid>11111</userid>
 <mediaid>abc123</mediaid>
 <mediaurl>http://twitpic.com/abc123</mediaurl>
</rsp>


METHOD: http://twitpic.com/api/upload

Use this method if you only want to upload a photo to TwitPic.

Fields to post in:
(post data should be formatted as multipart/form-data)
- media (required) - Binary image data
- username (required) - Twitter username
- password (required) - Twitter password

Sample response:
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok">
 <mediaid>abc123</mediaid>
 <mediaurl>http://twitpic.com/abc123</mediaurl>
</rsp>


Sample error response:
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="fail">
    <err code="1001" msg="Invalid twitter username or password" />
</rsp>


Error codes and their descriptions:
1001 - Invalid twitter username or passwor
1002 - Image not found
1003 - Invalid image type
1004 - Image larger than 4MB


Image thumbnails:
Use the following URL structure to add Twitpic thumbnails into your app:

http://twitpic.com/show/<size>/<image-id>

Size: mini or thumb
Image ID: this is the ID of the Twitpic photo

Example: http://twitpic.com/show/thumb/1e10q

*note: Twitpic Community Guidelines specify that if you use a Twitpic thumbnail in your app, the photo must link back to its original photo page or a link to the original photo page must be provided somewhere within context to the thumbnail

**note: this URL may forward to an amazon s3 URL


How do I get "from [my_application]" appended to updates sent from my API application?
Please email noah@twitpic.com with the name, URL, and description of your application. Your application must have a web site to which we can link in order to this up. Please allow up to 48 hours for the link to your application to go live, as this is a manual process contingent on TwitPic's deployment schedule.
<?

// Posting a file to TwitPic using PHP and cUrl.
// here are two possible ways to do this with php and curl.
// : navioo.com


// 1. using 'exec' to get php to execute a command line curl function

// $command = "curl -F media=@/full/path/to/your/image.jpg
             
-F username=YOUR_TWITTER_USERNAME -F password=YOUR_TWITTER_PASSWORD 
              
-F message="your twitter message\" http://twitpic.com/api/uploadAndPost";
// exec $command;

// nb: many php systems will not allow this. And you should also use caution when
// allowing your system to use the exec command. there are 'safe' ways for doing this.
//  See php.net for more info.
// nb: you MUST have @ at the start of your path. 
//       This is to tell curl that its a binary file you want to post



// 2. using PHPs built in curl stuff

$twitterUser "YOUR_TWITTER_NAME";
$twitterPass "YOUR_TWITTER_PASSWORD";

$tweet "The message you want to tweet";

$fullfilepath "/full/path/to/your/image.jpg";
$upload_url "http://twitpic.com/api/uploadAndPost";
$params = array(
    
"media"=>"@$fullfilepath",
    
"username"=>"$twitterUser",
    
"password"=>"$twitterPass",
    
"message"=>"$tweet"
);

// NB: do NOT forget to have the '@' sign at the start of the filepath. very common mistake!

if(!file_exists($fullfilepath)){

    echo 
"file does not exist!";
    exit;

}


$ch curl_init();
curl_setopt($chCURLOPT_VERBOSE1);
curl_setopt($chCURLOPT_URL$upload_url);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_POSTFIELDS$params);
$response curl_exec($ch);
curl_close($ch);


// debug the raw response
echo "<hr>response:<br /><pre>";
print_r($response);
echo 
"</pre><hr>";


// then you could parse the response using SimpleXML

$rsp simplexml_load_string($response);

echo 
"<br />status :".$rsp['status'];
echo 
"<br />statusid: ".$rsp->statusid;
echo 
"<br />userid: ".$rsp->userid;
echo 
"<br />mediaid: ".$rsp->mediaid;
echo 
"<br />mediaurl: ".$rsp->mediaurl;



// hope this helps!
// kosso

/*
ps: if you wanted to create simple html form to do this. That's easy too:

<form enctype="multipart/form-data" method="POST" action="http://twitpic.com/api/uploadAndPost">
    choose a picture: <input name="media" type="file" /> <br />
    twitter username: <input type="text" name="username" /> <br />
    twitter password: <input type="text" name="password" /> <br />
    tweet message: <input type="text" name="message" /> <br />
    <input type="submit" value=" send pic to twitbin " />
</form>




*/


?>



?>


HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo Development
» GET POST