Example of using CURL to get page title in PHP, curltitle
Practical demonstration of obtaining page title content through PHP:
Sample code:
Copy code The code is as follows:
/*
Function: Get the
content on the URL page
Parameters: $_POST['url']
*/
//Set the longest execution seconds
ini_set ("expect.timeout", 30);
set_time_limit(30);
// Check URL
if(!isset($_POST['url']) || $_POST['url'] == ''){
echo "URL error";
exit;
}
/* Get URL page data */
//Initialize CURL
$ch = curl_init();
// Set URL
curl_setopt($ch, CURLOPT_URL, $_POST['url']);
// Let the information obtained by curl_exec() be returned in the form of a data stream instead of being output directly.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
// The time to wait before initiating a connection. If set to 0, there will be no waiting
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
//Set the maximum number of seconds for CURL execution
curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
// Try to get the file content
$store = curl_exec ($ch);
// Check whether the file is obtained correctly
if (curl_errno($ch)){
echo "Unable to obtain URL data";
//echo curl_error($ch);/*Display error message*/
exit;
}
// Close CURL
curl_close($ch);
// Parse the section of HTML
preg_match("/(.*)/smUi",$store, $htmlHeaders);
if(!count($htmlHeaders)){
echo "Unable to parse the section in the data";
exit;
}
// Get the encoding format set by meta in
if(preg_match("/]*http-equiv[^>]*charset=(.*)("|')/Ui",$htmlHeaders[1], $results)){
$charset = $results[1];
}else{
$charset = "None";
}
// Get the text in
if(preg_match("/(.*)/Ui",$htmlHeaders[1], $htmlTitles)){
If(!count($htmlTitles)){
echo "Unable to parse the content of ";
exit;
}
// Convert the text encoding format of to UTF-8
if($charset == "None"){
$title=$htmlTitles[1];
}else{
$title=iconv($charset, "UTF-8", $htmlTitles[1]);
}
echo $title;
}
http://www.bkjia.com/PHPjc/939415.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/939415.htmlTechArticleExample of using CURL to get page title in PHP, curltitle Practical demonstration of getting page title content through PHP: Sample code: Copy The code is as follows: php /* Function: Get the URL on the page...