Search.php 4.0 KB

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