Sometimes we need to get the title and content of the webpage, which is a collection function. here we will share it to help you with your needs.
The code is as follows:
Function getPageContent ($ url ){
// $ Url = 'http: // www.ttphp.com;
$ Pageinfo = array ();
$ Pageinfo [content_type] = '';
$ Pageinfo [charset] = '';
$ Pageinfo [title] = '';
$ Pageinfo [description] = '';
$ Pageinfo [keywords] = '';
$ Pageinfo [body] = '';
$ Pageinfo ['httpcodec'] = 200;
$ Pageinfo ['all'] = '';
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0 )");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, 0 );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, 0 );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 8 );
Curl_setopt ($ ch, CURLOPT_FILETIME, 1 );
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
// Curl_setopt ($ ch, CURLOPT_HEADER, 1 );
Curl_setopt ($ ch, CURLOPT_URL, $ url );
$ Curl_start = microtime (true );
$ Store = curl_exec ($ ch );
$ Curl_time = microtime (true)-$ curl_start;
If (curl_error ($ ch )){
$ Pageinfo ['httpcode'] = 505; // gate way error
Echo 'curl error: '. curl_error ($ ch). "/n ";
Return $ pageinfo;
}
// Print_r (curl_getinfo ($ ch ));
$ Pageinfo ['httpcode'] = curl_getinfo ($ ch, CURLINFO_HTTP_CODE );
// Echo curl_getinfo ($ ch, CURLINFO_CONTENT_TYPE). "/n ";
$ Pageinfo [content_type] = curl_getinfo ($ ch, CURLINFO_CONTENT_TYPE );
If (intval ($ pageinfo ['httpcode']) <> 200 or! Preg_match ('@ text/html @', curl_getinfo ($ ch, CURLINFO_CONTENT_TYPE ))){
// Print_r (curl_getinfo ($ ch ));
// Exit;
Return $ pageinfo;
}
Preg_match ('/charset = ([^/s/n/r] +)/I', curl_getinfo ($ ch, CURLINFO_CONTENT_TYPE), $ matches ); // Retrieve charset from the header
If (trim ($ matches [1]) {
$ Pageinfo [charset] = trim ($ matches [1]);
}
// Echo $ pageinfo [charset];
// Exit;
Curl_close ($ ch );
// Echo $ store;
// Remove javascript
$ Store = preg_replace ("/
$ Store = preg_replace ("/ /SmUi ",'', $ store );
// Remove