The captured content is filtered by using a regular expression to get the content you want. As for how to use a regular expression to filter the content, I will not introduce it here. If you are interested, the following are several common methods to capture webpage content using php.
1. file_get_contents
PHP code
Copy codeThe Code is as follows:
<? Php
$ Url = "http://www.jb51.net ";
$ Contents = file_get_contents ($ url );
// Use the following code if Chinese characters are garbled
// $ Getcontent = iconv ("gb2312", "UTF-8", $ contents );
Echo $ contents;
?>
2. curl
PHP code
Copy codeThe Code is as follows:
<? Php
$ Url = "http://www.jb51.net ";
$ Ch = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
// Add the following two lines to the webpage for User Detection:
// Curl_setopt ($ ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY );
// Curl_setopt ($ ch, CURLOPT_USERPWD, US_NAME. ":". US_PWD );
$ Contents = curl_exec ($ ch );
Curl_close ($ ch );
Echo $ contents;
?>
3. fopen-> fread-> fclose
PHP code
Copy codeThe Code is as follows:
<? Php
$ Handle = fopen ("http://www.jb51.net", "rb ");
$ Contents = "";
Do {
$ Data = fread ($ handle, 1024 );
If (strlen ($ data) = 0 ){
Break;
}
$ Contents. = $ data;
} While (true );
Fclose ($ handle );
Echo $ contents;
?>
Note:
1. Use file_get_contents and fopen to enable allow_url_fopen. Method: Edit php. ini and set allow_url_fopen = On. When allow_url_fopen is disabled, neither fopen nor file_get_contents can open remote files.
2. Use curl to enable curl. Method: Modify php. ini in windows, remove the semicolon before extension = php_curl.dll, and copy ssleay32.dll and libeay32.dll to C: \ WINDOWS \ system32. Install curl extension in Linux.