/** * Fetch and store a new App access_token from Facebook * * @return string the access_token * @throws FacebookAuthException if our request was denied * @throws FacebookApiException if some unexpected response was received from Facebook */ protected function fetchAccessToken() { $request = new GraphRequest(); $client = $request->getClient(); $request = $this->buildFacebookHttpRequest($client); $response = $request->send(); if ($response->getStatusCode() == 200) { parse_str($response->getBody(true), $arr); if (isset($arr['access_token'])) { $this->session_cached = new \DateTime(); $this->access_token = (string) $arr['access_token']; } else { throw new FacebookApiException('Unexpected Facebook error'); } } else { throw new FacebookAuthException(sprintf('Facebook error: %s', $response->getBody(true))); } }
/** * Prepare a Graph HTTP request * * @return \Guzzle\Http\Message\RequestInterface */ public function getGraphHttpRequest() { $graph_request = new GraphRequest(); $client = $graph_request->getClient(); $request = $this->buildFacebookHttpRequest($client); return $request; }
/** * Load the node and return a populated object * * @param string $id unique node id * @param array $fields (optional) array of field names to fetch * * @return $this * @throws FacebookInvalidNodeException */ public function load($id, $fields = array()) { $request = new GraphRequest(); $request->setNode($id); if (count($fields) > 0) { $request->setFields($fields); } $response = $request->send(); $arr = json_decode($response->getBody(true), true); if (is_array($arr)) { foreach ($arr as $key => $val) { $this->setFieldValue($key, $val); } $this->is_new = false; $this->is_modified = false; return $this; } throw new FacebookInvalidNodeException(sprintf('Id %s not valid', $id)); }
/** * Load the subscriptions for this Application from Facebook Graph * * @returns void * @throws \Graph\Exception\InvalidArgumentException */ public function fetchSubscriptions() { if ($this->getFieldValue('id') === null) { throw new InvalidArgumentException('Application ID not set'); } $request = new GraphRequest(); $request->setAccessToken($this->getAccessToken()); $request->setNode($this->getFieldValue('id')); $request->setPath('subscriptions'); $response = $request->send(); $json = $response->getBody(true); if ($arr = json_decode($json, true)) { if (isset($arr['data']) && !empty($arr['data'])) { $this->setSubscriptions($response->getBody(true)); } } }
/** * Send a notification to a Player using their Facebook userId * * @return boolean true if the message was successfully sent * @throws FacebookInsufficientPermissions * thrown if the facebook user has not given permission (or has revoked permission) to our app * @throws FacebookAuthException * @throws FacebookApiException * @throws FacebookConnectionException * thrown if we are unable to connect to the Facebook HTTP Api */ public function send() { $this->isValid(); $href = $this->buildHrefString(); $request = new GraphRequest(); $request->setNode($this->facebook_user_id)->setHttpMethod('POST')->setPath('notifications')->setAccessToken($this->access_token)->setQueryParameters(array('template' => $this->message, 'href' => $href)); $response = $request->send(); return $response->getBody(); }