Ask how to download the source code to a file with a cookie

Source: Internet
Author: User
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 ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.