dongxie45083 2017-01-02 23:35
浏览 236

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

This is an auto promotion system (written in php) for a website, however it seems to give an error that I'm not familiar with. I replaced the website name with 'example' for privacy reasons.

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

When accessing the page I get the page error:

Bad Request - Invalid Content Length

HTTP Error 400. There is an invalid content length or chunk length in the request.

I'm not sure why this is happening, and I've tried just about everything, even switching hosts, but the issue still arises. What can I do to fix this?

  • 写回答

1条回答 默认 最新

  • douyijin7741 2017-01-03 00:29
    关注

    It may be that within the changeRank() function you are doing a POST request with no data. Try adding curl_setopt($ch, CURLOPT_POSTFIELDS, array()); before the curl_exec().

    Or you can add it to your curl_setopt_array() declaration.

    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退