通过php curl但不通过邮递员发送文件时,IIS返回错误500

时间:2020-06-09 11:08:18

标签: php curl iis

这是我的情况。一个用PHP(7.x)编写的Web应用程序,该应用程序在某个点通过cURL将一些信息和2个文件发送到Web服务器。 Web服务器上还有一些用PHP(7.x)编写的Rest API。

我的问题是,当我发送文件时,服务器(Win 2008 R2)上的IIS(7.5)返回“ 500内部服务器错误”。查找错误详细信息,错误代码为“ 0x80070026”

如果我使用Apache在另一台服务器上部署API,则该API可以正常运行。 在IIS上,我已经做了一些测试,并且: -如果我发送不带文件的cURL命令,则API运行正常。 -如果我用Postman调用API并附带文件,则该API也可以正常运行。

网络应用具有此代码

$url = $this->config->item('API_endpoint').'Scans/scans/';
        $username = $this->config->item('API_username');
        $password = $this->config->item('API_password');
        //$header = array('Content-Type: multipart/form-data');

        $postData = array(
            'scanDate' => date('Y-m-d H:i:s'),
            ...other data...
            'file1' => curl_file_create('file1.stl'),
            'file2' => curl_file_create('file2.stl')
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        //curl_setopt($ch, CURLOPT_HEADER, false);

        $result=curl_exec($ch);

服务器上的脚本读取数据并保存文件。

我也尝试添加 curl_setopt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');

IIS日志显示

2020-06-09 10:37:27 172.31.2.11 POST /index.php/Scans/scans/-yyyy-xxxx Mozilla / 4.0 +(兼容; + MSIE + 5.01; + Windows + NT + 5.0)< strong> 500 0 38 405

谢谢!


编辑: 在我的curl脚本中,我添加了var_dump($ result);

this is the output, with "detailed" error of IIS

1 个答案:

答案 0 :(得分:0)

我在 IIS 上的 PHP 7.3 遇到了类似的问题,我发现 CURL 在原始 POST 请求之后发送 100-continue 请求,而 POSTMAN 没有。

我添加了选项 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); 我的 curl 请求避免它

这使得 CURL 停止发送 100-continue 并且 IIS 不再抛出带有错误代码 x80070026500

相关问题