Ejemplo n.º 1
0
 /**
  * Debugger
  * @return resource|string
  */
 public function __debugInfo()
 {
     return ['ramData' => (array) $this->getRawData(), 'jsonEncoded' => JsonParser::decode($this->getRawData())];
 }
Ejemplo n.º 2
0
 /**
  * Curl request
  * @param string      $url
  * @param string      $method
  * @param array       $postFields
  * @param null|string $apiUrl
  * @return string
  */
 public function request($url, $method = Request::METHOD_GET, $postFields = [], $apiUrl = null)
 {
     /** Building url */
     if (null === $apiUrl) {
         $apiUrl = $this->getApiUrl();
     }
     $url = $apiUrl . $url;
     /**
      * OAuth2 Key/Secret authentication
      * @see https://developer.github.com/v3/#oauth2-keysecret
      */
     if (null !== $this->getClientId() && null !== $this->getClientSecret()) {
         $url .= strstr($url, '?') !== false ? '&' : '?';
         $url .= http_build_query(['client_id' => $this->getClientId(), 'client_secret' => $this->getClientSecret()]);
     } else {
         if ($this->getAuthentication() === self::OAUTH2_PARAMETERS_AUTH) {
             $url .= http_build_query(['access_token' => $this->getToken()]);
         }
     }
     /** Call curl */
     $curl = new Curl();
     $curl->setOption([CURLOPT_USERAGENT => self::USER_AGENT, CURLOPT_TIMEOUT => $this->getTimeout(), CURLOPT_HEADER => false, CURLOPT_FOLLOWLOCATION => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HTTPHEADER => ['Accept: ' . $this->getAccept(), 'Content-Type: ' . $this->getContentType()], CURLOPT_URL => $url]);
     /**
      * Basic authentication via username and Password
      * @see https://developer.github.com/v3/auth/#via-username-and-password
      */
     if (!empty($this->getHttpAuth())) {
         if (!isset($this->getHttpAuth()['password']) || empty($this->getHttpAuth()['password'])) {
             $curl->setOption([CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => $this->getHttpAuth()['username']]);
         } else {
             $curl->setOption([CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => sprintf('%s:%s', $this->getHttpAuth()['username'], $this->getHttpAuth()['password'])]);
         }
     }
     if (!empty($this->getToken()) && $this->getAuthentication() !== self::OAUTH2_PARAMETERS_AUTH) {
         /**
          * Basic authentication via OAuth token
          * @see https://developer.github.com/v3/auth/#via-oauth-tokens
          **/
         if ($this->getAuthentication() === self::OAUTH_AUTH) {
             $curl->setOption([CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => sprintf('%s:x-oauth-basic', $this->getToken())]);
         } else {
             if ($this->getAuthentication() === self::OAUTH2_HEADER_AUTH) {
                 $curl->setOption([CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_HTTPHEADER => [sprintf('Authorization: token %s', $this->getToken())]]);
             }
         }
     }
     /** Methods */
     switch ($method) {
         /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7 */
         case Request::METHOD_DELETE:
             /** @see http://tools.ietf.org/html/rfc5789 */
         /** @see http://tools.ietf.org/html/rfc5789 */
         case Request::METHOD_PATCH:
             $curl->setOption([CURLOPT_CUSTOMREQUEST => $method, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode(array_filter($postFields))]);
             break;
             /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3 */
         /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3 */
         case Request::METHOD_GET:
             $curl->setOption(CURLOPT_HTTPGET, true);
             break;
             /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 */
         /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 */
         case Request::METHOD_HEAD:
             $curl->setOption([CURLOPT_CUSTOMREQUEST => $method, CURLOPT_NOBODY => true]);
             break;
             /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5 */
         /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5 */
         case Request::METHOD_POST:
             $curl->setOption([CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode(array_filter($postFields))]);
             break;
             /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6 */
         /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6 */
         case Request::METHOD_PUT:
             $curl->setOption([CURLOPT_CUSTOMREQUEST => $method, CURLOPT_PUT => true, CURLOPT_HTTPHEADER => ['X-HTTP-Method-Override: PUT', 'Content-type: application/x-www-form-urlencoded']]);
             break;
         default:
             break;
     }
     $curl->success(function (Curl $instance) {
         $this->headers = $instance->getHeaders();
         $this->success = $instance->getResponse();
         if ($this->jsonValidator->isValid($this->success)) {
             $this->success = JsonParser::decode($this->success);
         }
     });
     $curl->error(function (Curl $instance) {
         $this->headers = $instance->getHeaders();
         $this->failure = $instance->getResponse();
         if ($this->jsonValidator->isValid($this->failure)) {
             $this->failure = JsonParser::decode($this->failure);
         }
     });
     $curl->perform();
     return null != $this->success ? $this->success : $this->failure;
 }