如何通过PHP发送和接收标题 [英] How to send and receive headers via PHP
问题描述
我了解PHP的头文件
函数并认为我可以看看实际的请求头(例如使用Firebug)并向服务器发出相同的请求(包括欺骗User-Agent)。这是正确的吗?
另一个问题是如何获取标题回复?我想分析答复。
谢谢。
编辑:
b $ b @Tatu,这是我运行的代码:
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,http://www.google.com/);
curl_setopt($ ch,CURLOPT_HEADER,0);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_USERAGENT,'Mozilla / 5.0(X11; U; Linux x86_64; en-US)AppleWebKit / 540.0(KHTML,如Gecko)Ubuntu / 10.10 Chrome / 8.1.0.0 Safari / 540.0');
$ result = curl_exec($ ch);
curl_close($ ch);
header('Content-type:text / plain');
echo($ result);
您可能想看看 cURL ,它可以让你发出请求并设置和检查标题。 PHP的标题
只设置当前页面的标题,不能用它来欺骗你的用户代理–这些是由服务器设置的标题,因此没有这种意义。
具有自定义标头的cURL请求的基本结构可能如下所示:
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,http://www.example.com/);
curl_setopt($ ch,CURLOPT_HEADER,1);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_USERAGENT,Your user agent);
$ result = curl_exec($ ch);
curl_close($ ch);
$ result
的开头现在会包含从服务器收到的标题。
I'd like to study how headers are sent and received.
I know about PHP's header
function and think I can just look at an actual request header (e.g. using Firebug) and make identical requests to a server (including spoofing the User-Agent). Is this correct?
The other problem is how do I get the header responses back? I want to analyze the response.
Thanks.
EDIT:
@Tatu, here's the code I ran:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/540.0 (KHTML, like Gecko) Ubuntu/10.10 Chrome/8.1.0.0 Safari/540.0');
$result = curl_exec($ch);
curl_close($ch);
header('Content-type: text/plain');
echo($result);
You might want to take a look at cURL which will allow you to make requests and set and inspect headers. PHP's header
only sets headers for the current page, you cannot use that to spoof your user agent – these are headers set by the server and as such have no such significance.
The basic structure of a cURL request with custom headers might be something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Your user agent");
$result = curl_exec($ch);
curl_close($ch);
The beginning of $result
will now contain the headers received from the server.
这篇关于如何通过PHP发送和接收标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!