Services_Google
[ class tree: Services_Google ] [ index: Services_Google ] [ all elements ]

Source for file Google.php

Documentation is available at Google.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Services_Google                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2004 Jon Wood                                          |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jon Wood <jon@jellybob.co.uk>                               |
  17. // +----------------------------------------------------------------------+
  18. //
  19. /**
  20. * A PHP implementation of the Google Web API (http://www.google.com/apis/)
  21. *
  22. * To use this package you will need to register for a search key.
  23. *
  24. @author Jon Wood <jon@jellybob.co.uk>
  25. @package Services_Google
  26. @category Services
  27. @copyright Jon Wood, 2004
  28. */
  29. class Services_Google implements Iterator
  30. {
  31.     /**
  32.      * The key to use for queries.
  33.      * @var string 
  34.      * @access public
  35.      */
  36.     public $key;
  37.  
  38.     /**
  39.      * The object being used for queries.
  40.      * @var SoapClient 
  41.      * @access private
  42.      */
  43.     private $_soapClient;
  44.     
  45.     /**
  46.      * The last query to be made.
  47.      * @var string 
  48.      * @access private
  49.      */
  50.     private $_lastQuery "";
  51.  
  52.     /**
  53.      * The current index which has been reached.
  54.      * @var int 
  55.      * @access private
  56.      */
  57.     private $_index = 0;
  58.     
  59.     /**
  60.      * The last resultset retrieved.
  61.      * @var Object 
  62.      * @access private
  63.      */
  64.     private $_result = null;
  65.     
  66.     /**
  67.      * An array of options to be applied to queries.
  68.      * @var array 
  69.      * @access public
  70.      */
  71.     public $queryOptions = array("start" => 0,
  72.                                  "maxResults" => 10,
  73.                                  "limit" => false,
  74.                                  "filter" => true,
  75.                                  "restricts" => "",
  76.                                  "safeSearch" => true,
  77.                                  "language" => "");
  78.     
  79.     /**
  80.      * Constructor
  81.      *
  82.      * @param string $key The web services key provided by Google.
  83.      * @return null 
  84.      * @access public
  85.      */
  86.     public function __construct($key)
  87.     {
  88.         $this->key = $key;
  89.         $this->_soapClient = new SoapClient('http://api.google.com/GoogleSearch.wsdl');
  90.     }
  91.  
  92.     /**
  93.      * Setup up a search to be run.
  94.      *
  95.      * Once you've run this method, you need to call fetch() to get results.
  96.      * You can set search options with the queryOptions variable.
  97.      *
  98.      * @param string $query The query string to use.
  99.      * @return null 
  100.      * @see Services_Google::fetch()
  101.      * @see Services_Google::$queryOptions
  102.      * @access public
  103.      */
  104.     public function search($query)
  105.     {
  106.         $this->_lastQuery $query;
  107.         $this->_index $this->queryOptions["start"];
  108.         $this->_result = null;
  109.     }
  110.  
  111.     /**
  112.      * Fetch results from a search.
  113.      *
  114.      * This method will return a GoogleSearchResult object, or false if no
  115.      * results were found, or the limit has been reached.
  116.      *
  117.      * @return GoogleSearchResult|false
  118.      * @see http://api.google.com/GoogleSearch.wsdl
  119.      * @see Services_Google::search()
  120.      * @access public
  121.      */
  122.     public function fetch()
  123.     {
  124.         if (isset($this->queryOptions["limit"])) {
  125.             if ($this->queryOptions["limit"<= $this->_index{
  126.                 return false;
  127.             }
  128.         }
  129.         
  130.         if (is_null($this->_result)) {
  131.             $this->runQuery();
  132.         }
  133.  
  134.         if ($this->_index == $this->_result->endIndex - 1{
  135.             $this->runQuery();
  136.         }
  137.         
  138.         if (count($this->_result->resultElements)) {
  139.             $this->_index++;
  140.             return $this->_result->resultElements[$this->_index $this->_result->startIndex];
  141.         else {
  142.             return false;
  143.         }
  144.     }
  145.    
  146.     /**
  147.      * Returns the number of results for the current query.
  148.      *
  149.      * @return int 
  150.      * @access public
  151.      */
  152.     public function numResults()
  153.     {
  154.     if (is_null($this->_result&& !empty($this->_lastQuery)) {
  155.             $this->runQuery();
  156.     }
  157.  
  158.         return $this->_result->estimatedTotalResultsCount;
  159.     }
  160.     
  161.     /**
  162.      * Does a spell check using Google's spell checking engine.
  163.      *
  164.      * @param string $phrase The string to spell check.
  165.      * @return string A suggestion of how the phrase should be spelt.
  166.      * @access public
  167.      */
  168.     public function spellingSuggestion($phrase)
  169.     {
  170.         return $this->_soapClient->doSpellingSuggestion($this->key$phrase);
  171.     }
  172.  
  173.     /**
  174.      * Gets a cached page from Google's cache.
  175.      *
  176.      * @param string $url The page to get.
  177.      * @return string The cached page.
  178.      * @access public
  179.      */
  180.     public function getCachedPage($url)
  181.     {
  182.         $result $this->_soapClient->doGetCachedPage($this->key$url);
  183.         return base64_decode($result);
  184.     }
  185.  
  186.     /**
  187.      * Runs a query when neccesary
  188.      *
  189.      * @return null 
  190.      * @access private
  191.      */
  192.     private function runQuery()
  193.     {
  194.         $this->_result $this->_soapClient->doGoogleSearch($this->key,
  195.                                                      $this->_lastQuery,
  196.                                                      $this->_index,
  197.                                                      $this->queryOptions["maxResults"],
  198.                                                      $this->queryOptions["filter"],
  199.                                                      $this->queryOptions["restricts"],
  200.                                                      $this->queryOptions["safeSearch"],
  201.                                                      $this->queryOptions["language"],
  202.                                                      "",
  203.                                                      "");
  204.     }
  205.  
  206.     public function valid()
  207.     {
  208.         if (isset($this->queryOptions["limit"])) {
  209.             if ($this->queryOptions["limit"<= $this->_index{
  210.                 return false;
  211.             }
  212.         }
  213.  
  214.         if (is_null($this->_result)) {
  215.             $this->runQuery();
  216.         }
  217.  
  218.         if ($this->_index == $this->_result->endIndex - 1{
  219.             $this->runQuery();
  220.         }
  221.  
  222.         return (bool)count($this->_result->resultElements);
  223.     }
  224.     
  225.     public function current()
  226.     {
  227.         if ($this->_result->startIndex < 10{
  228.             return $this->_result->resultElements[$this->_index];
  229.         else {
  230.             return $this->_result->resultElements[$this->_index ($this->_result->startIndex - 1)];
  231.         }
  232.     }
  233.  
  234.     public function next()
  235.     {
  236.         $this->_index++;
  237.     }
  238.  
  239.     public function rewind()
  240.     {
  241.         $this->_index = 0;
  242.     }
  243.  
  244.     public function key()
  245.     {
  246.         return $this->_index;
  247.     }
  248. }
  249. ?>

Documentation generated on Mon, 11 Mar 2019 13:54:07 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.