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. 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!
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:X8EQ8TIrk5QJarishilton.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...
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...
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.
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):
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.
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
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.
please give me your website code i'm confusing find pdf code search thanks google feed for filetype ??? please
<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.
No problem! I can share more if interested... Let us know your implementation if ready. Best regards, R.