createRequest() public method

public createRequest ( ) : Request
return Request request instance.
コード例 #1
0
ファイル: ApiBase.php プロジェクト: v3toys/yii2-api
 /**
  * @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
ファイル: Api.php プロジェクト: strong2much/yii2-facebook
 /**
  * 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;
 }
コード例 #3
0
 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
ファイル: CbtresultController.php プロジェクト: pengfen/work
    /**
     * 拼装申报数据并返回响应结果
     * $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;
        }
    }
コード例 #5
0
 /**
  * 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
ファイル: GCM.php プロジェクト: strong2much/yii2-google
 /**
  * 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;
 }