Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7068511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:19:58+00:00 2026-05-28T05:19:58+00:00

I am using the following code for parsing dom document but at the end

  • 0

I am using the following code for parsing dom document but at the end I get the error
“google.ac” is null or not an object
line 402
char 1

What I guess, line 402 contains tag and a lot of “;”,
How can I fix this?

<?php

//$ch = curl_init("http://images.google.com/images?q=books&tbm=isch/");


// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://images.google.com/images?q=books&tbm=isch/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
$data = curl_exec($ch);

curl_close($ch); 

$dom = new DOMDocument();
       $dom->loadHTML($data);
    //@$dom->saveHTMLFile('newfolder/abc.html')

     $dom->loadHTML('$data');

    // find all ul

    $list = $dom->getElementsByTagName('ul'); 
    // get few  list items 

    $rows = $list->item(30)->getElementsByTagName('li'); 
    // get anchors from the table   

    $links = $list->item(30)->getElementsByTagName('a'); 

    foreach ($links as $link) { 
        echo "<fieldset>"; 
        $links = $link->getElementsByAttribute('imgurl');

    $dom->saveXML($links);
                }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:19:59+00:00Added an answer on May 28, 2026 at 5:19 am

    There are a few issues with the code:

    1. You should add the CURL option – CURLOPT_RETURNTRANSFER – in order to capture the output. By default the output is displayed on the browser. Like this: curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);. In the code above, $data will always be TRUE or FALSE (http://www.php.net/manual/en/function.curl-exec.php)

    2. $dom->loadHTML('$data'); is not correct and not required

    3. The method of reading ‘li’ and ‘a’ tags might not be correct because $list->item(30) will always point to the 30th element

    Anyways, coming to the fixes. I’m not sure if you checked the HTML returned by the CURL request but it seems different from what we discussed in the original post. In other words, the HTML returned by CURL does not contain the required <ul> and <li> elements. It instead contains <td> and <a> elements.

    Add-on: I’m not very sure why do HTML for the same page is different when it is seen from the browser and when read from PHP. But here is a reasoning that I think might fit. The page uses JavaScript code that renders some HTML code dynamically on page load. This dynamic HTML can be seen when viewed from the browser but not from PHP. Hence, I assume the <ul> and <li> tags are dynamically generated. Anyways, that isn’t of our concern for now.

    Therefore, you should modify your code to parse the <a> elements and then read the image URLs. This code snippet might help:

    <?php
    $ch = curl_init(); // create a new cURL resource
    
    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "http://images.google.com/images?q=books&tbm=isch/");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
    $data = curl_exec($ch); // grab URL and pass it to the browser
    curl_close($ch); 
    
    $dom = new DOMDocument();
    @$dom->loadHTML($data); // avoid warnings
    
    $listA = $dom->getElementsByTagName('a'); // read all <a> elements
    foreach ($listA as $itemA) { // loop through each <a> element
        if ($itemA->hasAttribute('href')) { // check if it has an 'href' attribute
            $href = $itemA->getAttribute('href'); // read the value of 'href'
            if (preg_match('/^\/imgres\?/', $href)) { // check that 'href' should begin with "/imgres?"
                $qryString = substr($href, strpos($href, '?') + 1);
                parse_str($qryString, $arrHref); // read the query parameters from 'href' URI
                echo '<br>' . $arrHref['imgurl'] . '<br>';
            }
        }
    }
    

    I hope above makes sense. But please note that the above parsing might fail if Google modifies their HTML.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I`m parsing a Google Maps RSS with javascript and using the following code to
I am using following code in rowdatabound function. Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object,
I'm using following code but cannot return data from MySQL. This is the output:
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something
I am using the following code to build object for posting via ajax: eval('var
I'm parsing a XML using jQuery with the following code: function appendNav(xml) { $(xml).find(Nav).each(function()
I am using the following code for parsing of small xml files and it
In the following code, I am trying to do a text parsing by using
i'm using the following code: - (void)parserDidStartDocument:(NSXMLParser *)parser{ NSLog(@File found and parsing started); }
I am using following code for other JSON data its working but here it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.