PageRenderTime 25ms CodeModel.GetById 18ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Service/Twitter/Search.php

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