我试过这样做,但没有用!
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://google.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('GET /search?q=kk HTTP/1.1
Host: www.google.de
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Proxy-Connection: Close
Cookie: PREF=ID=2bb051bfbf00e95b:U=c0bb6046a0ce0334:
Cache-Control: max-age=0
Connection: Close
'));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
此外,是否可以在不设置 URL 的情况下仅使用 header 发出整个请求?我是说没有这个?
curl_setopt($ch, CURLOPT_URL, "http://google.com/");
谢谢!
最佳答案
我让它工作了。
1) 将 header Host: www.google.de
更改为 Host: www.google.com
动机:Host
header 中指定的主机应与 URL 的主机完全匹配。
2) 使用“www.google.com”而不是“google.com”
动机:向 google.com 发出的搜索请求不会检索搜索结果。您将被告知访问 www.google.com。
3) 将完整的 URL 设置为 CURLOPT_URL,而不仅仅是主机名。例如。将 CURLOPT_URL 更改为 curl_setopt($ch, CURLOPT_URL, "http://www.google.com/search?q=kk");
动机:正确使用 cURL API。
4) 从 CURLOPT_HTTPHEADER 中删除 GET/search?q=kk HTTP/1.1
-- 它放错地方了。
动机:正确使用 cURL API。
5) 响应将被 gzip 或 deflate 压缩。要停止这种情况,请删除 Accept-Encoding: gzip, deflate
请求 header 。
动机:如果您告诉 Google 您能够收到压缩的响应,他们会向您发送一个。解压缩 HTTP 响应是一个您可能不想执行的额外步骤。如果响应是未压缩的文本形式,则可能更容易处理响应。
关于php - 如何将 Curl 与 HEADERS 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9539466/