/**
  * newSessionAfterValidation - Returns a FacebookSession for a
  *   validated & parsed signed request.
  *
  * @param array $parsedSignedRequest
  *
  * @return FacebookSession
  *
  * @throws FacebookRequestException
  */
 private static function newSessionAfterValidation($parsedSignedRequest)
 {
     $params = array('client_id' => self::$defaultAppId, 'redirect_uri' => '', 'client_secret' => self::$defaultAppSecret, 'code' => $parsedSignedRequest['code']);
     $response = (new FacebookRequest(self::newAppSession(self::$defaultAppId, self::$defaultAppSecret), 'GET', '/oauth/access_token', $params))->execute()->getResponse();
     if (isset($response['access_token'])) {
         return new FacebookSession($response['access_token'], $parsedSignedRequest);
     }
     throw FacebookRequestException::create(json_encode($parsedSignedRequest), $parsedSignedRequest, 401);
 }
 /**
  * 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 (isset($this->etag)) {
         $connection->addRequestHeader('If-None-Match', $this->etag);
     }
     $result = $connection->send($url, $this->method, $params);
     // Client error
     if ($result === false) {
         throw new FacebookSDKException($connection->getErrorMessage(), $connection->getErrorCode());
     }
     $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);
 }
 /**
  * execute - Makes the request to Facebook and returns the result.
  *
  * @return FacebookResponse
  */
 public function execute()
 {
     $url = $this->getRequestURL();
     $params = $this->getParameters();
     $curl = curl_init();
     $options = array(CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 60, CURLOPT_ENCODING => '', CURLOPT_USERAGENT => 'fb-php-' . self::VERSION);
     if ($this->method === "GET") {
         $url = $url . "?" . http_build_query($params);
     } else {
         $options[CURLOPT_POSTFIELDS] = $params;
     }
     $options[CURLOPT_URL] = $url;
     curl_setopt_array($curl, $options);
     $result = curl_exec($curl);
     $error = curl_errno($curl);
     if ($error == 60 || $error == 77) {
         curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'fb_ca_chain_bundle.crt');
         $result = curl_exec($curl);
         $error = curl_errno($curl);
     }
     // With dual stacked DNS responses, it's possible for a server to
     // have IPv6 enabled but not have IPv6 connectivity.  If this is
     // the case, curl will try IPv4 first and if that fails, then it will
     // fall back to IPv6 and the error EHOSTUNREACH is returned by the
     // operating system.
     if ($result === false && empty($opts[CURLOPT_IPRESOLVE])) {
         $matches = array();
         $regex = '/Failed to connect to ([^:].*): Network is unreachable/';
         if (preg_match($regex, curl_error($curl), $matches)) {
             if (strlen(@inet_pton($matches[1])) === 16) {
                 error_log('Invalid IPv6 configuration on server, ' . 'Please disable or get native IPv6 on your server.');
                 curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
                 $result = curl_exec($curl);
                 $error = curl_errno($curl);
             }
         }
     }
     $errorMessage = curl_error($curl);
     $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     curl_close($curl);
     if ($result === false) {
         throw new FacebookSDKException($errorMessage, $error);
     }
     $decodedResult = json_decode($result);
     if ($decodedResult === null) {
         $out = array();
         parse_str($result, $out);
         return new FacebookResponse($this, $out, $result);
     }
     if (isset($decodedResult->error)) {
         throw FacebookRequestException::create($result, $decodedResult->error, $httpStatus);
     }
     return new FacebookResponse($this, $decodedResult, $result);
 }
Beispiel #4
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();
     $curl = curl_init();
     $options = array(CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 60, CURLOPT_ENCODING => '', CURLOPT_USERAGENT => 'fb-php-' . self::VERSION, CURLOPT_HEADER => true);
     if ($this->method === "GET") {
         $url = self::appendParamsToUrl($url, $params);
     } else {
         $options[CURLOPT_POSTFIELDS] = $params;
     }
     if ($this->method === 'DELETE' || $this->method === 'PUT') {
         $options[CURLOPT_CUSTOMREQUEST] = $this->method;
     }
     $options[CURLOPT_URL] = $url;
     // ETag
     if ($this->etag != null) {
         $options[CURLOPT_HTTPHEADER] = array('If-None-Match: ' . $this->etag);
     }
     curl_setopt_array($curl, $options);
     $result = curl_exec($curl);
     $error = curl_errno($curl);
     if ($error == 60 || $error == 77) {
         curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'fb_ca_chain_bundle.crt');
         $result = curl_exec($curl);
         $error = curl_errno($curl);
     }
     // With dual stacked DNS responses, it's possible for a server to
     // have IPv6 enabled but not have IPv6 connectivity.  If this is
     // the case, curl will try IPv4 first and if that fails, then it will
     // fall back to IPv6 and the error EHOSTUNREACH is returned by the
     // operating system.
     if ($result === false && empty($opts[CURLOPT_IPRESOLVE])) {
         $matches = array();
         $regex = '/Failed to connect to ([^:].*): Network is unreachable/';
         if (preg_match($regex, curl_error($curl), $matches)) {
             if (strlen(@inet_pton($matches[1])) === 16) {
                 error_log('Invalid IPv6 configuration on server, ' . 'Please disable or get native IPv6 on your server.');
                 curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
                 $result = curl_exec($curl);
                 $error = curl_errno($curl);
             }
         }
     } else {
         $info = curl_getinfo($curl);
         if ($info['http_code'] == 304) {
             $etagHit = true;
         } else {
             $etagHit = false;
         }
         $headers = mb_substr($result, 0, $info['header_size']);
         $result = mb_substr($result, $info['header_size']);
         if (($etagPos = strpos($headers, 'ETag: ')) !== FALSE) {
             $etagPos += strlen('ETag: ');
             $etagReceived = substr($headers, $etagPos, strpos($headers, chr(10), $etagPos) - $etagPos - 1);
         } else {
             $etagReceived = null;
         }
     }
     $errorMessage = curl_error($curl);
     $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     curl_close($curl);
     if ($result === false) {
         throw new FacebookSDKException($errorMessage, $error);
     }
     $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, $httpStatus);
     }
     return new FacebookResponse($this, $decodedResult, $result, $etagHit, $etagReceived);
 }