1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Google API using PHP

Discussion in 'Google API' started by shmekerosu, Jan 4, 2010.

  1. #1
    I am trying to get used with Google API (for web search) and I found some PHP example in Google code page.

    It is like:

    --------------------------------------------------------------------------------
    $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton";

    // sendRequest
    // note how referer is set manually
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, /* Enter the URL of your site here */);
    $body = curl_exec($ch);
    curl_close($ch);

    // now, process the JSON string
    $json = json_decode($body);
    // now have some fun with the results...
    --------------------------------------------------------------------------------

    Now I read about how to use this and only found out that results are in 4/8 sets in JSON format (which I discovered on official PHP page).

    I have to questions, no need to get into details as I will look up myself but I just need some guidance from people who did this already. :confused:

    1. How I can interpret the results and put in a form that I can include in HTML.
    2. How I can select next results and standard query is only returning 4/8 initial set.

    Thanks and wait your comments,
    Best to all for 2010!
     
    shmekerosu, Jan 4, 2010 IP
  2. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #2
    is anyone here who used Google API for search with PHP with no API Key ?

    I made some more research on JSON decode function and it seams here is the problem.
    It is displayin in plain like:


    {"responseData": {"results":[{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://en.wikipedia.org/wiki/Paris_Hilton","url":"http://en.wikipedia.org/wiki/Paris_Hilton","visibleUrl":"en.wikipedia.org","cacheUrl":"http://www.google.com/search?q\u003dcache:TwrPfhd22hYJ:en.wikipedia.org","title":"\u003cb\u003eParis Hilton\u003c/b\u003e - Wikipedia, the free encyclopedia","titleNoFormatting":"Paris Hilton - Wikipedia, the free encyclopedia","content":"\u003cb\u003eParis\u003c/b\u003e Whitney \u003cb\u003eHilton\u003c/b\u003e (born February 17, 1981) is an American socialite, heiress, media personality, model, singer, author, fashion designer and actress. \u003cb\u003e...\u003c/b\u003e"},{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://parishilton.com/","url":"http://parishilton.com/","visibleUrl":"parishilton.com","cacheUrl":"http://www.google.com/search?q\u003dcache:X8EQ8TIrk5QJ:parishilton.com","title":"\u003cb\u003eParis Hilton\u003c/b\u003e | The Official Website","titleNoFormatting":"Paris Hilton | The Official Website","content":"\u003cb\u003eParisHilton\u003c/b\u003e.com \u003cb\u003eParis Hilton\u003c/b\u003e, Nicky Hilton Fashion, Pictures, Apparel, Jewellery , Film, and Fun."},{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://www.parishiltonzone.com/","url":"http://www.parishiltonzone.com/","visibleUrl":"www.parishiltonzone.com","cacheUrl":"http://www.google.com/search?q\u003dcache:EXq0MGRNiNoJ:www.parishiltonzone.com","title":"\u003cb\u003eParis Hilton\u003c/b\u003e Zone | \u003cb\u003eParis Hilton\u003c/b\u003e Pictures, Pics, Photos","titleNoFormatting":"Paris Hilton Zone | Paris Hilton Pictures, Pics, Photos","content":"8000+ new \u003cb\u003eParis Hilton\u003c/b\u003e pictures, Paris wallpaper, lyrics, audio, video, daily Paris pics \u0026amp; news."},{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://www.imdb.com/name/nm0385296/","url":"http://www.imdb.com/name/nm0385296/","visibleUrl":"www.imdb.com","cacheUrl":"http://www.google.com/search?q\u003dcache:1i34KkqnsooJ:www.imdb.com","title":"\u003cb\u003eParis Hilton\u003c/b\u003e","titleNoFormatting":"Paris Hilton","content":"Socialite \u003cb\u003eParis Hilton\u003c/b\u003e was born on February 17, 1981 in New York City... Visit IMDb for Photos, Filmography, Discussions, Bio, News, Awards, Agent, \u003cb\u003e...\u003c/b\u003e"}],"cursor":{"pages":[{"start":"0","label":1},{"start":"4","label":2},{"start":"8","label":3},{"start":"12","label":4},{"start":"16","label":5},{"start":"20","label":6},{"start":"24","label":7},{"start":"28","label":8}],"estimatedResultCount":"9420000","currentPageIndex":0,"moreResultsUrl":"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dParis+Hilton"}}, "responseDetails": null, "responseStatus": 200}


    How can I interpret this into results with proper formating in HTML ?

    Just a hint, I need a starting point...
     
    shmekerosu, Jan 4, 2010 IP
  3. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Really, no one used google api for search from php ??
    I will apreciate any tips on this...
    Thanks!
     
    shmekerosu, Jan 5, 2010 IP
  4. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    szalinski, Jan 23, 2010 IP
  5. hopebd

    hopebd Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    :( no idea , let me google :)
     
    hopebd, Jan 24, 2010 IP
  6. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #6
    Thanks szalinski! I managed it in a slightly other way and it is working ok.

    Regards!
     
    shmekerosu, Jan 25, 2010 IP
  7. QiSoftware

    QiSoftware Well-Known Member

    Messages:
    805
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    158
    #7
    are you more familiar with using XML data? if so, request the output as XML rather than json. json is meant to be used with existing ajax or js function calls. xml is raw data in the xml format.

    Q...
     
    QiSoftware, Jan 27, 2010 IP
  8. illianafrazer

    illianafrazer Well-Known Member

    Messages:
    393
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    138
    #8
    Buddy its too technical. sorry couldnt help you

    i will be watching this thread :)
     
    illianafrazer, Jan 27, 2010 IP
  9. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #9
    Thanks you so much to all.

    I managed it through JSON and output to HTML. If you want I can post code here no problem.

    What I did not managed till now is how I can process next results pages, just initial one ? Any sugestions ?

    Regards,
    R.
     
    shmekerosu, Jan 27, 2010 IP
  10. maryjoanna

    maryjoanna Peon

    Messages:
    287
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    can you post it...i am also interested
     
    maryjoanna, Feb 5, 2010 IP
  11. shab

    shab Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #11
    here ya go.. it decodes your JSON outputs it in html. dont forget to say thanks... :)


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <?php 
    function retrieveGoogleSearch($searchTerms="PHP",$searchURL="site:www.Wercshop.com") {
        $googleBaseUrl = "http://ajax.googleapis.com/ajax/services/search/web";
        $googleBaseQuery = "?v=1.0&q=";
        $googleFullUrl = $googleBaseUrl . $googleBaseQuery . $searchURL . "%20" . $searchTerms; 
        $curlObject = curl_init();
        curl_setopt($curlObject,CURLOPT_URL,$googleFullUrl);
        curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($curlObject,CURLOPT_HEADER,false);
        curl_setopt($curlObject,CURLOPT_REFERER,"http://www.b13ed.com/");
        $returnGoogleSearch = curl_exec($curlObject);
        curl_close($curlObject);
        $returnGoogleSearch = json_decode($returnGoogleSearch,true);
        return $returnGoogleSearch["responseData"]["results"];
    }
    $setSearchTerms = "JavaScript";
    $setSearchURL = "site:www.MercuryState.com";
    $searchResultsArray = retrieveGoogleSearch($setSearchTerms,$setSearchURL);
    ?>
    </head>
    
    <body>
    <ol>
    <? foreach($searchResultsArray as $result): ?>
    <li><strong><a href="<?=$result["url"];?>"><?=$result["title"];?></a></strong> <?=$result["content"];?> <em><?=$result["visibleUrl"];?></em></li>
    <? endforeach; ?>
    </ol>
    </body>
    </html>
    Code (markup):
     
    shab, Feb 18, 2010 IP
  12. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #12
    Thanks Shab!
    Good addition to our sample codes in this post.

    On the other hand I still have 2 problems: I have '%25' in links and it is not decoded to space...
    and second how I can retrieve second results sets (pages).

    My code is similar to yours so not big differences. (sample html page with search button can be found at:
    vreaugps.ro/gs)

    Thanks and good luck,
    R.
     
    shmekerosu, Feb 22, 2010 IP
  13. shab

    shab Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #13
    your links seems ok. use url_decode or url_encode function if youre having problems with URLS.

    as for the next set of results, I see you're getting 16 results per page. you could add "&start=17" in your query parameter so you can retrieve the next set of results.

    Here is the direct link for the REST API parameters you require:
    http://code.google.com/apis/ajaxsearch/documentation/reference.html#_restUrlBase
     
    shab, Feb 22, 2010 IP
  14. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #14
    Implementation can be found at http://pdfsearcher.com

    my problem now is to retrieve result summary cursor and display 'next' pages results and number pages 1, 2, 3, 4, ....next> sets!
    Any idea how to fix this ?

    R.
     
    shmekerosu, May 13, 2010 IP
  15. avlinkads

    avlinkads Banned

    Messages:
    121
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    33
    #15
    please give me your website code

    i'm confusing find pdf code search

    thanks


    google feed for filetype ??? please
     
    avlinkads, May 14, 2010 IP
  16. elizasilvy

    elizasilvy Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    nice info of course
     
    elizasilvy, May 17, 2010 IP
  17. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #17
    <avlinkads>
    I just manipulate query string in order to include filetype I want.

    I still try to parse and present pages cursor but I don't succeed, any hints!?

    R.
     
    shmekerosu, May 20, 2010 IP
  18. me_Sam

    me_Sam Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    it is really a big help.
    Thank you so much for your effort.
     
    me_Sam, May 27, 2010 IP
  19. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #19
    No problem! I can share more if interested...

    Let us know your implementation if ready.

    Best regards,
    R.
     
    shmekerosu, May 28, 2010 IP
  20. premeet01245

    premeet01245 Guest

    Messages:
    87
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    as u search about it just like search it.
     
    premeet01245, Jun 1, 2010 IP