7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Service
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Search.php 16971 2009-07-22 18:05:45Z mikaelkael $
24 * @see Zend_Http_Client
26 require_once 'Zend/Http/Client.php';
31 require_once 'Zend/Uri/Http.php';
36 require_once 'Zend/Json.php';
41 require_once 'Zend/Feed.php';
45 * @package Zend_Service
47 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
48 * @license http://framework.zend.com/license/new-bsd New BSD License
51 class Zend_Service_Twitter_Search
extends Zend_Http_Client
57 protected $_responseType = 'json';
60 * Response Format Types
63 protected $_responseTypes = array(
78 * @param string $returnType
81 public function __construct($responseType = 'json')
83 $this->setResponseType($responseType);
84 $this->_uri
= Zend_Uri_Http
::fromString("http://search.twitter.com");
86 $this->setHeaders('Accept-Charset', 'ISO-8859-1,utf-8');
92 * @param string $responseType
93 * @throws Zend_Service_Twitter_Exception
94 * @return Zend_Service_Twitter_Search
96 public function setResponseType($responseType = 'json')
98 if(!in_array($responseType, $this->_responseTypes
, TRUE)) {
99 require_once 'Zend/Service/Twitter/Exception.php';
100 throw new Zend_Service_Twitter_Exception('Invalid Response Type');
102 $this->_responseType
= $responseType;
107 * Retrieve responseType
111 public function getResponseType()
113 return $this->_responseType
;
117 * Get the current twitter trends. Currnetly only supports json as the return.
121 public function trends()
123 $this->_uri
->setPath('/trends.json');
124 $this->setUri($this->_uri
);
125 $response = $this->request();
127 return Zend_Json
::decode($response->getBody());
130 public function search($query, array $params = array())
133 $this->_uri
->setPath('/search.' . $this->_responseType
);
134 $this->_uri
->setQuery(null);
138 $_query['q'] = $query;
140 foreach($params as $key=>$param) {
145 $_query[$key] = $param;
148 $_query[$key] = (intval($param) > 100) ?
100 : intval($param);
151 $_query[$key] = intval($param);
154 $_query[$key] = 'true';
158 $this->_uri
->setQuery($_query);
160 $this->setUri($this->_uri
);
161 $response = $this->request();
163 switch($this->_responseType
) {
165 return Zend_Json
::decode($response->getBody());
168 return Zend_Feed
::importString($response->getBody());