Using php to capture page content is very useful in actual development, such as being a simple content collector and extracting part of the content on the webpage, after you filter the captured content by using a regular expression, you can obtain the desired content. Below are several common methods to capture the content on the webpage using php.
1. file_get_contents
PHP code
$ Url = "http://www.phpzixue.cn "; $ 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
$ Url = "http://www.phpzixue.cn "; $ 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
$ Handle = fopen ("http://www.phpzixue.cn", "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.