Ask how to download the source code of a website with a cookie and save it to a file without any search results,
I would like to ask if I can access a website with a cookie, read the source code, process and save it to a file or output it out. I am very grateful to you for your advice.
I wrote this by myself, but only the original page effect can be output, and the source code is not visible... Adding a loop does not work ..
'Bar'); $ data = http_build_query ($ data); $ opts = array ('http' => array ('method' => 'GET ', 'header' => "Content-type: text/html ". "Content-Length :". strlen ($ data ). "\ r \ n ". "cookie: *** \ r \ n", 'content' => $ data); $ context = stream_context_create ($ opts); $ html = file_get_contents ($ url, '', $ context); // var_dump ($ html); $ lines = file ($ html ); // I guess this line may be wrong. // var_dump ($ lines); foreach ($ lines as $ key => $ value) {echo "{$ key }:". htmlspecialchars ($ value )."
\ N ";}?>
Reply to discussion (solution)
After you $ html = file_get_contents ($ url, '', $ context );
$ Isn't the file content in html?
Echo $ html;
Let's take a look.
Thanks to the reply from the moderator, I tried echo $ html. the whole page refreshed by the browser is not the source code I want.
Just like
$value){ echo "{$key}:".htmlspecialchars($value)."
\n";}?>
The output result is the source code. I do not know whether my description is clear.
Save $ html to a file by yourself, and open it in a text editor.
Save $ html to a file by yourself, and open it in a text editor.
The method you mentioned can be used just now, and then you can directly use file_get_contents with reference to others' instances on the Internet, but it is garbled...
'bar'); $data = http_build_query($data); $opts = array ( 'http' => array ( 'method' => 'GET', 'header'=> "Content-type: text/html" . "Content-Length: " . strlen($data) . "\r\n" . "cookie:**\r\n",'content' => $data ) ); $context = stream_context_create($opts); $html = file_get_contents($url,'',$context); //$source = htmlspecialchars($html);//$code = preg_match(pattern, subject)echo htmlspecialchars($html);//var_dump($html);//$lines = file($url);//$lines = file_get_contents($url);//var_dump($lines);//$files = fopen("text.txt","w");//fwrite($files,$lines);/*foreach($html as $key => $value){ echo "{$key}:".htmlspecialchars($value)."
\n";}*///fclose($lines);?>
Garbled characters are generated because your encoding is not uniform. use the header to set the character set.
Header ("content-type: text/html; charset = utf-8 ");