2
0

Search.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Twitter
  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$
  21. */
  22. /**
  23. * @see Zend_Http_Client
  24. */
  25. require_once 'Zend/Http/Client.php';
  26. /**
  27. * @see Zend_Uri_Http
  28. */
  29. require_once 'Zend/Uri/Http.php';
  30. /**
  31. * @see Zend_Json
  32. */
  33. require_once 'Zend/Json.php';
  34. /**
  35. * @see Zend_Feed
  36. */
  37. require_once 'Zend/Feed.php';
  38. /**
  39. * @category Zend
  40. * @package Zend_Service
  41. * @subpackage Twitter
  42. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. */
  45. class Zend_Service_Twitter_Search extends Zend_Http_Client
  46. {
  47. /**
  48. * Return Type
  49. * @var String
  50. */
  51. protected $_responseType = 'json';
  52. /**
  53. * Response Format Types
  54. * @var array
  55. */
  56. protected $_responseTypes = array(
  57. 'atom',
  58. 'json'
  59. );
  60. /**
  61. * Uri Compoent
  62. *
  63. * @var Zend_Uri_Http
  64. */
  65. protected $_uri;
  66. /**
  67. * Constructor
  68. *
  69. * @param string $returnType
  70. * @return void
  71. */
  72. public function __construct($responseType = 'json')
  73. {
  74. $this->setResponseType($responseType);
  75. $this->_uri = Zend_Uri_Http::fromString("http://search.twitter.com");
  76. $this->setHeaders('Accept-Charset', 'ISO-8859-1,utf-8');
  77. }
  78. /**
  79. * set responseType
  80. *
  81. * @param string $responseType
  82. * @throws Zend_Service_Twitter_Exception
  83. * @return Zend_Service_Twitter_Search
  84. */
  85. public function setResponseType($responseType = 'json')
  86. {
  87. if(!in_array($responseType, $this->_responseTypes, TRUE)) {
  88. require_once 'Zend/Service/Twitter/Exception.php';
  89. throw new Zend_Service_Twitter_Exception('Invalid Response Type');
  90. }
  91. $this->_responseType = $responseType;
  92. return $this;
  93. }
  94. /**
  95. * Retrieve responseType
  96. *
  97. * @return string
  98. */
  99. public function getResponseType()
  100. {
  101. return $this->_responseType;
  102. }
  103. /**
  104. * Get the current twitter trends. Currnetly only supports json as the return.
  105. *
  106. * @throws Zend_Http_Client_Exception
  107. * @return array
  108. */
  109. public function trends()
  110. {
  111. $this->_uri->setPath('/trends.json');
  112. $this->setUri($this->_uri);
  113. $response = $this->request();
  114. return Zend_Json::decode($response->getBody());
  115. }
  116. /**
  117. * Performs a Twitter search query.
  118. *
  119. * @throws Zend_Http_Client_Exception
  120. */
  121. public function search($query, array $params = array())
  122. {
  123. $this->_uri->setPath('/search.' . $this->_responseType);
  124. $this->_uri->setQuery(null);
  125. $_query = array();
  126. $_query['q'] = $query;
  127. foreach($params as $key=>$param) {
  128. switch($key) {
  129. case 'geocode':
  130. case 'lang':
  131. case 'since_id':
  132. $_query[$key] = $param;
  133. break;
  134. case 'rpp':
  135. $_query[$key] = (intval($param) > 100) ? 100 : intval($param);
  136. break;
  137. case 'page':
  138. $_query[$key] = intval($param);
  139. break;
  140. case 'show_user':
  141. $_query[$key] = 'true';
  142. }
  143. }
  144. $this->_uri->setQuery($_query);
  145. $this->setUri($this->_uri);
  146. $response = $this->request();
  147. switch($this->_responseType) {
  148. case 'json':
  149. return Zend_Json::decode($response->getBody());
  150. break;
  151. case 'atom':
  152. return Zend_Feed::importString($response->getBody());
  153. break;
  154. }
  155. return ;
  156. }
  157. }