Php obtains the webpage title and description based on the website.
Namespace Xuyaoxiang; class Snoopy {private $ pattern_array = array ('title' => '/(\ S *. *) <\/title>/I ', 'description' => '/
/', 'Charset' =>'/charset = \"? ([\ W-] + )\"? /I ',); public $ url; public $ target_code = "UTF-8"; // target code: public $ user_agent = 'mozilla/5.0 (Windows NT 10.0; WOW64) appleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36 '; // simulate the browser header data function _ construct ($ url) {$ this-> url = $ url;} public function set_pattern ($ key, $ val) {if (isset ($ this-> pattern_array [$ key]) {return false;} $ this-> pattern_array [$ key] = $ val;} // only rules can be added. The original rule function get_peg cannot be modified. E_content ($ pattern_key) {if ($ pattern_key = '') {return false;} $ curl = curl_init (); // Set the URLcurl_setopt ($ curl, CURLOPT_URL, $ this-> url); // sets headercurl_setopt ($ curl, CURLOPT_HEADER, 0); // sets the cURL parameter, and requires that the result be saved to the string or output to the screen. Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ curl, CURLOPT_USERAGENT, $ this-> user_agent); // run cURL to request the webpage $ data = curl_exec ($ curl ); if ($ data = false) {return false;} preg_match ($ this-> pattern_array ['charset'], $ data, $ reg_charset ); // Get the webpage code $ page_charset = strtolower ($ reg_charset [1]); $ charset = $ this-> target_code | $ data = mb_convert_encoding ($ data, $ this-> target_code, $ page_charset); // transcode the preg_match ($ this-> pattern_array [$ pattern_key], $ data, $ content); // curl_close ($ curl) of the URL request; return trim ($ content [1]); }}$ snoopy = new snoopy (" http://www.qq.com "); $ Title = $ snoopy-> get_pege_content ('title'); print_r ($ title );