Use curl in PHP to get the page title example, Curltitle
Get the actual demo of the page title through PHP:
Sample code:
Copy CodeThe code is as follows:
<?php
/*
Function: Gets the URL on the page<title>Content</title>
Parameter: $_post[' URL ']
*/
Set the maximum number of seconds to execute
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, rather than directly output.
curl_setopt ($ch, Curlopt_returntransfer, 1);
The time to wait before initiating the connection, and if set to 0, do not wait for
curl_setopt ($ch, curlopt_connecttimeout, 0);
Sets the maximum number of seconds that CURL executes
curl_setopt ($ch, Curlopt_timeout, 30);
Try to get the contents of the file
$store = curl_exec ($ch);
Check if the file is correctly obtained
if (Curl_errno ($ch)) {
echo "Unable to get URL data";
echo Curl_error ($ch);/* Display error message */
Exit
}
Turn off CURL
Curl_close ($ch);
Parsing the HTMLSection
Preg_match ("/ (. *) <\/head>/smui ", $store, $htmlHeaders);
if (!count ($htmlHeaders)) {
echo "Cannot parse segments in the data";
Exit
}
Gets the encoding format for the meta setting in
if (Preg_match ("/ ]*http-equiv[^>]*charset= (. *) (. *) (\" | ") /ui ", $htmlHeaders [1], $results)) {
$charset = $results [1];
}else{
$charset = "None";
}
Made The text in
if (Preg_match ("/ (. *) <\/title>/ui ", $htmlHeaders [1], $htmlTitles)) {
if (!count ($htmlTitles)) {
echo "Cannot parse The content ";
Exit
}
Will Text encoding format 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.com true http://www.bkjia.com/PHPjc/939415.html techarticle PHP using Curl to get the page title example, Curltitle through PHP to get the page title content of the actual demo: Sample code: Copy code code as follows: PHP/* Function: Get the URL on the page ...