PHP cURL Content-Length 和 Content-Type 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9152165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:15:58  来源:igfitidea点击:

PHP cURL Content-Length and Content-Type wrong

phpcurlheaderrequest

提问by Simon

I'm trying to login to a site via PHP cURL and I'm only getting "Bad Request" responses.

我正在尝试通过 PHP cURL 登录到一个站点,但我只收到“错误请求”响应。

I played around with hosts file and set it to my server to check which Request Headers my browser sends and compare it to the request headers sent by cURL.

我使用了主机文件并将其设置到我的服务器以检查我的浏览器发送的请求标头并将其与 cURL 发送的请求标头进行比较。

Everything is equal, except of:

一切都是平等的,除了:

Browser:

浏览器:

Content-Type: application/x-www-form-urlencoded
Content-Length: 51

PHP cURL:

PHP 卷曲:

Content-Length: 51, 359
Content-Type: application/x-www-form-urlencoded; boundary=----------------------------5a377b7e6ba7

I already set that values with this command, but it still sends the wrong headers:

我已经用这个命令设置了这些值,但它仍然发送错误的标头:

curl_setopt($this->hCurl, CURLOPT_HTTPHEADER, array(
    'Expect:',
    'Content-Type: application/x-www-form-urlencoded',
    'Content-Length: 51' 
));

回答by drew010

You shouldn't have to set the content-length yourself. If you use cURL to send an HTTP POST, it will calculate the content length for you.

您不必自己设置内容长度。如果您使用 cURL 发送 HTTP POST,它将为您计算内容长度。

If you set the CURLOPT_POSTFIELDSvalue as an array, it will automatically submit the request as multipart/form-dataand use a boundary. If you pass a string, it will use application/x-www-form-urlencodedso make sure you pass a urlencoded string to CURLOPT_POSTFIELDSand not an array since you want form-urlencoded.

如果您将该CURLOPT_POSTFIELDS值设置为数组,它将自动提交请求multipart/form-data并使用边界。如果您传递一个字符串,它将使用,application/x-www-form-urlencoded因此请确保您将 urlencoded 字符串传递给CURLOPT_POSTFIELDS而不是数组,因为您需要表单 urlencoded。

You need to be doing this:

你需要这样做:

$data = 'name=' . urlencode($value) . '&name2=' . urlencode($value2);
curl_setopt($this->hCurl, CURLOPT_POSTFIELDS, $data);

// NOT

$dataArray = array('name' => 'value', 'name2' => 'value2');
curl_setopt($this->hCurl, CURLOPT_POSTFIELDS, $dataArray);

In either case, you do not need to set the content length, but you have to use the first method to get application/x-www-form-urlencodedencoding on the form.

无论哪种情况,您都不需要设置内容长度,但您必须使用第一种方法来获取application/x-www-form-urlencoded表单上的编码。

If that doesn't help, post all the code relevant to setting up the curl request, (all the options, and data you are passing to it) and that should help solve the problem.

如果这没有帮助,请发布与设置 curl 请求相关的所有代码(所有选项和您传递给它的数据),这应该有助于解决问题。

EDIT:

编辑:

Added is an example I came up with that works (I get failed login).

添加的是我想出的一个例子(我登录失败)。

<?php

$URL_HOME  = 'http://ilocalis.com/';
$LOGIN_URL = 'https://ilocalis.com/login.php';

$ch = curl_init($URL_HOME);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$home = curl_exec($ch);

//echo $home;

$post = array('username' => 'drew', 'password' => 'testing 123');
$query = http_build_query($post);

curl_setopt($ch, CURLOPT_URL, $LOGIN_URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);

$login = curl_exec($ch);

echo $login;