【问题标题】:cURL post request methodcURL post 请求方法
【发布时间】:2018-06-05 06:59:36
【问题描述】:

您好,我的 cURL 发布请求方法有问题。我需要从其他主机获取内容,所以我从http://php.net/manual/de/book.curl.php 获取了这个方法,因为我知道目标主机也使用 cURL 并向执行可执行文件(exe)的服务发出请求。

    function postRequest($url, $data, $refer = "", $timeout = 10, $header = [])
{
    $curlObj = curl_init();
    $ssl = stripos($url,'https://') === 0 ? true : false;
    $options = [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_AUTOREFERER => 1,
        CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)',
        CURLOPT_TIMEOUT => $timeout,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
        CURLOPT_HTTPHEADER => ['Expect:'],
        CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
        CURLOPT_REFERER => $refer
    ];
    if (!empty($header)) {
        $options[CURLOPT_HTTPHEADER] = $header;
    }
    if ($refer) {
        $options[CURLOPT_REFERER] = $refer;
    }
    if ($ssl) {
        //support https
        $options[CURLOPT_SSL_VERIFYHOST] = false;
        $options[CURLOPT_SSL_VERIFYPEER] = false;
    }
    curl_setopt_array($curlObj, $options);
    $returnData = curl_exec($curlObj);
    if (curl_errno($curlObj)) {
        //error message
        $returnData = curl_error($curlObj);
    }
    curl_close($curlObj);
    return $returnData;
}

我这样调用方法:

$command= array();

 $command["cmd"] = "D://Workspace/Interne_Entwicklung/Folder1/Folder2/Executable.exe " . "--command " . chr(34) .
                    "[{'filters': [ {'Z_RG_Generiert':0} ],auth:[{'userid':'gartner'}],'RequestId': 105.1,'Status': 0}]" . chr(34);
 echo postRequest("localhost:8080",$command);

它返回一个错误 (“HTTP 错误 411。请求必须分块或具有内容长度。”) 我已经尝试向选项数组添加一个选项,该选项将 Content-Length 定义为 CURLOPT_HTTPHEADER 选项,如下所示:

$command= array();

 $command["cmd"] = "D://Workspace/Interne_Entwicklung/PreisigTest2/PreisigTest2/bin/Debug/PreisigTest2.exe " . "--command " . chr(34) .
                    "[{'filters': [ {'Z_RG_Generiert':0} ],auth:[{'userid':'gartner'}],'RequestId': 105.1,'Status': 0}]" . chr(34);
 $header=array();
 $header['Content-Length'] = strlen(json_encode($command));
 echo postRequest("localhost:8080",json_encode($command),"",10,$header)

所以我的问题是:

  • 此消息的原因是什么?
  • 如何解决这个问题,定义内容长度还是有其他方法?

【问题讨论】:

    标签: php curl php-curl


    【解决方案1】:

    411 代码是指需要长度。所以这里也需要发送 Content-Length 头。

    $command= array();
    $command["cmd"] = "D://Workspace/Interne_Entwicklung/PreisigTest2/PreisigTest2/bin/Debug/PreisigTest2.exe " . "--command " . chr(34) .
                        "[{'filters': [ {'Z_RG_Generiert':0} ],auth:[{'userid':'gartner'}],'RequestId': 105.1,'Status': 0}]" . chr(34);
     $header = array('Content-Length: ' . strlen(json_encode($command)));
     echo postRequest("localhost:8080",json_encode($command),"",10,$header);
    

    【讨论】:

    • 一旦尝试使用静态标头,例如 curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-length: ".strlen(json_encode($data))));
    • 是这个问题和响应主机的证书问题。
    猜你喜欢
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 2019-01-17
    • 2015-05-25
    • 2018-09-14
    • 2020-09-07
    相关资源
    最近更新 更多