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. /**
  21.  * A PHP implementation of the Google Web API (http://www.google.com/apis/)
  22.  *
  23.  * To use this package you will need to register for a search key.
  24.  *
  25.  * @author      Jon Wood <jon@jellybob.co.uk>
  26.  * @package     Services_Google
  27.  * @category    Services
  28.  * @copyright   Jon Wood, 2004
  29.  */
  30. class Services_Google implements Iterator
  31. {
  32.     /**
  33.      * The key to use for queries.
  34.      *
  35.      * @var     string 
  36.      * @access  public
  37.      */
  38.     public $key;
  39.  
  40.     /**
  41.      * The object being used for queries.
  42.      *
  43.      * @var     SoapClient 
  44.      * @access  private
  45.      */
  46.     private $_soapClient;
  47.  
  48.     /**
  49.      * The last query to be made.
  50.      *
  51.      * @var     string 
  52.      * @access  private
  53.      */
  54.     private $_lastQuery "";
  55.  
  56.     /**
  57.      * The current index which has been reached.
  58.      *
  59.      * @var     int 
  60.      * @access  private
  61.      */
  62.     private $_index = 0;
  63.  
  64.     /**
  65.      * The last resultset retrieved.
  66.      *
  67.      * @var     Object 
  68.      * @access  private
  69.      */
  70.     private $_result = null;
  71.  
  72.     /**
  73.      * An array of options to be applied to queries.
  74.      *
  75.      * @var     array 
  76.      * @access  public
  77.      */
  78.     public $queryOptions = array(
  79.                                     "start"         => 0,
  80.                                     "maxResults"    => 10,
  81.                                     "limit"         => false,
  82.                                     "filter"        => true,
  83.                                     "restricts"     => "",
  84.                                     "safeSearch"    => true,
  85.                                     "language"      => ""
  86.                                 );
  87.  
  88.     /**
  89.      * Constructor
  90.      *
  91.      * @param   string  $key    The web services key provided by Google.
  92.      * @return  null 
  93.      * @access  public
  94.      */
  95.     public function __construct($key)
  96.     {
  97.         $this->key = $key;
  98.         $this->_soapClient = new SoapClient('http://api.google.com/GoogleSearch.wsdl');
  99.     }
  100.  
  101.     /**
  102.      * Set Query options
  103.      *
  104.      * @param   array   $options   An array of options
  105.      * @access  public
  106.      */
  107.     public function setOptions($options)
  108.     {
  109.         if (is_array($options)) {
  110.             foreach($options as $key => $value{
  111.                 if (isset($this->queryOptions[$key])) {
  112.                     $this->queryOptions[$key$value;
  113.                 }
  114.             }
  115.         }
  116.     }
  117.  
  118.     /**
  119.      * Setup up a search to be run.
  120.      *
  121.      * Once you've run this method, you need to call fetch() to get results.
  122.      * You can set search options with the queryOptions variable.
  123.      *
  124.      * @param   string  $query  The query string to use.
  125.      * @return  null 
  126.      * @see     Services_Google::fetch()
  127.      * @see     Services_Google::$queryOptions
  128.      * @access  public
  129.      */
  130.     public function search($query)
  131.     {
  132.         $this->_lastQuery $query;
  133.         $this->_index $this->queryOptions["start"];
  134.         $this->_result = null;
  135.     }
  136.  
  137.     /**
  138.      * Fetch results from a search.
  139.      *
  140.      * This method will return a GoogleSearchResult object, or false if no
  141.      * results were found, or the limit has been reached.
  142.      *
  143.      * @return  GoogleSearchResult|false
  144.      * @see     http://api.google.com/GoogleSearch.wsdl
  145.      * @see     Services_Google::search()
  146.      * @access  public
  147.      */
  148.     public function fetch()
  149.     {
  150.         if (isset($this->queryOptions["limit"])) {
  151.             if ($this->queryOptions["limit"<= $this->_index{
  152.                 return false;
  153.             }
  154.         }
  155.  
  156.         if (is_null($this->_result)) {
  157.             $this->runQuery();
  158.         }
  159.  
  160.         if ($this->_index == $this->_result->endIndex - 1{
  161.             $this->runQuery();
  162.         }
  163.  
  164.         if (count($this->_result->resultElements)) {
  165.             $this->_index++;
  166.             return $this->_result->resultElements[$this->_index $this->_result->startIndex];
  167.         else {
  168.             return false;
  169.         }
  170.     }
  171.  
  172.     /**
  173.      * Returns the number of results for the current query.
  174.      *
  175.      * @return  int 
  176.      * @access  public
  177.      */
  178.     public function getResultsCount()
  179.     {
  180.         if (is_null($this->_result&& !empty($this->_lastQuery)) {
  181.             $this->runQuery();
  182.         }
  183.  
  184.         return $this->_result->estimatedTotalResultsCount;
  185.     }
  186.  
  187.     /**
  188.      * Returns search time
  189.      *
  190.      * @return  float 
  191.      * @access  public
  192.      */
  193.     public function getSearchTime()
  194.     {
  195.         if (is_null($this->_result&& !empty($this->_lastQuery)) {
  196.             $this->runQuery();
  197.         }
  198.  
  199.         return $this->_result->searchTime;
  200.     }
  201.  
  202.     /**
  203.      * Does a spell check using Google's spell checking engine.
  204.      *
  205.      * @param   string $phrase The string to spell check.
  206.      * @return  string A suggestion of how the phrase should be spelt.
  207.      * @access  public
  208.      */
  209.     public function spellingSuggestion($phrase)
  210.     {
  211.         return $this->_soapClient->doSpellingSuggestion($this->key$phrase);
  212.     }
  213.  
  214.     /**
  215.      * Gets a cached page from Google's cache.
  216.      *
  217.      * @param   string  $url    The page to get.
  218.      * @return  string  The cached page.
  219.      * @access  public
  220.      */
  221.     public function getCachedPage($url)
  222.     {
  223.         $result $this->_soapClient->doGetCachedPage($this->key$url);
  224.         return base64_decode($result);
  225.     }
  226.  
  227.     /**
  228.      * Runs a query when neccesary
  229.      *
  230.      * @return  null 
  231.      * @access  private
  232.      */
  233.     private function runQuery()
  234.     {
  235.         $this->_result $this->_soapClient->doGoogleSearch(
  236.                                             $this->key,
  237.                                             $this->_lastQuery,
  238.                                             $this->_index,
  239.                                             $this->queryOptions["maxResults"],
  240.                                             $this->queryOptions["filter"],
  241.                                             $this->queryOptions["restricts"],
  242.                                             $this->queryOptions["safeSearch"],
  243.                                             $this->queryOptions["language"],
  244.                                             "",
  245.                                             ""
  246.                                         );
  247.     }
  248.  
  249.     public function valid()
  250.     {
  251.         if (isset($this->queryOptions["limit"])) {
  252.             if ($this->queryOptions["limit"<= $this->_index{
  253.                 return false;
  254.             }
  255.         }
  256.  
  257.         if (is_null($this->_result)) {
  258.             $this->runQuery();
  259.         }
  260.  
  261.         if ($this->_index == $this->_result->endIndex - 1{
  262.             $this->runQuery();
  263.         }
  264.  
  265.         if ($this->_index >= $this->_result->estimatedTotalResultsCount - 1{
  266.             return false;
  267.         }
  268.  
  269.         return (bool)count($this->_result->resultElements);
  270.     }
  271.  
  272.     public function current()
  273.     {
  274.         if ($this->_result->startIndex < 10{
  275.             return $this->_result->resultElements[$this->_index];
  276.         else {
  277.             return $this->_result->resultElements[$this->_index ($this->_result->startIndex - 1)];
  278.         }
  279.     }
  280.  
  281.     public function next()
  282.     {
  283.         $this->_index++;
  284.     }
  285.  
  286.     public function rewind()
  287.     {
  288.         $this->_index = 0;
  289.     }
  290.  
  291.     public function key()
  292.     {
  293.         return $this->_index;
  294.     }
  295. }
  296. ?>

Documentation generated on Mon, 11 Mar 2019 15:17:56 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.