Example of using CURL in PHP to obtain the page title

Source: Internet
Author: User
This article mainly introduces the example of using CURL in PHP to obtain the page title. This article uses regular expressions to obtain the page title, page encoding, and content in the head tag, for more information about how to obtain the title of a page through PHP, see:

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.