HTTP 错误 400.请求中存在无效的内容长度或区块长度

  • 本文关键字:错误 请求 无效 存在 HTTP php
  • 更新时间 :
  • 英文 :


这是一个网站的自动推广系统(用php编写),但它似乎给出了一个我不熟悉的错误。出于隐私原因,我将网站名称替换为"示例"。

<?php
$group_id         = $_GET['groupId'];
$new_role_set_id  = $_GET['newRoleSetId'];
$target_user_id   = $_GET['targetUserId'];

$login_user       = 'username=user&password=pass';
$file_path_rs     = 'rs.txt';
$file_path_token  = 'token.txt';
$current_rs       = file_get_contents($file_path_rs);
$current_token    = file_get_contents($file_path_token);

function getRS()
{
    global $login_user, $file_path_rs;
    $get_cookies = curl_init('https://www.example.com/newlogin');
    curl_setopt_array($get_cookies,
        array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => true,
            CURLOPT_POST => true,
            // CURLOPT_HTTPHEADER => array("Content-Length: " . strlen($login_user)),
            CURLOPT_POSTFIELDS => $login_user
        )
    );
    $rs = (preg_match('/(.SECURITY=.*?);/', curl_exec($get_cookies), $matches) ? $matches[1] : '');
    file_put_contents($file_path_rs, $rs, true);
    curl_close($get_cookies);
    return $rs;
}

function changeRank($rs, $token) 
{
    global $group_id, $new_role_set_id, $target_user_id, $file_path_token;
    $promote_user = curl_init("http://www.example.com/groups/api/change-member-rank?groupId=$group_id&newRoleSetId=$new_role_set_id&targetUserId=$target_user_id");
    curl_setopt_array($promote_user,
        array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_HEADER => true,
            CURLOPT_HTTPHEADER => array("Cookie: $rs", "X-CSRF-TOKEN: $token")
        )
    );
    $resp = curl_exec($promote_user);
    $resp_header_size = curl_getinfo($promote_user, CURLINFO_HEADER_SIZE);
    $resp_header = substr($resp, 0, $resp_header_size);
    $resp_body = substr($resp, $resp_header_size);
    if (preg_match('/GuestData/', $resp_header)) {
        $resp_body = changeRank( getRS(), $token );
    } else if (preg_match('/Token Validation Failed/', $resp_header)) {
        $new_token = (preg_match('/X-CSRF-TOKEN: (S+)/', $resp_header, $matches) ? $matches[1] : '');
        file_put_contents($file_path_token, $new_token, true);
        $resp_body = changeRank( $rs, $new_token );
    }
    curl_close($promote_user);
    return $resp_body;
}

echo changeRank($current_rs, $current_token);

访问页面时,我收到页面错误:

Bad Request - Invalid Content Length
HTTP Error 400. There is an invalid content length or chunk length in the request.

我不确定为什么会发生这种情况,我已经尝试了几乎所有方法,甚至切换主机,但问题仍然存在。我该怎么做才能解决这个问题?

可能是在changeRank()函数中,您正在执行没有数据的POST请求。尝试在 curl_exec() 之前添加 curl_setopt($ch, CURLOPT_POSTFIELDS, array());

或者,您可以将其添加到 curl_setopt_array() 声明中。

最新更新