Search code examples
phpxmljsonrestgoogle-search-api

Google Custom Search - Query database/API?


We're interested about using Google Custom Search / Google in our project, mostly due to the fact that it's amazing at conjugation & correcting misspelled words.

We know that it can return data in JSON or XML, and we're fine with that. But finding an answer to question:

Can we use that conjugation and mistake correction and search our own database/api?

If you would type drnks with no alcohol it would automatically correct to drinks with no alcohol, and then search our database like this:

http://example.com?search=drinks&alcohol=0, and it could respond like this:

{
    "coke": {
        "alcohol": 0,
        "calories": 300,
        "taste": "awesome"
    },
    "pepsi": {
        "alcohol": 0,
        "calories": 300,
        "taste": "meh"
    }
}

And then it would return these two results, in some form.

Solutions using the paid version are fine.

If it's possible to do this, could you provide me with a simple example?


Solution

  • Google provides a REST API for their custom search, you can query it from your server to determine whether there is a better spelling for the search terms or not, and then use that to query your internal database.

    In my code I'm using Guzzle, a REST client library to avoid suffering with cURL's ugly and verbose code, but feel free to use cURL if you really need to.

    // Composer's autoloader to load the REST client library
    require "vendor/autoload.php";
    
    $api_key = "..."; // Google API key, looks like random text
    $search_engine = "..."; // search engine ID, looks like "<numbers>:<text>"
    $query = "drnks with no alcohol"; // the original search query
    
    // REST client object with some defaults
    // avoids specifying them each time we make a request
    $client = new GuzzleHttp\Client(["base_url" => "https://www.googleapis.com", "defaults" => ["query" => ["key" => $api_key, "cx" => $search_engine, "fields" => "spelling(correctedQuery)"]]]);
    
    try {
        // the actual request, with the search query
        $resp = $client->get("/customsearch/v1", ["query" => ["q" => $query]])->json();
    
        // whether Google suggests an alternative spelling
        if (isset($resp["spelling"]["correctedQuery"])) {
            $correctedQuery = $resp["spelling"]["correctedQuery"];
            // now use that corrected spelling to query your internal DB
            // or do anything else really, the query is yours now
            echo $correctedQuery;
        } else {
            // Google doesn't have any corrections, use the original query then
            echo "No corrections found";
        }
    } catch (GuzzleHttp\Exception\TransferException $e) {
        // Something bad happened, log the exception but act as if
        // nothing is wrong and process the user's original query
        echo "Something bad happened";
    }
    

    Here are some instructions to obtain your API key, and the custom search engine ID can be obtained from the control panel.

    If you look carefully you can see I've specified the fields query parameter to request a partial response with only the eventual spelling suggestions, to (hopefully) get better performance as we don't need anything else from the response (but feel free to change/remove it if you do need the complete response).

    Note that Google has no clue about what's in your database so the spelling corrections will only be based on the public data Google has about your website, I don't think there is a way to make Google know about your internal DB, not that it's a good idea anyway.

    Finally, make sure to handle rate-limits and API failures gracefully by still giving the user the possibility to search using their original query (just act like nothing wrong happened, and only log the error for later review).