0

我想使用 Curl 将图像上传到 url。但由于图像文件位于 https url 上,我无法使用 fopen 读取文件。

代码如下。

$file = "https://xyz.com/image.jpg";
$url = "http://abc.com/upload.php";

$fp = fopen($file, "r");
$headers = array("Content-Type: xml");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
4

2 回答 2

2

我认为这应该可行,假设您不需要将上传的文件数据作为命名键/值对传递。

$file = "https://xyz.com/image.jpg";
$url = "http://abc.com/upload.php";

$fileData = file_get_contents($file);

$fp = fopen($file, "r");
$headers = array("Content-Type: xml");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fileData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
于 2012-11-22T14:22:19.187 回答
0

代替:

curl_setopt($ch, CURLOPT_PUT, true);

PUT API 请求的 opt 设置在使用以下设置时可以正常工作:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
于 2013-09-13T07:32:28.277 回答