コード例 #1
0
ファイル: api.php プロジェクト: huylv-hust/ws
 /**
  * get sscode
  * @author NamDD
  * @since 1.0.0
  * @param
  * @return array ss
  */
 protected static function _api($url, $params, $method = 'post')
 {
     $curl = new curl\Curl();
     $curl->setOption(CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
     if (isset(static::$api['proxy'])) {
         $curl->set_option(CURLOPT_PROXY, static::$api['proxy']);
     }
     if ($method == 'post') {
         $curl->setOption(CURLOPT_POSTFIELDS, json_encode($params, JSON_UNESCAPED_UNICODE));
     } else {
         $url = $url . '?' . http_build_query($params);
     }
     $res = $curl->{$method}($url);
     $res = json_decode($res, true);
     $status = $curl->responseCode;
     if ($status == 200) {
         \Yii::info('Api result: ' . print_r($params, true) . print_r($res, true));
         return $res;
     }
     \Yii::info('Api error: ' . print_r($params, true) . print_r($res, true));
     return [];
 }