请求的 header 名称无效。 HTTP 错误 400。
The request has an invalid header name. HTTP Error 400.
这是我的 curl 请求(在一些旧的 Whosebug 答案中找到它):
$headers[] = "Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0";
$headers[] = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$headers[] = "Accept-Language:en-us,en;q=0.5";
$headers[] = "Accept-Encoding:gzip,deflate";
$headers[] = "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$headers[] = "Keep-Alive:115";
$headers[] = "Connection:keep-alive";
$headers[] = "Cache-Control:max-age=0";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_ENCODING, "gzip");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($curl);
curl_close($curl);
return $data;
我收到这个错误:
Bad Request - Invalid Header
HTTP Error 400. The request has an invalid header name.
你知道我为什么会收到这个错误吗?我应该如何解决它?
我认为你应该使用 CURLOPT_USERAGENT
作为 User-Agent
(我推荐的方式)
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0');
或(没玩过)
$headers[] = "User-Agent:Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0";
而不是
$headers[] = "Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0";
要考虑的另一点是空格。
User-Agent:Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0
用错了
"...Agent : Mozilla.."
这会发给你
Bad Request - Invalid Header
HTTP Error 400. The request has an invalid header name.
这是我的 curl 请求(在一些旧的 Whosebug 答案中找到它):
$headers[] = "Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0";
$headers[] = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$headers[] = "Accept-Language:en-us,en;q=0.5";
$headers[] = "Accept-Encoding:gzip,deflate";
$headers[] = "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$headers[] = "Keep-Alive:115";
$headers[] = "Connection:keep-alive";
$headers[] = "Cache-Control:max-age=0";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_ENCODING, "gzip");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($curl);
curl_close($curl);
return $data;
我收到这个错误:
Bad Request - Invalid Header
HTTP Error 400. The request has an invalid header name.
你知道我为什么会收到这个错误吗?我应该如何解决它?
我认为你应该使用 CURLOPT_USERAGENT
作为 User-Agent
(我推荐的方式)
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0');
或(没玩过)
$headers[] = "User-Agent:Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0";
而不是
$headers[] = "Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0";
要考虑的另一点是空格。
User-Agent:Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0
用错了
"...Agent : Mozilla.."
这会发给你
Bad Request - Invalid Header
HTTP Error 400. The request has an invalid header name.