1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: class Horde_Service_Facebook_Request
12: {
13: 14: 15: 16:
17: protected $_facebook;
18:
19: 20: 21: 22:
23: protected $_http;
24:
25: 26: 27: 28: 29:
30: protected $_method;
31:
32: 33: 34: 35: 36:
37: protected $_params;
38:
39: 40: 41: 42: 43: 44: 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: 57: 58: 59: 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: 87: 88: 89: 90: 91: 92: 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: 110: 111: 112: 113: 114: 115:
116: protected function _finalizeParams(&$params)
117: {
118:
119:
120:
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: 132: 133: 134: 135: 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: 155: 156: 157: 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: 170: 171: 172: 173: 174: 175: 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: }