【问题标题】:PHP: CURL PUT FOR Https Url not working as fopen failsPHP:CURL PUT FOR Https Url 不能作为 fopen 工作失败
【发布时间】:2012-11-22 14:11:09
【问题描述】:

我想使用 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);

【问题讨论】:

  • 让我看看。我的魔法水晶球告诉我我们有新月,这就是你的代码不起作用的原因。说真的,请将您看到的错误消息添加到您的问题中。

标签: php curl https put


【解决方案1】:

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

$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);

【讨论】:

    【解决方案2】:

    代替:

    curl_setopt($ch, CURLOPT_PUT, true);
    

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

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-03
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      相关资源
      最近更新 更多