Ejemplo n.º 1
0
 /**
  * Build the URL for given domain alias, path and parameters.
  *
  * @param string $name The name of the domain
  * @param string $path Optional path (without a leading slash)
  * @param array $params Optional query parameters
  *
  * @return UrlScript The URL for the given parameters
  */
 public function createUrl($name, $path = NULL, $params = array())
 {
     if (preg_match('~^https?://([^.]+\\.)?github\\.com/~', trim($path))) {
         $url = new UrlScript($path);
     } else {
         $url = new UrlScript($this->domains[$name]);
         $url->path .= ltrim($path, '/');
     }
     $url->appendQuery(array_map(function ($param) {
         return $param instanceof UrlScript ? (string) $param : $param;
     }, $params));
     return $url;
 }
Ejemplo n.º 2
0
 /**
  * Build the URL for given domain alias, path and parameters.
  *
  * @param string $name The name of the domain
  * @param string $path Optional path (without a leading slash)
  * @param array $params Optional query parameters
  *
  * @return Http\UrlScript The URL for the given parameters
  */
 public function createUrl($name, $path = NULL, $params = [])
 {
     if (preg_match('~^https?://([^.]+\\.)?twitter\\.com/~', trim($path))) {
         $url = new Http\UrlScript($path);
     } else {
         $url = new Http\UrlScript($this->domains[$name]);
         $path = $url->getPath() . ltrim($path, '/');
         $url->setPath($path);
     }
     $url->appendQuery(array_map(function ($param) {
         return $param instanceof Http\UrlScript ? (string) $param : $param;
     }, $params));
     return $url;
 }
Ejemplo n.º 3
0
 /**
  * Makes an HTTP request. This method can be overridden by subclasses if
  * developers want to do fancier things or use something other than curl to
  * make the request.
  *
  * @param string $url The URL to make the request to
  * @param array $params The parameters to use for the POST body
  * @param resource $ch Initialized curl handle
  *
  * @throws Facebook\FacebookApiException
  * @return string The response text
  */
 protected function makeRequest($url, array $params, $ch = NULL)
 {
     if (isset($this->cache[$cacheKey = md5(serialize(array($url, $params)))])) {
         return $this->cache[$cacheKey];
     }
     $url = new UrlScript($url);
     $method = strtoupper(isset($params['method']) ? $params['method'] : 'GET');
     $this->onRequest((string) $url, $params);
     $ch = $ch ?: curl_init();
     $opts = $this->curlOptions;
     if ($this->fb->config->graphVersion !== '' && $this->fb->config->graphVersion !== 'v1.0') {
         // v2.0 or later
         unset($params['method']);
         if ($method === 'GET') {
             $url->appendQuery($params);
             $params = array();
         }
         if ($method === 'DELETE' || $method === 'PUT') {
             $opts[CURLOPT_CUSTOMREQUEST] = $method;
         }
         if ($method !== 'GET') {
             $opts[CURLOPT_POSTFIELDS] = $params;
         }
         $opts[CURLOPT_HTTPHEADER][] = 'Accept-Encoding: *';
     } else {
         // BC
         $opts[CURLOPT_POSTFIELDS] = $this->fb->config->fileUploadSupport ? $params : http_build_query($params, NULL, '&');
         // disable the 'Expect: 100-continue' behaviour. This causes CURL to wait
         // for 2 seconds if the server does not support this header.
         $opts[CURLOPT_HTTPHEADER][] = 'Expect:';
     }
     $opts[CURLOPT_URL] = (string) $url;
     // execute request
     curl_setopt_array($ch, $opts);
     $result = curl_exec($ch);
     // provide certificate if needed
     if (curl_errno($ch) == CURLE_SSL_CACERT || curl_errno($ch) === CURLE_SSL_CACERT_BADFILE) {
         Debugger::log('Invalid or no certificate authority found, using bundled information', 'facebook');
         $this->curlOptions[CURLOPT_CAINFO] = CertificateHelper::getCaInfoFile();
         curl_setopt($ch, CURLOPT_CAINFO, CertificateHelper::getCaInfoFile());
         $result = curl_exec($ch);
     }
     // 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();
         if (preg_match('/Failed to connect to ([^:].*): Network is unreachable/', curl_error($ch), $matches)) {
             if (strlen(@inet_pton($matches[1])) === 16) {
                 Debugger::log('Invalid IPv6 configuration on server, Please disable or get native IPv6 on your server.', 'facebook');
                 $this->curlOptions[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4;
                 curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
                 $result = curl_exec($ch);
             }
         }
     }
     $info = curl_getinfo($ch);
     if (isset($info['request_header'])) {
         list($info['request_header']) = self::parseHeaders($info['request_header']);
     }
     $info['method'] = $method;
     if ($result === FALSE) {
         $e = new Facebook\FacebookApiException(array('error_code' => curl_errno($ch), 'error' => array('message' => curl_error($ch), 'type' => 'CurlException')));
         curl_close($ch);
         $this->onError($e, $info);
         throw $e;
     }
     if (!$result && isset($info['redirect_url'])) {
         $result = Json::encode(array('url' => $info['redirect_url']));
     }
     $info['headers'] = self::parseHeaders(substr($result, 0, $info['header_size']));
     $result = trim(substr($result, $info['header_size']));
     $this->onSuccess($result, $info);
     curl_close($ch);
     return $this->cache[$cacheKey] = $result;
 }