PHP crawls pages in several ways. When developing network programs, we often need to capture non-local files. generally, php is used to simulate browser access and access the url address through http requests, then we can get the html source. when developing network programs, we often need to capture non-local files. generally, php is used to simulate browser access and access the url through http requests, then we can get html source code or xml data, and we cannot directly output the data. we often need to extract the content and then format it to display it in a more friendly way.
The following describes several methods and principles for capturing pages in php:
I. main methods for capturing pages in PHP:
1. file () function
2. file_get_contents () function
3. fopen ()-> fread ()-> fclose () mode
4. curl method
5. fsockopen () function socket mode
6. use plug-ins (such as: http://sourceforge.net/projects/snoopy)
II. main methods for parsing html or xml code in PHP:
1. file () function
?123456789
// Define the url$url='http://t.qq.com'; // The fiel function reads the content array.$lines_array=file($url); // Split the array into strings$lines_string=implode('',$lines_array); // Output Content. you can save it on your server.echo $lines_string;
2. file_get_contents () function
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.
?1234567 // Define the url$url='http://t.qq.com'; // The file_get_contents function reads data remotely.$lines_string=file_get_contents($url); // Output Content. you can save it on your server.echo htmlspecialchars($lines_string);
3. fopen ()-> fread ()-> fclose () mode
?12345678910111213141516171819 // Define the url$url='http://t.qq.com'; // Fopen is opened in binary format$handle=fopen($url,"rb");// Variable initialization$lines_string="";// Read data cyclicallydo{ $data=fread($handle,1024); if(strlen($data)==0) { break; } $lines_string.=$data; }while(true);// Close the fopen handle and release the resourcefclose($handle); // Output Content. you can save it on your server.echo $lines_string;
4. curl method
Use curl to enable curl. Method: modify php in windows. ini, remove the semicolon before extension = php_curl.dll, and copy ssleay32.dll and libeay32.dll to C: \ WINDOWS \ system32; install curl extension in Linux.
?123456789101112131415 // Create a new cURL resource$url='http://t.qq.com'; $ch=curl_init(); $timeout=5; // Set the URL and corresponding optionscurl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);// Capture the URL$lines_string=curl_exec($ch); // Disable cURL resources and release system resourcescurl_close($ch);// Output Content. you can save it on your server.echo $lines_string;
5. fsockopen () function socket mode
Whether the socket mode can be correctly executed depends on the server settings. you can use phpinfo to check which communication protocols are enabled on the server.
?1234567891011121314 $fp = fsockopen("t.qq.com", 80, $errno, $errstr, 30);if (!$fp) { echo "$errstr ($errno)
\n";} else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: t.qq.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp);}
6. snoopyplug-in. the latest version is snoopy-1.2.4.zip Last Update:. we recommend that you use it.
Snoopy, a popular collection plug-in on the Internet, is very powerful and easy to use. you can also set an agent in it to simulate browser information.
?123456789101112 // Introduce the snoopy class filerequire('Snoopy.class.php');// Initialize the snoopy class$snoopy = new Snoopy;$url = "http://t.qq.com";// Start to collect content$snoopy->fetch($url); // Save the collected content to $ lines_string$lines_string = $snoopy->results;// Output Content. you can save it on your server.echo $lines_string;
Note: Set the agent to Line 1 of the Snoopy. class. php file. search for "var $ agent" in this file (in quotation marks ). You can use PHP to obtain the browser content,
Use echo $ _ SERVER ['http _ USER_AGENT ']; to obtain the browser information, copy the echo content to the agent.
And then get the html source generation...