尝试在浏览器上“保存页面”时会发送哪些HTTP标头?

时间:2015-04-12 12:16:37

标签: php curl web-scraping scrape

我使用CURL来检索页面。我注意到,当我只使用浏览器查看页面时,表单中的关键字段不会出现。尝试执行CURL请求时 - 该字段确实没有出现。但是,在保存页面然后执行"查看源代码"在本地,突然出现了隐藏的表单字段。

表单字段,例如:

<input type="hidden" name="frc" id="frc" value="123">

我想知道如何模拟&#34;保存页面&#34;请求由浏览器发送到服务器。点击&#34;保存页面&#34;?是否有任何特定的标题 是否有任何其他解释为什么某个字段会突然弹出&#34;本地&#34;?

1 个答案:

答案 0 :(得分:0)

并不总是使用HTTP标头...... 您可以使用它来使用php完成保存页面:

$data = file_get_contents("https://stackoverflow.com/questions/29589363/what-http-headers-are-sent-when-trying-to-save-page-on-a-browser");
echo htmlentities($data);

您可以稍后根据需要保存,例如将此页面保存为*.html作为*.php等。

请在this中阅读我的回答,您可以稍后为您的目的处理......

你可以使用fsockopen这样做:

$server  = 'somesite or somefile'; //"https://stackoverflow.com/questions/29589363/what-http-headers-are-sent-when-trying-to-save-page-on-a-browser" or "example.html"
$port    = 80;
$size    = 1024;   // Bytes will be read (and display). 0 for read all

$socket  = fsockopen( $server, $port, $errno, $errstr, $timeout=100 );
fputs( $socket, $request );
if ( $size > 0 ) {

    $tmp="";
    while (!feof($socket))
    {
        $buffer = fgets($socket,$size);
        $tmp .= $buffer;
        [another code you want]
    }               
    $fp = fopen("output.txt","w");  //or, whatever output extension you want...
    fwrite($fp,$tmp);
    fclose($fp);
}

fclose( $socket );

对于自我卷曲你可以使用它:

$source = "https://stackoverflow.com/questions/29589363/what-http-headers-are-sent-when-trying-to-save-page-on-a-browser";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
$data = curl_exec ($ch);
$error = curl_error($ch); 
curl_close ($ch);

$destination = "output.html";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);

希望这可以帮到你。