Home >Backend Development >PHP Tutorial >Example of using CURL to get page title in PHP, curltitle_PHP tutorial

Example of using CURL to get page title in PHP, curltitle_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:09:59918browse

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

http://www.bkjia.com/PHPjc/939415.html

truehttp: //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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn