Exemple #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;
 }
Exemple #2
0
 /**
  * @inheritdoc
  */
 protected function renderDataCellContent($model, $key, $index)
 {
     $text = $model->{$this->attribute};
     return "<small>" . StringHelper::substr($text, 0, $this->maxLength) . (StringHelper::strlen($text) > $this->maxLength ? " ..." : "") . "</small>";
 }