views:

179

answers:

1

Hi All,

My apologies in advance, this question may not be the clearest, but I will try my best to articulate.

In my PHP script I'm processing a string. I want to grab whatever is in between brackets [ ] and execute a google local search (see http://code.google.com/apis/ajax/playground/#show_driving_directions) on the string. Once the search is processed I want to only grab the first result.

Just as a background, I have the PHP script executing anytime a "message" comes in.

I have all the regex stuff working, just need to know what the flow would be to call the google search api and return the first result.

+2  A: 

I would suggest calling the AJAX API directly, and parsing the result from JSON into an object (json_decode()) and taking the first result.

See the API docs at the following parts,

http://code.google.com/apis/ajaxsearch/documentation/reference.html#_fonje_urlbase http://code.google.com/apis/ajaxsearch/documentation/reference.html#_fonje_args http://code.google.com/apis/ajaxsearch/documentation/reference.html#_fonje_local

I've personally been working on some classes that wrap around the AJAX API, but they're no where near finished (probably going to refactor them for better structure).

You can however grab the current source from my SVN if you want. I've done the Local search and it works in its current form.

http://codelib.stephenmelrose.co.uk/svn/php/branches/1.0/library/

You'd use it like,

require_once("library/google/search/Local.php");

$google_search = new SM_Google_Search_Local('starbucks');
$google_search->setHTTPReferer("http://localhost.localhost/");
$google_search->setResultSetSize(Google_Search::RESULT_SET_SIZE_LARGE);
$google_search->setCenterPoint('53.79548,-1.549416');

$response = $google_search->getResponse();
$results = $response->getResults();

// Your first result
$results[0];
Stephen Melrose
why would i need to set httpreferer?
st4ck0v3rfl0w
also where is $response coming from?
st4ck0v3rfl0w
Told you it wasn't finished, haven't done the docs yet! :) The response is the response from the API. You get more than results. You get paging, etc, too. You then get the results from the response. As for the HTTP_REFERER, you need to provide one as per Google's API instructions.
Stephen Melrose