Jump to content

Grab Page Data, Meta Tags And Display Them


phpsane
 Share

Recommended Posts

Folks,

 

I found this piece of code on my notes. Cannot remember where I grabbed it from.

It is supposed to fetch a page and grab it's meta details and display them on screen.

Question is: What is this error I am getting ?

 

Fatal error: Uncaught TypeError: DOMDocument::loadHTML() expects parameter 1 to be string, boolean given in C:\xampp\htdocs\curl\filter.php:31 Stack trace: #0 C:\xampp\htdocs\curl\filter.php(31): DOMDocument->loadHTML(false) #1 {main} thrown in C:\xampp\htdocs\curl\filter.php on line 31

 



<?php
 
/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
 
function file_get_contents_curl($url)
{
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 
    $data = curl_exec($ch);
    curl_close($ch);
 
    return $data;
}
 
$html = file_get_contents_curl("https://www.google.com/search?q=dirty+harry&oq=dirty+harry&aqs=chrome..69i57.2604j0j7&sourceid=chrome&ie=UTF-8");
 
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
 
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
 
$metas = $doc->getElementsByTagName('meta');
 
for ($i = 0; $i < $metas->length; $i++)
{
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
    if($meta->getAttribute('name') == 'keywords')
        $keywords = $meta->getAttribute('content');
}
 
echo "Title: $title". '<br/><br/>';
echo "Description: $description". '<br/><br/>';
echo "Keywords: $keywords";
 
?> 
 
<br>
 
 


 

This is line 31 in my NotePad++:

@$doc->loadHTML($html);

Edited by phpsane
Link to comment
Share on other sites

The error is telling you that $html didn't get a result that you expect.  It got a false - meaning that your curl call is flawed, or the address is not valid anymore.

 

The google url is valid. It's just curl seems to have trouble fetching google pages.

Anyway, changed the url to:

http://php.net/manual/en/function.file-get-contents.php

 

And now I get this as output:

 

Title: PHP: file_get_contents - Manual 

 

 

Notice: Undefined variable: description in C:\xampp\htdocs\curl\filter.php on line 50

Description: 

 

 

Notice: Undefined variable: keywords in C:\xampp\htdocs\curl\filter.php on line 51

Keywords: 

 

 

Notice from my op that those variables have been defined. And so, I don't understand why the error.

Edited by phpsane
Link to comment
Share on other sites

It's having problems with these:

 

 

echo "Description: $description". '<br/><br/>';
echo "Keywords: $keywords";

 

But they have been defined earlier. Like so:

 

 

for ($i = 0; $i < $metas->length; $i++)
{
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
    if($meta->getAttribute('name') == 'keywords')
        $keywords = $meta->getAttribute('content');
}
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.