public function fetch($uri, $method, $parameters = array(), $parse_mode = null, $expect_http_code = 200) { switch ($this->system['version']) { case '1.0': $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->access_token, $method, $uri, $parameters); if ($this->system['info']['secret'] === false) { $request->sign_request(new SystemBasedOAuthSignatureMethod_RSA_SHA1($this->system['info']), $this->consumer, $this->access_token); } else { $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, $this->access_token); } $this->debug('BASE STRING: ' . $request->get_signature_base_string()); if ($this->system['use_auth_header']) { $to_header = $request->to_header(); $response_raw = $this->sendRequest($uri, $method, $request->to_postdata(), array($to_header)); } else { $response_raw = $this->sendRequest($request->to_url(), $method, $request->to_postdata()); } break; case '2.0': if ($this->system['info']['access_token']) { if (strpos($uri, '?') === false) { $uri .= '?'; } else { $uri .= '&'; } $uri .= 'access_token=' . $this->system['info']['access_token']; } $response_raw = $this->sendRequest($uri, $method, $parameters); break; } if (!is_numeric($expect_http_code) or $expect_http_code == $this->http_code) { $response = false; if ($parse_mode === null) { $parse_mode = $this->default_parse_mode; } switch ($parse_mode) { case 'xml': $this->debug('Parsing as XML'); $response = OAuthHelper::parse_xml($response_raw); break; case 'json': $this->debug('Parsing as JSON'); $response = OAuthHelper::parse_json($response_raw); break; case 'param': $this->debug('Parsing as Parametters'); parse_str($response_raw, $response); break; case 'none': $this->debug('No parsing!'); $response = $response_raw; break; case 'custom': default: $this->debug('Calling custom parsing method'); $response = $this->parse_response($response_raw, $parse_mode); break; } } else { $this->debug('UNEXPECTED HTTP CODE: ' . $this->http_code . ' (SHOULD BE ' . $expect_http_code . ')'); $response = $response_raw; } $this->debug('Response: <pre>' . var_export($response, true) . '</pre><hr/>'); return $response; }
protected function parse_response($raw, $mode) { if ($mode == 'fb_json') { $response = OAuthHelper::parse_json($raw); if (isset($response['data'])) { $data = $response['data']; unset($response); } else { $data =& $response; } foreach (array_keys($data) as $key) { if (!is_array($data[$key])) { continue; } $type = false; $has = array(); //album if (strpos($data[$key]['link'], 'album.php') !== false) { $type = 'album'; $has = array('photos' => 'photo', 'comments' => 'comment'); } else { if (isset($data[$key]['start_time']) and isset($data[$key]['venue'])) { $type = 'event'; $has = array('feed' => 'post', 'picture' => 'picture', 'noreply' => 'user', 'maybe' => 'user', 'invited' => 'user', 'attending' => 'user', 'declined' => 'user'); } else { if (isset($data[$key]['venue'])) { $type = 'group'; $has = array('feed' => 'post', 'members' => 'user', 'picture' => 'picture'); } else { if (isset($data[$key]['to']) and isset($data[$key]['message'])) { $type = 'message'; $has = array('comments' => 'comment'); } else { if (isset($data[$key]['subject']) and isset($data[$key]['message'])) { $type = 'note'; $has = array('comments' => 'comment'); } else { if (isset($data[$key]['category'])) { $type = 'page'; $has = array('feed' => 'post', 'picture' => 'picture', 'tagged' => 'post', 'links' => 'post', 'photos' => 'photo', 'groups' => 'group', 'albums' => 'album', 'statuses' => 'post', 'videos' => 'video', 'notes' => 'note', 'posts' => 'post', 'events' => 'event'); } else { if (isset($data[$key]['picture'])) { $type = 'photo'; $has = array('comments' => 'comment'); } else { if (isset($data[$key]['first_name']) and isset($data[$key]['last_name'])) { $type = 'user'; $has = array('home' => 'post', 'feed' => 'post', 'tagged' => 'post', 'posts' => 'post', 'picture' => 'picture', 'friends' => 'user', 'activities' => 'page', 'interests' => 'page', 'music' => 'page', 'books' => 'page', 'movies' => 'page', 'television' => 'page', 'likes' => 'page', 'photos' => 'photo', 'albums' => 'album', 'videos' => 'video', 'groups' => 'group', 'statuses' => 'post', 'links' => 'post', 'notes' => 'note', 'events' => 'event', 'inbox' => 'message', 'outbox' => 'message', 'updates' => 'message'); } else { if (isset($data[$key]['length'])) { $type = 'video'; $has = array('comments' => 'comment'); } else { if (isset($data[$key]['from'])) { //maybe a post $type = 'post'; $has = array('comments' => 'comment'); } } } } } } } } } } } return $data; } else { return parent::parse_response($raw, $mode); } }