Use curl in PHP to get the page title example, curltitle_php tutorial

Source: Internet
Author: User

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 ...

  • 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.