首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的PHP代码返回错误400“您的浏览器发送了一个无效的请求”?

为什么我的PHP代码返回错误400“您的浏览器发送了一个无效的请求”?
EN

Stack Overflow用户
提问于 2021-11-24 01:32:21
回答 2查看 173关注 0票数 0

对API vv的vv请求

代码语言:javascript
复制
    $data = $_POST['csrf'];
    $headers = [
        "x-csrf-token: $data\r\n".
        "Content-Type: application/json\r\n".
        "Accept: application/json\r\n"
    ];
    $data = <<<DATA
    {
        "username": "string",
        "password": "string",
        "gender": "Unknown",
        "birthday": "2021-11-22T23:29:51.656Z",
        "isTosAgreementBoxChecked": true,
        "email": "string",
        "locale": "string",
        "assetIds": [
            0
        ],
        "bodyColorId": 0,
        "bodyTypeScale": 0,
        "headScale": 0,
        "heightScale": 0,
        "widthScale": 0,
        "proportionScale": 0,
        "referralData": {
            "acquisitionTime": "2021-11-22T23:29:51.656Z",
            "acquisitionReferrer": "string",
            "medium": "string",
            "source": "string",
            "campaign": "string",
            "adGroup": "string",
            "keyword": "string",
            "matchType": "string",
            "sendInfo": true,
            "requestSessionId": "string",
            "offerId": "string"
        },
        "agreementIds": [
            "string"
        ],
        "identityVerificationResultToken": "string",
        "captchaId": "string",
        "captchaToken": "string",
        "captchaProvider": "string"
    }
    DATA;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://auth.roblox.com/v1/signup');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
}

服务器告诉我“错误400,您的浏览器发送了无效的请求”。问题在于x-csrf-令牌,当我将其作为字符串“x token: j8acha7hffh”时,它可以工作。当我将其命名为“:$data”时,它会返回一个错误。我尝试过不同的方法来处理这个错误。我已经改变了标题。尝试了不同的请求方式。还是没有什么能纠正我的错误。我是新的PHP,请帮助!

EN

回答 2

Stack Overflow用户

发布于 2021-11-24 11:37:19

您正在使用$header作为一个array,但是它包含长string,因为我怀疑长字符串中的行分隔符/r/n在这里造成了麻烦。

因此,更改充满新行分隔符的一个值数组。

代码语言:javascript
复制
$headers = [
    "x-csrf-token: $data\r\n".
    "Content-Type: application/json\r\n".
    "Accept: application/json\r\n"
];

具有多个值的数组。

代码语言:javascript
复制
$headers = [
    "x-csrf-token: $data",
    "Content-Type: application/json",
    "Accept: application/json",
];

CURLOPT_HTTPHEADER格式数组中要设置的header字段数组(“内容-类型:文本/平原”、“内容-长度:100”)

关于https://www.php.net/manual/en/function.curl-setopt.php的更多细节

票数 1
EN

Stack Overflow用户

发布于 2021-11-24 03:32:42

尝试这样做:

“x-csrf-令牌:”.$data.“\r”。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70089788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档