Search code examples
phpgdata-apigdatazend-gdata

(PHP) How to parse URLs in google search results?


How get google search results url?

(I use Zend_Gdata_Gbase for get search google results and not DomDocument/htmlsimpleparser because its looks to me that Zend_Gdata_Gbase done specially for parsing google results. if I wrong in my selection, please write.)

My function to get google search results for 'yahoo' or other query search string: (the function get a feed that should have search result for word 'yahoo', but when i use prin_t($feed) I don't see url for each result)

<?php    
function queryGoogleSearch($queryString='yahoo'){
            $service = new Zend_Gdata_Gbase();
            $query = $service->newSnippetQuery();
            $query->setBq('['.$queryString.']');
            $query->setOrderBy('modification_time');
            $query->setSortOrder('descending');
            $query->setMaxResults('4');
            $feed = $service->getGbaseSnippetFeed($query);
            return $feed; 
    }
    print_r(queryGoogleSearch());
?>

I get 4 first url results (when I search manually in google):

www.yahoo.com, mail.yahoo.com, search.yahoo.com, maps.yahoo.com

But I can't find them when I print $feed variable.

Please what should i change or add inqueryGoogleSearch() function? (Or other better code)

Thanks


Solution

  • Are you trying to search google.com. Looks like that class is for Google Base, not google.com search engine. http://base.google.com/support/bin/answer.py?hl=en&answer=59260

    You probably want this: http://code.google.com/apis/customsearch/v1/overview.html They recently just changed this. The old google search API has now deprecated as of Nov 1st. Custom search is the new API.

    Its pretty simple to use without Zend.

    http://code.google.com/apis/customsearch/v1/using_rest.html#WorkingResults

    There is a JSON decoder in PHP. http://php.net/manual/en/function.json-decode.php

    Hope that helps!