2012-08-28 56 views
0

我想使用curl將圖片上傳到Google Picasa,我發現這一行代碼使用curl將圖片上傳到Google Picasa但我不知道如何將其轉換爲php和捲曲以便能夠使用它。如何在php curl中編寫此代碼

curl --silent --request POST --data-binary "@sweeping_the_rock.png" --header "Slug: Sweeping the rock" --header "Content-Type: image/png" --header "Authorization: GoogleLogin auth=ABCDEFG" "http://picasaweb.google.com/data/feed/api/user/brad.gushue/albumid/5113621341847124417" | tidy -xml -indent -quiet 

$file = "C:/Users/Michael/Desktop/182178_271150652992340_118089494_n.jpg"; 
$postimg = "https://picasaweb.google.com/data/feed/api/user/userId/albumid/albumId"; 
$header = array("Content-Type: image/jpeg", 
     "Content-Length: ".filesize($file), 
     "Authorization: GoogleLogin auth=$token", 
     "Slug: 182178_271150652992340_118089494_n.jpg" 
); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $postimg); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file)); 
$hasil = curl_exec($ch); 
curl_close($ch); 

來源:https://developers.google.com/gdata/articles/using_cURL#media-support

+2

你好,歡迎來到StackOverflow。儘管您的計劃可能會遇到麻煩,但我們強烈建議(並堅持)在向社區尋求幫助之前,您已盡其最大努力。很明顯,您遇到了您嘗試過的代碼問題,我們很樂意提供幫助!如果您沒有花費精力設計自己的解決方案,我們傾向於對您的問題進行降級投票並關閉它。你必須至少嘗試一個解決方案。絕不要求別人寫代碼來代替誠實的努力。總之,[你試過什麼](http://www.whathaveyoutried.com)? – Matt

+0

[PHP Curl文檔](http://www.php.net/manual/en/ref.curl.php)應該是有用的。 –

+0

那麼你在哪裏遇到問題?你捲曲錯誤?你沒有得到你期望從API中得到的迴應嗎? –

回答

0

也許你沒有正確發送文件。你需要添加CURL_OPTs文件上傳所以它的MIME編碼和正確傳遞(一個上傳文件的形式編碼完全不同,那麼發送簡單的POST數據)

http://php.net/manual/en/function.curl-setopt.php

CURLOPT_INFILE的文件應該從上傳時讀取傳輸。

CURLOPT_INFILESIZE將文件上傳到遠程站點時文件的預期大小(以字節爲單位)。請注意,使用此選項不會阻止libcurl發送更多數據,因爲發送的內容取決於CURLOPT_READFUNCTION。