createRequest() публичный Метод

public createRequest ( ) : Request
Результат Request request instance.
Пример #1
0
 /**
  * @param array $data
  * @param string $method
  *
  * @return ApiResponseOk|ApiResponseError
  */
 private function _send(array $data, $method = 'post')
 {
     $client = new Client(['requestConfig' => ['format' => Client::FORMAT_JSON]]);
     $response = $client->createRequest()->setMethod($method)->setUrl($this->url)->addHeaders(['Content-type' => 'application/json'])->addHeaders(['user-agent' => 'JSON-RPC PHP Client'])->setData($data)->setOptions(['timeout' => $this->timeout])->send();
     //Нам сказали это всегда json. А... нет, все мы люди, бывает и не json )
     try {
         $dataResponse = (array) Json::decode($response->content);
     } catch (\Exception $e) {
         \Yii::error("Json api response error: " . $e->getMessage() . ". Response: \n{$response->content}", self::className());
         //Лайф хак, вдруг разработчики апи оставили var dump
         if ($pos = strpos($response->content, "{")) {
             $content = StringHelper::substr($response->content, $pos, StringHelper::strlen($response->content));
             try {
                 $dataResponse = (array) Json::decode($content);
             } catch (\Exception $e) {
                 \Yii::error("Api response error: " . $response->content, self::className());
             }
         }
     }
     if (!$response->isOk) {
         \Yii::error($response->content, self::className());
         $responseObject = new ApiResponseError($dataResponse);
     } else {
         $responseObject = new ApiResponseOk($dataResponse);
     }
     $responseObject->statusCode = $response->statusCode;
     return $responseObject;
 }
Пример #2
0
 /**
  * Initializes a new request.
  * @param string $url relative url to request.
  * @param array $options HTTP request options. Keys: query, data, headers, options
  * @return \yii\httpclient\Request
  */
 protected function initRequest($url, $options = [])
 {
     $client = new Client(['baseUrl' => self::API_BASE_URL . '/v' . $this->version, 'responseConfig' => ['format' => Client::FORMAT_JSON]]);
     $request = $client->createRequest();
     if (isset($options['data'])) {
         $request->setMethod('post');
         $request->setData($options['data']);
     }
     if (!empty($options['headers'])) {
         $request->setHeaders($options['headers']);
     }
     if (!empty($options['options'])) {
         $request->setOptions($options['options']);
     }
     if (isset($options['query'])) {
         $url_parts = parse_url($url);
         if (isset($url_parts['query'])) {
             $query = $url_parts['query'];
             if (strlen($query) > 0) {
                 $query .= '&';
             }
             $query .= http_build_query($options['query']);
             $url = str_replace($url_parts['query'], $query, $url);
         } else {
             $url_parts['query'] = $options['query'];
             $new_query = http_build_query($url_parts['query']);
             $url .= '?' . $new_query;
         }
     }
     $request->setUrl($url);
     return $request;
 }
 private function getRemoteRates()
 {
     /**
      * uploading rates from European Central Bank
      */
     $client = new Client();
     $response = $client->createRequest()->setMethod('get')->setFormat(Client::FORMAT_XML)->setUrl(self::RATES_XML_URL)->send();
     $this->xml = $response;
     $currency = new Currency(['code' => 'EUR', 'rateToEur' => 1]);
     $this->rates[$currency->code] = $currency;
     foreach ($response->data['Cube']['Cube']['Cube'] as $k => $v) {
         $currency = new Currency();
         $currency->code = (string) $v['currency'];
         $currency->rateToEur = (double) $v['rate'];
         $this->rates[$currency->code] = $currency;
     }
     //foreach
 }
Пример #4
0
    /**
     * 拼装申报数据并返回响应结果
     * $data 原生数据
    */
    private function response($data, $url){
    	//echo $url;
        $basic = $this->basicData();

        $array = array_merge($basic, $data);
        $EData = json_encode($array); // 原生数据
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('post')
            ->setUrl($url)
            ->setData(['EData' => $EData])
            ->send();

        if ($response->isOk) {
            $res = json_decode($response->content);
            return $res;
        }
    }
 /**
  * Private method to send request for all methods
  * @param $url
  * @param $params
  * @param string $format
  * @return Response
  */
 private function _send($url, $params, $format = 'post')
 {
     $data = ArrayHelper::merge(['login' => $this->login, 'pass' => $this->pass], $params);
     return $this->_client->createRequest()->setUrl($url)->setMethod($format)->setData($data)->send();
 }
Пример #6
0
 public static function request($url, $params = array(), $requestMethod = 'get')
 {
     $client = new Client();
     $response = $client->createRequest()->setMethod($requestMethod)->setUrl($url)->setData($params)->send();
     if ($response->isOk) {
         return $response->content;
     } else {
         return null;
     }
 }
Пример #7
0
 /**
  * Initializes a new request.
  * @param array $options HTTP post data
  * @return \yii\httpclient\Request
  */
 protected function initRequest($options = [])
 {
     $client = new Client(['requestConfig' => ['format' => Client::FORMAT_JSON]]);
     $request = $client->createRequest()->setMethod('post')->setUrl(self::BASE_URL)->setData($options)->setHeaders(['Authorization' => 'key=' . $this->_apiKey]);
     return $request;
 }