Use CURL in PHP to obtain the page title example, curltitle. In PHP, you can use CURL to obtain the page title. In practice, curltitle uses PHP to obtain the page title content. example code: Copy the code as follows: php * function: example of using CURL in PHP to obtain the page title, curltitle
A practical example of getting the page title content through PHP:
Sample code:
The code is as follows:
<? Php
/*
Function: obtainContent
Parameter: $ _ POST ['URL']
*/
// Set the maximum number of seconds for execution
Ini_set ("expect CT. timeout", 30 );
Set_time_limit (30 );
// Check the URL
If (! Isset ($ _ POST ['URL']) | $ _ POST ['URL'] = ''){
Echo "URL error ";
Exit;
}
/* Retrieve URL page data */
// Initialize CURL
$ Ch = curl_init ();
// Set the URL
Curl_setopt ($ ch, CURLOPT_URL, $ _ POST ['URL']);
// Let the information obtained by curl_exec () be returned in the form of data streams rather than directly output.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
// The waiting time before the connection is initiated. if it is set to 0, 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;
}
// Disable CURL
Curl_close ($ ch );
// Parse the HTMLSection
Preg_match ("/ (. *) <\/Head>/smUi ", $ store, $ htmlHeaders );
If (! Count ($ htmlHeaders )){
Echo "unable to parse dataSection ";
Exit;
}
// ObtainEncoding format set in meta
If (preg_match ("/ ] * Http-equiv [^>] * charset = (. *) (\ "| ')/Ui", $ htmlHeaders [1], $ results )){
$ Charset = $ results [1];
} Else {
$ Charset = "None ";
}
// Obtain Text in
If (preg_match ("/ (. *) <\/Title>/Ui ", $ htmlHeaders [1], $ htmlTitles )){
If (! Count ($ htmlTitles )){
Echo "cannot be parsed Content ";
Exit;
}
// Set Convert the text encoding format to UTF-8
If ($ charset = "None "){
$ Title = $ htmlTitles [1];
} Else {
$ Title = iconv ($ charset, "UTF-8", $ htmlTitles [1]);
}
Echo $ title;
}
Example code: Code: PHP/* function: Get URL on the page...