POST请求需要laravel中的Content-length标头

时间:2018-03-12 06:49:39

标签: laravel http-headers

       $output = Curl::httpGet("https://bhleh.com/emailstorage",  "POST", $params);                    
       return $output;

这是在我的laravel但是当我试图运行这个我得到一个错误说

411. That’s an error.

POST requests require a Content-length header. That’s all we know.

我尝试在我的中间件文件夹中添加头文件,但似乎没有任何效果。 我发现内容长度标题不包含在laravel中(我认为不那么确定)所以如何添加它 请注意我是laravel的新手

2 个答案:

答案 0 :(得分:1)

这个解决方法是我用这个

替换了整个代码
$data_string = json_encode($params);                                                                                                                                                                                                                   
    $ch = curl_init('https://bhleh.com/emailstorage');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );                                                                                                                   

    $result = curl_exec($ch);              
    return  $result;

答案 1 :(得分:0)

我遇到了同样的问题,但我没有按照 API 端点发送 CURLOPT_POSTFIELDS。我不需要发送任何变量,但是当我发送带有空白数组的 CURLOPT_POSTFIELDS 时,它开始工作。

$data_string = array();
$ch = curl_init('https://bhleh.com/emailstorage');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data_string));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json'
);

$result = curl_exec($ch);
return $result;
相关问题