phpsane Posted September 3, 2017 Share Posted September 3, 2017 (edited) 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 September 3, 2017 by phpsane Link to comment https://forums.phpfreaks.com/topic/304853-grab-page-data-meta-tags-and-display-them/ Share on other sites More sharing options...
Barand Posted September 3, 2017 Share Posted September 3, 2017 the obvious next step is to var_dump($html) Link to comment https://forums.phpfreaks.com/topic/304853-grab-page-data-meta-tags-and-display-them/#findComment-1550665 Share on other sites More sharing options...
ginerjm Posted September 3, 2017 Share Posted September 3, 2017 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. Link to comment https://forums.phpfreaks.com/topic/304853-grab-page-data-meta-tags-and-display-them/#findComment-1550667 Share on other sites More sharing options...
phpsane Posted September 3, 2017 Author Share Posted September 3, 2017 (edited) 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 September 3, 2017 by phpsane Link to comment https://forums.phpfreaks.com/topic/304853-grab-page-data-meta-tags-and-display-them/#findComment-1550670 Share on other sites More sharing options...
ginerjm Posted September 3, 2017 Share Posted September 3, 2017 Show us lines 50 & 51? Link to comment https://forums.phpfreaks.com/topic/304853-grab-page-data-meta-tags-and-display-them/#findComment-1550672 Share on other sites More sharing options...
Barand Posted September 4, 2017 Share Posted September 4, 2017 Those variable are defined only if the conditions are true, otherwise they are not defined, Link to comment https://forums.phpfreaks.com/topic/304853-grab-page-data-meta-tags-and-display-them/#findComment-1550687 Share on other sites More sharing options...
phpsane Posted September 5, 2017 Author Share Posted September 5, 2017 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 https://forums.phpfreaks.com/topic/304853-grab-page-data-meta-tags-and-display-them/#findComment-1550737 Share on other sites More sharing options...
Barand Posted September 5, 2017 Share Posted September 5, 2017 (edited) Those variable are defined only if the conditions are true, otherwise they are not defined, You had this answer already, yet still you keep posting. Is this a deliberate attempt to waste our time? Closing the thread before more is wasted. Edited September 5, 2017 by Barand Link to comment https://forums.phpfreaks.com/topic/304853-grab-page-data-meta-tags-and-display-them/#findComment-1550738 Share on other sites More sharing options...
Recommended Posts