PHP Classes

File: examples.php

Recommend this page to a friend!
  Classes of Jeremy Cook   Google Custom Search   examples.php   Download  
File: examples.php
Role: Example script
Content type: text/plain
Description: Examples of how to use GoogleCustomSearch
Class: Google Custom Search
Perform searches with the Google Site Search API
Author: By
Last change: Updated code examples to use new namespaced version of the code and new Exception class.
Date: 12 years ago
Size: 2,467 bytes


Class file image Download
use Search\Google,
try {
//A simple example, default parameters in constructor and no extra parameters set.
$search = new SiteSearch('CSE Key Here');
$search->query('some search text');
//The results property contains the search results, spellingSuggestions contains any spelling suggestions from Google.
//Example with results returned with HTML from Google left in and character encoding set to UTF-16. Result text will also be encoded in UTF-16
        //NOTE: When using a custom character encoding the search query must also be encoded using this character set.
        //The object defaults to character encoding in iso-8859-1 and assumes that queries passed to it are also encoded in this character set.
$search = new SiteSearch('CSE Key Here', 'utf-16', GoogleCustomSearch::HTML);
$search->query('some search text');
//Use the same object to perform an extra search, but with some different parameters
$search->charEncoding = 'iso-8859-1';
$search->processText = GoogleCustomSearch::ENTITIES_ENCODED; //Strips HTML but leaves HTML special characters entity encoded.
$search->query('another query');
//Example using an array to set extra, custom options in the query string used to send a request to Google.
        //See for a full list of query parameter options that may be passed in the request.
$opts = array(
'lr' => 'lang_fr', //Request search results only for French language pages.
'cr' => 'countryCA', //Request search results only for a particular country, in this case Canada
'num' => 7 //Limit the search to a maximum of 7 results
//Create an object using default search options
$search = new SiteSearch('CSE Key Here');
//Set the custom options for the search.
        //These can also be set by passing the $opts array as the final argument in the object constructor.
$search->opts = $opts;
$search->query('yet another query');
    catch (
SiteSearchException $e) {
//Handle any exceptions raised by the object here.
echo $e->getMessage();