How to connect to twitpic API with PHP?
I can't connect to the twitpic API. I've tried this:
$post = array (
'key' => 'fghgfhdfghf',
'consumer_token' => 'retert',
'consumer_secret' => 'ertertwerwtetey',
'oauth_token' => 'wety43y4y4wy',
'oauth_secret' => 'seryeryereshrh',
'message' => 'ffff',
'media' => file_get_contents('http://img.yandex.net/i/www/logo.png')
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.twitpic.com/1/upload.json');
curl_setopt($ch, CURLOPT_RETURNT开发者_开发技巧RANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
echo $errno = curl_errno($ch);
curl_close ($ch);
echo $response;
What's wrong with this code? Can anyone give me an example of how to get this working?
"file_get_contents" is not allways Enabled on a server
PEAR has a package for that which abstracts you from the details: Services_Twitter_Uploader
You can't stream the image just like that. You have to download it first, for example, to a temporary file:
$tempname = tempnam( sys_get_temp_dir(), 'twitpic' );
file_put_contents( $tempname, file_get_contents( 'http://img.yandex.net/i/www/logo.png' ) );
The media
field will be then set like this:
$post['media'] = "@$tempname";
After the requested is done (after curl_exec()
), you can delete the temporary file:
unlink( $tempname );
精彩评论