1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: class Horde_Service_Facebook_BatchRequest extends Horde_Service_Facebook_Request
12: {
13: 14: 15: 16: 17:
18: private $_queue = array();
19:
20: 21: 22: 23: 24:
25: private $_batchMode;
26:
27:
28: const BATCH_MODE_DEFAULT = 0;
29: const BATCH_MODE_SERVER_PARALLEL = 0;
30: const BATCH_MODE_SERIAL_ONLY = 2;
31:
32: 33: 34: 35: 36:
37: public function __construct(Horde_Service_Facebook $facebook)
38: {
39: $this->_facebook = $facebook;
40: $this->_http = $facebook->http;
41: if (!empty($params['batch_mode'])) {
42: $this->_batchMode = $params['batch_mode'];
43: } else {
44: $this->_batchMode = self::BATCH_MODE_DEFAULT;
45: }
46: }
47:
48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
59: public function &add($method, array $params)
60: {
61: $result = null;
62: $batch_item = array('m' => $method, 'p' => $params, 'r' => &$result);
63: $this->_queue[] = $batch_item;
64: return $result;
65: }
66:
67: 68: 69: 70: 71:
72: public function run()
73: {
74: $item_count = count($this->_queue);
75: $method_feed = array();
76: foreach ($this->_queue as $batch_item) {
77: $params = $batch_item['p'];
78: $params['method'] = $batch_item['m'];
79: $this->_finalizeParams($params);
80: $method_feed[] = $this->_createPostString($params);
81: }
82: $method_feed_json = json_encode($method_feed);
83:
84: $serial_only = ($this->_batchMode == self::BATCH_MODE_SERIAL_ONLY);
85: $params = array('method_feed' => $method_feed_json,
86: 'serial_only' => $serial_only,
87: 'session_key' => $this->_facebook->auth->getSessionKey());
88: $json = $this->_postRequest('batch.run', $params);
89: $result = json_decode($json, true);
90:
91: if (is_array($result) && isset($result['error_code'])) {
92: throw new Horde_Service_Facebook_Exception($result['error_msg'],
93: $result['error_code']);
94: }
95:
96: for ($i = 0; $i < $item_count; $i++) {
97: $batch_item = $this->_queue[$i];
98: $batch_item_json = $result[$i];
99: $batch_item_result = json_decode($batch_item_json, true);
100: if (is_array($batch_item_result) &&
101: isset($batch_item_result['error_code'])) {
102:
103: throw new Horde_Service_Facebook_Exception($batch_item_result['error_msg'],
104: $batch_item_result['error_code']);
105: }
106: $batch_item['r'] = $batch_item_result;
107: }
108: }
109:
110: }
111: