2012-08-28 55 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。