Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial Commit

  • Loading branch information...
commit f5c0eaa55e4155ddb50e81cce98145d9af17aff9 0 parents
Dan Horrigan dhrrgn authored

Showing 2 changed files with 184 additions and 0 deletions. Show diff stats Hide diff stats

  1. +46 0 README.textile
  2. +138 0 libraries/Google.php
46 README.textile
Source Rendered
... ... @@ -0,0 +1,46 @@
  1 +h1. CodeIgniter Google API Library
  2 +
  3 +* Author: "Dan Horrigan":http://dhorrigan.com/
  4 +
  5 +h2. DESCRIPTION
  6 +
  7 +This allows you to easily interface with Google's AJAX API within your CodeIgniter application.
  8 +
  9 +h2. INSTALLATION
  10 +
  11 +1. Copy Google.php to your application/libraries folder.
  12 +2. Load the library like normal. (@$this->load->library('google');@)
  13 +
  14 +h2. USAGE
  15 +
  16 +API calls takes 2 parameters:
  17 +1. The search string
  18 +2. An array of options
  19 +
  20 +Valid API calls include:
  21 +<pre><code>$this->google->web();
  22 +$this->google->local();
  23 +$this->google->video();
  24 +$this->google->blogs();
  25 +$this->google->news();
  26 +$this->google->books();
  27 +$this->google->images();
  28 +$this->google->patents();</code></pre>
  29 +
  30 +Examples:
  31 +@$this->google->web('codeigniter');@
  32 +
  33 +@$this->google->images('cougar', array('safe' => 'active'));@
  34 +
  35 +h3. Return Values
  36 +
  37 +The library return the responseData object sent by Google. You must access the results like this:
  38 +
  39 +<pre><code>$search = $this->google->web('codeigniter');
  40 +$results = $search->results;</code></pre>
  41 +
  42 +You can also use chaining to simplify it:
  43 +<pre><code>$results = $this->google->web('codeigniter')->results;</code></pre>
  44 +
  45 +For more information on the return object please see the API reference:
  46 +"Google API Reference":http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje
138 libraries/Google.php
... ... @@ -0,0 +1,138 @@
  1 +<?php defined('BASEPATH') or exit('No direct script access allowed');
  2 +/**
  3 + * CodeIgniter Google AJAX API Library
  4 + *
  5 + * A CodeIgniter library that interacts with the Google AJAX API.
  6 + *
  7 + * @package CodeIgniter
  8 + * @subpackage Google
  9 + * @author Dan Horrigan <http://dhorrigan.com>
  10 + * @license Apache License v2.0
  11 + * @copyright 2010 Dan Horrigan
  12 + *
  13 + * Licensed under the Apache License, Version 2.0 (the "License");
  14 + * you may not use this file except in compliance with the License.
  15 + * You may obtain a copy of the License at
  16 + *
  17 + * http://www.apache.org/licenses/LICENSE-2.0
  18 + *
  19 + * Unless required by applicable law or agreed to in writing, software
  20 + * distributed under the License is distributed on an "AS IS" BASIS,
  21 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22 + * See the License for the specific language governing permissions and
  23 + * limitations under the License.
  24 + */
  25 +
  26 +/**
  27 + * Google class
  28 + *
  29 + * @subpackage Google
  30 + */
  31 +class Google
  32 +{
  33 + /**
  34 + * The global CI object
  35 + */
  36 + private $_ci;
  37 +
  38 + /**
  39 + * The base API url
  40 + */
  41 + private $_api_base = 'http://ajax.googleapis.com/ajax/services/search/';
  42 +
  43 + /**
  44 + * The API version to use
  45 + */
  46 + private $_api_version = '1.0';
  47 +
  48 + /**
  49 + * Valid API calls
  50 + */
  51 + private $_valid_calls = array('web','local','video','blogs','news','books','images','patent');
  52 +
  53 + /**
  54 + * The amount of results to return.
  55 + * NOTE: Google limits you to 8 per request.
  56 + */
  57 + public $result_size = 8;
  58 +
  59 + /**
  60 + * Contruct
  61 + *
  62 + * Gets the global CI object
  63 + *
  64 + * @access public
  65 + * @return void
  66 + */
  67 + public function __construct()
  68 + {
  69 + $this->_ci =& get_instance();
  70 + }
  71 +
  72 + /**
  73 + * __Call overload
  74 + *
  75 + * Makes sure the function name is a valid API call, then makes the call.
  76 + * API calls takes 2 parameters:
  77 + * 1 - The search string
  78 + * 2 - An array of options
  79 + *
  80 + * Examples:
  81 + * $this->google->web('codeigniter');
  82 + * $this->google->images('cougar', array('safe' => 'active'));
  83 + *
  84 + * @access public
  85 + * @param string $name
  86 + * @param array $args
  87 + * @return object The response data object
  88 + */
  89 + public function __call($name, $args)
  90 + {
  91 + // Check if the function name is a valid API call.
  92 + if(!in_array($name, $this->_valid_calls))
  93 + {
  94 + show_error(sprintf('"%s" is not a valid Google API function.', $name));
  95 + }
  96 +
  97 + return $this->_api_call($name, $args[0], isset($args[1]) ? $args[1] : array());
  98 + }
  99 +
  100 + /**
  101 + * API Call
  102 + *
  103 + * Makes the API call to Google.
  104 + *
  105 + * @access private
  106 + * @param string $type
  107 + * @param string $q
  108 + * @return object The response data object
  109 + */
  110 + private function _api_call($type, $q, $params = array())
  111 + {
  112 + $url = $this->_api_base . $type . '?v=' . $this->_api_version;
  113 + $params['q'] = $q;
  114 + (!isset($params['rsz'])) AND $params['rsz'] = $this->result_size;
  115 +
  116 + foreach($params as $name => $val)
  117 + {
  118 + $url .= '&' . $name . '=' . rawurlencode($val);
  119 + }
  120 +
  121 + $ch = curl_init();
  122 + curl_setopt($ch, CURLOPT_URL, $url);
  123 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  124 + curl_setopt($ch, CURLOPT_REFERER, $this->_ci->config->site_url($this->_ci->uri->uri_string()));
  125 + $response = curl_exec($ch);
  126 + curl_close($ch);
  127 +
  128 + $response = json_decode($response);
  129 +
  130 + if($response->responseStatus != 200)
  131 + {
  132 + show_error(sprintf('Google API Error (%s): %s', $response->responseStatus, $response->responseDetails));
  133 + }
  134 + return $response->responseData;
  135 + }
  136 +}
  137 +
  138 +/* End of file Google.php */

0 comments on commit f5c0eaa

Please sign in to comment.
Something went wrong with that request. Please try again.