예제 #1
0
 /**
  * @param string $binding
  * @param MethodJwt $jwt
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  * @return MethodJwt
  */
 public function send($binding, MethodJwt $jwt)
 {
     $this->checkBinding($binding);
     $token = $this->encoder->encode($jwt, $this->key, $this->getAlgorithm());
     if ($binding == JwtBindingTypes::HTTP_POST) {
         $response = $this->httpClient->post($this->targetUrl, array(), array('jwt' => $token));
     } else {
         if ($binding == JwtBindingTypes::HTTP_REDIRECT) {
             $response = $this->httpClient->get($this->getRedirectUrl(), array('jwt' => $token));
         } else {
             throw new \InvalidArgumentException('Unsupported or invalid binding ' . $binding);
         }
     }
     $statusCode = $this->httpClient->getStatusCode();
     if ($statusCode != HttpStatusCode::OK) {
         throw new \RuntimeException(sprintf('API error: %s %s', $statusCode, $response));
     }
     $resultJwt = null;
     if ($response) {
         try {
             $result = $this->encoder->decode($response, $this->key);
             $resultJwt = new MethodJwt($result->getHeader(), $result->getPayload());
         } catch (\Exception $ex) {
         }
     }
     if (!$resultJwt) {
         $resultJwt = new MethodJwt();
     }
     if ($ex = $resultJwt->getException()) {
         throw new RemoteMethodException($ex);
     }
     return $resultJwt;
 }
예제 #2
0
 /**
  * @param string $url
  * @param string $method
  * @param array $queryData
  * @param array $postData
  * @param null $contentType
  * @param array $arrHeaders
  * @return string
  * @throws \LogicException
  * @throws HttpException
  */
 protected function makeRequest($url, $method = null, array $queryData = array(), array $postData = array(), $contentType = null, array $arrHeaders = array())
 {
     if (null == $method) {
         if ($this->authType == self::AUTH_TYPE_REQUEST) {
             $method = 'post';
         } else {
             $method = 'get';
         }
     }
     $this->prepareRequest($arrHeaders, $postData);
     switch ($method) {
         case 'post':
             $json = $this->httpClient->post($url, $queryData, $postData, $contentType, $arrHeaders);
             break;
         case 'get':
             $json = $this->httpClient->get($url, $queryData, $arrHeaders);
             break;
         case 'delete':
             $json = $this->httpClient->delete($url, $queryData, $arrHeaders);
             break;
         default:
             throw new \LogicException(sprintf("Unsupported HTTP method '%s'", $method));
     }
     if ($this->httpClient->getStatusCode() != HttpStatusCode::OK) {
         throw new HttpException($this->httpClient->getStatusCode(), $json);
     }
     return $json;
 }