? & lt/span>。 $ URL = \"/\";& ltspan style=\"white-space:pre \" >? & lt/span>。 $fh= co" />

Traditional Culture Encyclopedia - Almanac inquiry - What methods does php have to obtain the source content of web pages?

What methods does php have to obtain the source content of web pages?

You can refer to the following methods:

Method 1: Get file_get_contents.

& ltspan style="white-space:pre " >? & lt/span>。 $ URL = "/";

& ltspan style="white-space:pre " >? & lt/span>。 $fh= content of file acquisition

('/news/fzfj/'); & ltspan style="white-space:pre " >? & lt/span>。 echo $ fh

Method 2: use fopen to obtain the source code of the web page.

& ltspan style="white-space:pre " >? & lt/span>。 $ URL = "/";

& ltspan style="white-space:pre " >? & lt/span>。 $handle = fopen ($url," Rb ");

& ltspan style="white-space:pre " >? & lt/span>。 $ contents =

& ltspan style="white-space:pre " >? & lt/span>。 And (! feof($handle)) {

& ltspan style="white-space:pre " >& lt/span>。 $ content. = fread($handle,8 192);

& ltspan style="white-space:pre " >? & lt/span>。 }

& ltspan style="white-space:pre " >? & lt/span>。 fclose($ handle);

& ltspan style="white-space:pre " >? & lt/span>。 Echo $ contents// outputs the obtained contents.

Method 3: Use CURL to get the source code of the web page.

$ URL = "/";

$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0Windows NT 6.0slcc 1; . Net clr 2.0.50727; . Net clr 3.0.04506; . Net clr 3.5.21022; . Net clr1.0.3705; . Net clr1.1.4322)';

$ curl = curl _ init(); ? //Create a new CURL resource.

curl_setopt($curl,CURLOPT_URL,$ URL); ? //Set the URL and corresponding options.

curl_setopt($curl,CURLOPT_HEADER,0); ? //0 means no output header, and 1 means output.

curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1); ? //Set whether to display header information. 1 Yes, 0 does not. //If successful, only the result will be returned, and nothing will be output automatically. If it fails, it returns FALSE.

curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);

curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);

curl_setopt($curl,CURLOPT_ENCODING,' '); ? //Set the encoding format. If blank, all encoding formats are supported.

//For the contents of the "Accept-Encoding:" section in the//header, the supported encoding formats are: "identity", "deflate" and "gzip". ?

curl_setopt($curl,CURLOPT_USERAGENT,$ user agent);

curl_setopt($curl,CURLOPT_FOLLOWLOCATION, 1);

//Set this option to a non-zero header (such as "Location:"), and the server will send it as part of the HTTP header (note that this is recursive, and PHP will send a header in the shape of "Location:").

$ data = curl _ exec($ curl);

echo $ data

//echo curl _ errno($ curl); //When 0 is returned, the program is executed successfully.

curl _ close($ curl); ? //Close cURL resources and release system resources.

PHP (mbth: PHP: Hypertext Preprocessor, Chinese name: Hypertext Preprocessor) is a universal open source scripting language. Grammar absorbs the characteristics of C language, Java and Perl, which is beneficial to learning and widely used, mainly in the field of Web development. The unique syntax of PHP is a mixture of C, Java, Perl and PHP's own syntax. It can execute dynamic web pages faster than CGI or Perl.

Compared with other programming languages, dynamic pages made by PHP embed programs into HTML (an application under standard generalized markup language) documents for execution, and the execution efficiency is much higher than CGI which completely generates HTML tags. PHP can also execute compiled code, which can realize encryption and optimize code operation, making the code run faster.

References:

PHP (Hypertext Preprocessor)-Baidu Encyclopedia