[英]How to connect to twitpic API with PHP?
我無法連接到 twitpic API。 我試過這個:
$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_RETURNTRANSFER, 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;
這段代碼有什么問題? 誰能給我一個如何讓這個工作的例子?
“file_get_contents”並非總是在服務器上啟用
PEAR 有一個 package 用於將您從細節中抽象出來: Services_Twitter_Uploader
你不能像這樣 stream 圖像。 您必須先將其下載到一個臨時文件中:
$tempname = tempnam( sys_get_temp_dir(), 'twitpic' );
file_put_contents( $tempname, file_get_contents( 'http://img.yandex.net/i/www/logo.png' ) );
然后將media
字段設置如下:
$post['media'] = "@$tempname";
請求完成后(在curl_exec()
之后),您可以刪除臨時文件:
unlink( $tempname );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.