Пример #1
0
 /**
  * execute - Makes the request to Facebook and returns the result.
  *
  * @return FacebookResponse
  *
  * @throws FacebookSDKException
  * @throws FacebookRequestException
  */
 public function execute()
 {
     $url = $this->getRequestURL();
     $params = $this->getParameters();
     if ($this->method === 'GET') {
         $url = self::appendParamsToUrl($url, $params);
         $params = array();
     }
     $connection = self::getHttpClientHandler();
     $connection->addRequestHeader('User-Agent', 'fb-php-' . self::VERSION);
     $connection->addRequestHeader('Accept-Encoding', '*');
     // Support all available encodings.
     // ETag
     if (null !== $this->etag) {
         $connection->addRequestHeader('If-None-Match', $this->etag);
     }
     // Should throw `FacebookSDKException` exception on HTTP client error.
     // Don't catch to allow it to bubble up.
     $result = $connection->send($url, $this->method, $params);
     static::$requestCount++;
     $etagHit = 304 === $connection->getResponseHttpStatusCode();
     $headers = $connection->getResponseHeaders();
     $etagReceived = isset($headers['ETag']) ? $headers['ETag'] : null;
     $decodedResult = json_decode($result);
     if ($decodedResult === null) {
         $out = array();
         parse_str($result, $out);
         return new FacebookResponse($this, $out, $result, $etagHit, $etagReceived);
     }
     if (isset($decodedResult->error)) {
         throw FacebookRequestException::create($result, $decodedResult->error, $connection->getResponseHttpStatusCode());
     }
     return new FacebookResponse($this, $decodedResult, $result, $etagHit, $etagReceived);
 }
Пример #2
0
 /**
  * Request a code from a long lived access token.
  *
  * @param array $params
  * @param string|null $appId
  * @param string|null $appSecret
  *
  * @return string
  *
  * @throws FacebookRequestException
  */
 public static function requestCode(array $params, $appId = null, $appSecret = null)
 {
     $response = static::request('/oauth/client_code', $params, $appId, $appSecret);
     $data = $response->getResponse();
     if (isset($data->code)) {
         return (string) $data->code;
     }
     throw FacebookRequestException::create($response->getRawResponse(), $data, 401);
 }