Php obtains the summary of webpage content. 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 content captured below is filtered through the regular expression to get the content you want. As for how to use the regular expression to filter, I will not introduce it here. I am interested in it, the following are several common methods to capture webpage content using php.
1. file_get_contents
PHP code
The code is as follows:
$ 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
The code is as follows:
$ 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
The code is as follows:
$ 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.
...