使用 PHP CURL 时出现错误 400 错误请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24569281/
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-25 17:22:39  来源:igfitidea点击:

Error 400 Bad request when I use PHP CURL

phpcurl

提问by Sadikhasan

When I CURLin PHP. With appropriate headers and fields but It will give me error Error 400 Bad request.Why this error becomes?

当我CURL在 PHP 中。使用适当的标题和字段,但它会给我错误Error 400 Bad request。为什么会出现这个错误?

HTTP/1.0 400 Bad request Cache-Control: no-cache Connection: close Content-Type: text/html
400 Bad request
Your browser sent an invalid request. 1

Header Request

标头请求

$header[] = "Accept:application/json, text/javascript, */*; q=0.01";
$header[]="Accept-Encoding:gzip, deflate";
$header[] = "Cache-Control:max-age=0";
$header[]= "Accept-Language:en-US,en;q=0.5";
$header[] = "Content-Length:37";
$header[]="Content-Type:application/x-www-form-urlencoded; charset=UTF-8";
$header[]="Cookie:__qca=P0-116849880-1387336057175; __utma=140029553.335591273.1387336057.1389609300.1389617402.102; __utmz=140029553.1389617402.102.89.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); _ga=GA1.2.335591273.1387336057; sgt=id=3380ce36-a139-4845-bd20-5bb3fd4174ec; usr=t=qrthm51g2UyV&s=QtuIYj84zEOR";
$header[]="X-Requested-With:XMLHttpRequest";

CURL Code

卷曲代码

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_POST, $fields);
curl_setopt($ch, CURLOPT_, $value);
$response=curl_exec($ch);
curl_close($ch);

If you want any further data please comment I will explain in detail.

如果您需要任何进一步的数据,请发表评论,我将详细解释。

回答by Stephen C

A 400 response means that the HTTP server that the request went to has said that the request is invalid.

400 响应意味着请求所到达的 HTTP 服务器已表示请求无效。

Why?

为什么?

Who knows! Maybe the URL you sent in the request has malformed arguments (in the query string) or maybe it requires arguments that you didn't supply. Or maybe the problem is the other headers.

谁知道!也许您在请求中发送的 URL 有格式错误的参数(在查询字符串中),或者它可能需要您未提供的参数。或者也许问题出在其他标题上。

Or maybe it just doesn't like you.

或者它只是不喜欢你。

I suggest that you compare the request you are sending with either the web site's API documentation or a request sent by a (real) browser. And if that fails, ask the folks who look after the web server you are sending the requests to.

我建议您将发送的请求与网站的 API 文档或(真实的)浏览器发送的请求进行比较。如果失败,请询问负责您向其发送请求的 Web 服务器的人员。

回答by user3756907

This HTTP error usually occurs when the web server cannot understand part of a request that it received. Internet Explorer may report this as “The webpage cannot be found”.Microsoft Windows as HTTP 400 errors Two things that you can do are: 1: a:Clear your DNS cache by : b:opening a Command Prompt:Type ipconfig /flushdns c:Restart your web browser. 2:Try checking for an archived copy of the object, e.g. by entering the address in the Internet Archive Wayback Machine.

当 Web 服务器无法理解它收到的请求的一部分时,通常会发生此 HTTP 错误。Internet Explorer 可能将此报告为“找不到网页”。Microsoft Windows 作为 HTTP 400 错误您可以做的两件事是:1:a:通过以下方式清除 DNS 缓存:b:打开命令提示符:键入 ipconfig /flushdns c :重新启动您的网络浏览器。2:尝试检查对象的存档副本,例如通过在 Internet Archive Wayback Machine 中输入地址。