如何在php-ccurl中编写


How to write this in php curl

我想使用curl将图片上传到Google picasa,我发现了这行代码,它使用curl上传图片到Google Picassa,但我不知道如何将其转换为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-支持

可能您没有正确发送文件。您需要为文件上传添加CURL_OPTs,以便对其进行mime编码并正确传输(发布文件上传与发送简单的发布数据完全不同)

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

CURLOPT_INFILE上传时应读取传输的文件。

CURLOPT_INFILESIZE将文件上载到远程站点时,文件的预期大小(以字节为单位)。请注意,使用此选项不会阻止libcurl发送更多数据,因为发送的数据取决于CURLOPT_READFUNCTION。