Overview

Packages

  • None
  • Service
    • Facebook

Classes

  • Horde_Service_Facebook_Auth
  • Horde_Service_Facebook_Base
  • Horde_Service_Facebook_BatchRequest
  • Horde_Service_Facebook_ErrorCodes
  • Horde_Service_Facebook_Events
  • Horde_Service_Facebook_Exception
  • Horde_Service_Facebook_Fql
  • Horde_Service_Facebook_Friends
  • Horde_Service_Facebook_Groups
  • Horde_Service_Facebook_Links
  • Horde_Service_Facebook_Notes
  • Horde_Service_Facebook_Notifications
  • Horde_Service_Facebook_Photos
  • Horde_Service_Facebook_Request
  • Horde_Service_Facebook_Streams
  • Horde_Service_Facebook_Translation
  • Horde_Service_Facebook_UploadRequest
  • Horde_Service_Facebook_Users
  • Horde_Service_Facebook_Videos
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Horde_Service_Facebook_Request:: encapsulate a request to the Facebook API.
  4:  *
  5:  * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * @author Michael J. Rubinsky <mrubinsk@horde.org>
  8:  * @category Horde
  9:  * @package Service_Facebook
 10:  */
 11: class Horde_Service_Facebook_Request
 12: {
 13:     /**
 14:      *
 15:      * @var Horde_Service_Facebook
 16:      */
 17:     protected $_facebook;
 18: 
 19:     /**
 20:      *
 21:      * @var Horde_Http_Client
 22:      */
 23:     protected $_http;
 24: 
 25:     /**
 26:      * The current method being processed.
 27:      *
 28:      * @var string
 29:      */
 30:     protected $_method;
 31: 
 32:     /**
 33:      * The method parameters for the current method call.
 34:      *
 35:      * @var array
 36:      */
 37:     protected $_params;
 38: 
 39:     /**
 40:      * Const'r
 41:      *
 42:      * @param Horde_Service_Facebook $facebook
 43:      * @param string                 $method
 44:      * @param array                  $params
 45:      *
 46:      */
 47:     public function __construct($facebook, $method, array $params = array())
 48:     {
 49:         $this->_facebook = $facebook;
 50:         $this->_http = $facebook->http;
 51:         $this->_method = $method;
 52:         $this->_params = $params;
 53:     }
 54: 
 55:     /**
 56:      * Run this request and return the data.
 57:      *
 58:      * @return mixed Either raw JSON, or an array of decoded values.
 59:      * @throws Horde_Service_Facebook_Exception
 60:      */
 61:     public function &run()
 62:     {
 63:         $data = $this->_postRequest($this->_method, $this->_params);
 64:         switch ($this->_facebook->dataFormat) {
 65:         case Horde_Service_Facebook::DATA_FORMAT_JSON:
 66:             return $data;
 67:         case Horde_Service_Facebook::DATA_FORMAT_ARRAY:
 68:             if (defined('JSON_BIGINT_AS_STRING')) {
 69:                 $result = json_decode($data, true, constant('JSON_BIGINT_AS_STRING'));
 70:             } else {
 71:                 if (is_numeric($data)) {
 72:                     $result = $data;
 73:                 } else {
 74:                     $result = json_decode($data, true);
 75:                 }
 76:             }
 77:         }
 78:         if (is_array($result) && isset($result['error_code'])) {
 79:             throw new Horde_Service_Facebook_Exception($result['error_msg'], $result['error_code']);
 80:         }
 81: 
 82:         return $result;
 83:     }
 84: 
 85:     /**
 86:      * Send a POST request
 87:      *
 88:      * @param string $method  The method to call.
 89:      * @param array  $params  The method parameters.
 90:      *
 91:      * @return string The request results
 92:      * @throws Horde_Service_Facebook_Exception
 93:      */
 94:     protected function _postRequest($method, &$params)
 95:     {
 96:         $this->_finalizeParams($params);
 97:         try {
 98:             $url = new Horde_Url(Horde_Service_Facebook::REST_SERVER_ADDR . $method);
 99:             $result = $this->_http->request('POST', $url->toString(), $this->_createPostString($params));
100:         } catch (Exception $e) {
101:             $this->_facebook->logger->err($e->getMessage());
102:             throw new Horde_Service_Facebook_Exception(Horde_Service_Facebook_Translation::t("Facebook service is unavailable. Please try again later."));
103:         }
104: 
105:         return $result->getBody();
106:     }
107: 
108:     /**
109:      * Finalize, sanity check, standardze and sign the method parameters, $params
110:      *
111:      * @param string $method  The method name
112:      * @param array  $params  Method parameters
113:      *
114:      * @return void
115:      */
116:     protected function _finalizeParams(&$params)
117:     {
118:         // Run through the params and see if any of them are arrays. If so,
119:         // json encode them, as per the new Facebook API guidlines.
120:         // http://www.facebook.com/developers/message.php#msg_351
121:         foreach ($params as &$param) {
122:             if (is_array($param)) {
123:                 $param = json_encode($param);
124:             }
125:         }
126: 
127:         $this->_addStandardParams($params);
128:     }
129: 
130:     /**
131:      * Adds standard facebook api parameters to $params
132:      *
133:      * @param array  $params  Method parameters
134:      *
135:      * @return void
136:      */
137:     protected function _addStandardParams(&$params)
138:     {
139:         $params['access_token'] = $this->_facebook->auth->getSessionKey();
140:         if ($this->_facebook->dataFormat == Horde_Service_Facebook::DATA_FORMAT_ARRAY) {
141:             $params['format'] = $this->_facebook->internalFormat;
142:         } else {
143:             $params['format'] = $this->_facebook->dataFormat;
144:         }
145:         if (!isset($params['v'])) {
146:             $params['v'] = '1.0';
147:         }
148:         if (!empty($this->_facebook->useSslResources)) {
149:             $params['return_ssl_resources'] = true;
150:         }
151:     }
152: 
153:     /**
154:      * Helper function to convert array to CSV string
155:      *
156:      * @param array $params
157:      * @return string
158:      */
159:     protected function _convertToCsv(&$params)
160:     {
161:         foreach ($params as $key => &$val) {
162:             if (is_array($val)) {
163:                 $val = implode(',', $val);
164:             }
165:         }
166:     }
167: 
168:     /**
169:      * Create a string suitable for sending as POST data.
170:      *
171:      * TODO: Figure out why using http_build_query doesn't work here.
172:      *
173:      * @param array $params  The parameters array
174:      *
175:      * @return string  The POST string
176:      */
177:     protected function _createPostString($params)
178:     {
179:         $post_params = array();
180:         foreach ($params as $key => &$val) {
181:             $post_params[] = $key.'='.urlencode($val);
182:         }
183: 
184:         return implode('&', $post_params);
185:     }
186: 
187: }
API documentation generated by ApiGen