/** * @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; }
/** * 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; }
/** * 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); } } }