How to get the web page title tag content in PHP

1 Answer

0 votes
function get_html($url) {
    $handle = curl_init();
           
    curl_setopt($handle, CURLOPT_HTTPGET, true);
    curl_setopt($handle, CURLOPT_HEADER, true);
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
           
    $output = curl_exec($handle);
   
    curl_close($handle);
           
    $separator = "\r\n\r\n";
    $header = substr($output, 0, strpos($output, $separator));
      
    $body_start = strlen($header) + strlen($separator);
    $html = substr($output, $body_start, strlen($output) - $body_start);
       
    return $html;
}
   
   
$url = "https://www.webshopy.com"; 
   
$html = get_html($url);

$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);

$titleTags = $doc->getElementsByTagName('title');
$title = $titleTags[0]->nodeValue;
   
echo $title;
   
   
/*
run:
   
WebShopy - Search! before you buy to find deals, coupons, sales & discounts
   
*/

 



answered Sep 13, 2019 by avibootz

Related questions

1 answer 188 views
2 answers 246 views
1 answer 202 views
1 answer 177 views
1 answer 178 views
178 views asked Sep 13, 2019 by avibootz
...