且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP cURL Content-Length 和 Content-Type 错误

更新时间:2022-10-30 09:25:28

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

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

你需要这样做:

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

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

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

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

 '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);回声 $login;

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

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.

Everything is equal, except of:

Browser:

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

PHP cURL:

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' 
));

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.

If you set the CURLOPT_POSTFIELDS value as an array, it will automatically submit the request as multipart/form-data and use a boundary. If you pass a string, it will use application/x-www-form-urlencoded so make sure you pass a urlencoded string to CURLOPT_POSTFIELDS and not an array since you want form-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-urlencoded encoding on the form.

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.

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;