Since: 2.0
Author: Paul Klimov (klimov.paul@gmail.com)
Inheritance: extends Message
 /**
  * @inheritdoc
  */
 public function format(Request $request)
 {
     $contentType = $this->contentType;
     $charset = $this->encoding === null ? Yii::$app->charset : $this->encoding;
     if (stripos($contentType, 'charset') === false) {
         $contentType .= '; charset=' . $charset;
     }
     $request->getHeaders()->set('Content-Type', $contentType);
     $data = $request->getData();
     if ($data !== null) {
         if ($data instanceof DOMDocument) {
             $content = $data->saveXML();
         } elseif ($data instanceof SimpleXMLElement) {
             $content = $data->saveXML();
         } else {
             $dom = new DOMDocument($this->version, $charset);
             $root = new DOMElement($this->rootTag);
             $dom->appendChild($root);
             $this->buildXml($root, $data);
             $content = $dom->saveXML();
         }
         $request->setContent($content);
     }
     return $request;
 }
 /**
  * @inheritdoc
  */
 public function format(Request $request)
 {
     $data = (array) $request->getData();
     $content = http_build_query($data, '', '&', $this->encodingType);
     if (strcasecmp('get', $request->getMethod()) === 0) {
         if (!empty($content)) {
             $url = $request->getUrl();
             $url .= strpos($url, '?') === false ? '?' : '&';
             $url .= $content;
             $request->setUrl($url);
         }
         return $request;
     }
     $request->getHeaders()->set('Content-Type', 'application/x-www-form-urlencoded');
     $request->setContent($content);
     return $request;
 }
 /**
  * @inheritdoc
  */
 public function format(Request $request)
 {
     $request->getHeaders()->set('Content-Type', 'application/json; type=entry');
     $request->setContent(Json::encode($request->getData(), $this->encodeOptions));
     return $request;
 }
Esempio n. 4
0
 /**
  * @return Request request instance.
  */
 public function createRequest()
 {
     $config = $this->requestConfig;
     if (!isset($config['class'])) {
         $config['class'] = Request::className();
     }
     $config['client'] = $this;
     return Yii::createObject($config);
 }
 /**
  * Prepare request for execution, creating cURL resource for it.
  * @param Request $request request instance.
  * @return array cURL options.
  */
 private function prepare($request)
 {
     $request->prepare();
     $curlOptions = $this->composeCurlOptions($request->getOptions());
     $method = strtoupper($request->getMethod());
     switch ($method) {
         case 'GET':
             break;
         case 'POST':
             $curlOptions[CURLOPT_POST] = true;
             break;
         default:
             $curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
     }
     $content = $request->getContent();
     if ($content !== null) {
         $curlOptions[CURLOPT_POSTFIELDS] = $content;
     }
     $curlOptions[CURLOPT_RETURNTRANSFER] = true;
     $curlOptions[CURLOPT_URL] = $request->getUrl();
     $curlOptions[CURLOPT_HTTPHEADER] = $request->composeHeaderLines($request);
     return $curlOptions;
 }
Esempio n. 6
0
 /**
  * Sign given request with [[signatureMethod]].
  * @param \yii\httpclient\Request $request request instance.
  * @param OAuthToken|null $token OAuth token to be used for signature, if not set [[accessToken]] will be used.
  * @since 2.1 this method is public.
  */
 public function signRequest($request, $token = null)
 {
     $params = $request->getData();
     if (isset($params['oauth_signature_method'])) {
         // avoid double sign of request
         return;
     }
     if (empty($params)) {
         $params = $this->generateCommonRequestParams();
     } else {
         $params = array_merge($this->generateCommonRequestParams(), $params);
     }
     $url = $request->getFullUrl();
     $signatureMethod = $this->getSignatureMethod();
     $params['oauth_signature_method'] = $signatureMethod->getName();
     $signatureBaseString = $this->composeSignatureBaseString($request->getMethod(), $url, $params);
     $signatureKey = $this->composeSignatureKey($token);
     $params['oauth_signature'] = $signatureMethod->generateSignature($signatureBaseString, $signatureKey);
     $request->setData($params);
     if ($this->authorizationHeaderMethods === null || in_array(strtoupper($request->getMethod()), array_map('strtoupper', $this->authorizationHeaderMethods), true)) {
         $authorizationHeader = $this->composeAuthorizationHeader($params);
         if (!empty($authorizationHeader)) {
             $request->addHeaders($authorizationHeader);
         }
     }
 }
Esempio n. 7
0
 /**
  * @param Client $client
  * @return $this
  */
 protected function _httpAuth(Request $request)
 {
     if ($this->httpAuthLogin && $this->httpAuthPassword) {
         $code = base64_encode("{$this->httpAuthLogin}:{$this->httpAuthPassword}");
         $request->addHeaders(['Authorization' => 'Basic ' . $code]);
     }
     return $this;
 }
Esempio n. 8
0
 /**
  * Sends the given HTTP request, returning response data.
  * @param \yii\httpclient\Request $request HTTP request to be sent.
  * @return array response data.
  * @throws InvalidResponseException on invalid remote response.
  * @since 2.1
  */
 protected function sendRequest($request)
 {
     $response = $request->send();
     if (!$response->getIsOk()) {
         throw new InvalidResponseException($response, 'Request failed with code: ' . $response->getStatusCode() . ', message: ' . $response->getContent());
     }
     return $response->getData();
 }