You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.0 KiB

<?php
namespace app\service;
use think\Exception;
use app\common\lib\Sign;
class ApiService {
private static $instance = null;
private $httpClient;
private function __construct() {
// 初始化 GuzzleHttp\Client 實例
$this->httpClient = new \GuzzleHttp\Client();
}
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new ApiService();
}
return self::$instance;
}
public function callApi($url, $method = 'GET', $params = []) {
$params['appid'] = 'sc';
$params['timestamp'] = time();
$params['sign'] = Sign::genSign($params);
$options = ['query' => $params];
$response = $this->httpClient->request($method, $url, $options);
if($response->getStatusCode()!=200){
//throw 異常;
throw new Exception('api error');
}
$result = json_decode($response->getBody()->getContents(),true);
return $result;
}
}