Overview

Packages

  • Service
    • Twitter

Classes

  • Horde_Service_Twitter
  • Horde_Service_Twitter_Account
  • Horde_Service_Twitter_Auth
  • Horde_Service_Twitter_Auth_Oauth
  • Horde_Service_Twitter_Exception
  • Horde_Service_Twitter_Request
  • Horde_Service_Twitter_Request_Oauth
  • Horde_Service_Twitter_Statuses
  • Overview
  • Package
  • Class
  • Tree
 1: <?php
 2: /**
 3:  * Horde_Service_Twitter_Request_Oauth class wraps sending requests to Twitter's
 4:  * REST API using OAuth authentication.
 5:  *
 6:  * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
 7:  *
 8:  * @author Michael J. Rubinsky <mrubinsk@horde.org>
 9:  * @license  http://www.horde.org/licenses/bsd BSD
10:  * @category Horde
11:  * @package Service_Twitter
12:  */
13: class Horde_Service_Twitter_Request_Oauth extends Horde_Service_Twitter_Request
14: {
15:     /**
16:      * Perform a GET request with OAuth authorization.
17:      *
18:      * @param mixed (string | Horde_Url) $url  The url to request.
19:      * @param array  $params                   URL parameters.
20:      *
21:      * @return string  Call results.
22:      * @throws Horde_Service_Twitter_Exception
23:      */
24:     public function get($url, array $params = array())
25:     {
26:         $key = md5($url . 'get' . serialize($params) . serialize($this->_twitter->auth->getAccessToken($this->_request)));
27:         $cache = $this->_twitter->responseCache;
28:         if (!empty($cache) && $results = $cache->get($key, $this->_twitter->cacheLifetime)) {
29:             return $results;
30:         }
31:         $request = new Horde_Oauth_Request($url, $params, 'GET');
32:         $request->sign($this->_twitter->auth->oauth->signatureMethod,
33:                        $this->_twitter->auth->oauth,
34:                        $this->_twitter->auth->getAccessToken($this->_request));
35:         $url = ($url instanceof Horde_Url) ? $url : new Horde_Url($url);
36:         $url->add($params);
37:         try {
38:             $response = $this->_twitter->getHttpClient()->get($url->setRaw(true), array('Authorization' => $request->buildAuthorizationHeader('Twitter API')));
39:         } catch (Horde_Http_Exception $e) {
40:             throw new Horde_Service_Twitter_Exception($e);
41:         }
42: 
43:         // Looks like some of the http clients (like Fopen) will thrown an
44:         // exception if we try to read an empty stream. Ignore this.
45:         try {
46:             $body = $response->getBody();
47:             if ($response->code >= 400 && $response->code <= 500) {
48:                 throw new Horde_Service_Twitter_Exception($body);
49:             }
50:         } catch (Horde_Http_Exception $e) {}
51: 
52:         if (!empty($cache)) {
53:             $cache->set($key, $body);
54:         }
55: 
56:         return $body;
57:     }
58: 
59:     /**
60:      * Send a POST request to the twitter API. Purposely do not cache results
61:      * from these since POST requests alter data on the server.
62:      *
63:      * @see self::get
64:      */
65:     public function post($url, array $params = array())
66:     {
67:         $request = new Horde_Oauth_Request($url, $params);
68:         $request->sign($this->_twitter->auth->oauth->signatureMethod,
69:                        $this->_twitter->auth->oauth,
70:                        $this->_twitter->auth->getAccessToken($this->_request));
71:         $url = ($url instanceof Horde_Url) ? $url : new Horde_Url($url);
72:         try {
73:             $response = $this->_twitter->getHttpClient()->post($url->setRaw(true), $params, array('Authorization' => $request->buildAuthorizationHeader('Twitter API')));
74:         } catch (Horde_Http_Exception $e) {
75:             throw new Horde_Service_Twitter_Exception($e);
76:         }
77: 
78:         if ($response->code >= 400 && $response->code <= 500) {
79:             throw new Horde_Service_Twitter_Exception($response->getBody());
80:         }
81:         return $response->getBody();
82:     }
83: 
84: }
85: 
API documentation generated by ApiGen