【问题标题】:CURL request works via command line but not in PHPCURL 请求通过命令行工作,但不在 PHP 中
【发布时间】:2020-07-14 11:25:58
【问题描述】:

以下命令在命令行中运行完美。

curl -X POST "http://xxx.xx.xx.xx:xxxx/api/upload?id=123456" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "file=@a.pdf;type=application/pdf"

当我使用命令行点击上述命令时,它会在返回响应中给出一个令牌。如果我也通过POSTMAN 调用它工作正常。
但是当我通过请求时 PHP CURL 它既没有给出任何错误也没有给出响应。我从POSTMAN复制了 PHP-CURL 代码

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://xxx.xx.xx.xx:xxxx/api/upload?id=123456",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => array('file'=> new CURLFILE('/C:/Users/User/Downloads/c.pdf')),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
?>

【问题讨论】:

  • 使用正确的大小写:CURLFile
  • 您是否在 php.ini 中启用了 curl 扩展?
  • @DarkBee 是的,我启用了 curl 扩展。
  • here
  • 不确定文件路径中的第一个斜杠在做什么,无论如何最好在 windows env 中使用反斜杠,例如new CURLFILE('C:\Users\User\Downloads\c.pdf')

标签: php curl php-curl


【解决方案1】:

尝试将Content-Type: multipart/form-data 添加到您的请求中,例如 CURLOPT_HTTPHEADER =&gt; array('Content-Type: multipart/form-data')

【讨论】:

    猜你喜欢
    • 2012-07-28
    • 2017-11-19
    • 1970-01-01
    • 2017-05-07
    • 2013-02-03
    • 1970-01-01
    • 2018-05-10
    • 2013-08-24
    • 1970-01-01
    相关资源
    最近更新 更多