PHP
::: hljs-left
**以下PHP代码,复制到本地即可调试**
**api地址及账号信息请联系服务商获取**
:::
```php
<?php
/**
* OpenApi客户端类
* @author Jacky 2021-05-18 11:17:52
*/
class OpenApi_Client{
/**
* @var API账号
*/
protected $_api_account = [];
/**
* @var 模块代码
*/
protected $_module = '';
/**
* @var 服务接口
*/
protected $_service = '';
/**
* 构造函数
* @param string $api_host 接口地址
* @param string $company_code 公司代码
* @param string $user_account api账号
* @param string $user_pass api密码
*/
public function __construct($api_host, $company_code, $user_account, $user_pass){
$this->_api_account = [
'api_host'=>$api_host,
'company_code'=>$company_code,
'user_account'=>$user_account,
'user_pass'=>$user_pass,
];
}
/**
* 设置模块
* @param string $module
* @return OpenApi_Client
*/
public function setModule($module){
$this->_module = $module;
return $this;
}
/**
* 设置服务接口
* @param string $service
* @return OpenApi_Client
*/
public function setService($service){
$this->_service = $service;
return $this;
}
/**
* 调用服务
* @param string $module 模块
* @param string $service 服务接口
* @param array $param 参数
* @param array $option curl可选参数
* @example $option = array(
* 'Type'=>'POST', //HTTP操作类型: POST GET PUT DELETE
* 'TimeOut'=>120, //超时时间
* );
*/
public function callService($module, $service, $param, $option=array()){
$return = array(
'ask'=>0,
'code'=>'500',
'message'=>'callService Error',
'errors'=>[],
'module'=>'',
'service'=>'',
'http_statu'=>'',
'response_time'=>'',
'data'=>[]
);
try {
/**
* 1、设置模块和方法、校验参数
*/
$this->setModule($module)->setService($service)->check();
/**
* 2、执行curl请求
*/
$req = array(
'company_code'=>$this->_api_account['company_code'],
'user_account'=>$this->_api_account['user_account'],
'user_pass'=>$this->_api_account['user_pass'],
'module'=>$this->_module,
'service'=>$this->_service,
'data'=>$param
);
$cRe = $this->curl($this->_api_account['api_host'], $req, $option);
$return['http_statu'] = $cRe['http_statu'];
if(!$cRe['ask']){
throw new Exception($cRe['message'], $cRe['code']);
}
/**
* 3、整理返回数据
*/
$rep = $cRe['data'];
$return['ask'] = $rep['code']==200 ? 1 : 0;
$return['code'] = $rep['code'];
$return['message'] = $rep['message'];
$return['errors'] = $rep['errors'];
$return['module'] = $rep['module'];
$return['service'] = $rep['service'];
$return['response_time'] = $rep['response_time'];
$return['data'] = $rep['data'];
} catch (Exception $e) {
$return['code'] = $e->getCode();
$return['message'] = $e->getMessage();
}
return $return;
}
/**
* 执行curl请求
* @param string $url 请求地址
* @param array $req 请求数据
* @param array $option curl可选参数
*/
private function curl($url,$req,$option=array()){
$return = array('ask'=>0,'message'=>'','http_statu'=>'','code'=>'','data'=>array());
//默认可选项
$Type = isset($option['Type']) ? $option['Type'] : 'POST' ;
$TimeOut = isset($option['TimeOut']) ? $option['TimeOut'] : 120 ;
$ch = curl_init() ;//初始化资源句柄
curl_setopt($ch, CURLOPT_URL, $url);//设置请求地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $Type);//设置http操作类型
curl_setopt($ch, CURLOPT_TIMEOUT,$TimeOut);//设置超时时间
curl_setopt ( $ch, CURLOPT_VERBOSE, false);//启用时会汇报所有的信息,存放在STDERR或指定的CURLOPT_STDERR中
curl_setopt($ch, CURLOPT_HEADER, false);//请求头是否包含在响应中
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,false);//是否跟随重定向
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//https请求不验证证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//https请求不验证hosts
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($req));//设置请求数据
//最多循环三次
$request_count = 1;
while ( $request_count <= 3 ) {
//执行请求
$data = curl_exec($ch);
//获取curl请求信息
$curlInfo = curl_getinfo($ch);
$return['http_statu'] = $curlInfo['http_code'];
//curl是否发生错误
if($errNo = curl_errno($ch)){
$errMsg = curl_error($ch);
$return['message'] = 'OpenApiApiCurlError,ErrNo:'.$errNo.',Error:'.$errMsg;
}else{
$return['message'] = '';
break;
}
//请求次数累加
$request_count ++;
}
if($return['message']){
$return['code'] = '5000';
return $return;
}
if(!$data){
$return['message'] = '返回数据为空';
$return['code'] = '5000';
return $return;
}
//请求格式为json
$rep = json_decode($data, true);
if(!($rep && is_array($rep))){
$return['message'] = 'json解析失败';
$return['code'] = '5001';
return $return;
}
//返回数据
$return['ask'] = 1;
$return['message'] = 'success';
$return['data'] = $rep;
return $return;
}
/**
* 参数检查
* @throws Exception
*/
public function check(){
if(!$this->_module){
throw new Exception('module Not Empty', 1001);
}
if(!$this->_service){
throw new Exception('service Not Empty', 1001);
}
if(!$this->_api_account['api_host']){
throw new Exception('api_account.api_host Not Empty', 1001);
}
if(!$this->_api_account['company_code']){
throw new Exception('api_account.company_code Not Empty', 1001);
}
if(!$this->_api_account['user_account']){
throw new Exception('api_account.user_account Not Empty', 1001);
}
if(!$this->_api_account['user_pass']){
throw new Exception('api_account.user_pass Not Empty', 1001);
}
}
/**
* 获取错误信息
*/
public static function getError(){
$map = array(
'1001'=>'参数错误',
'5000'=>'服务器错误',
'5001'=>'数据解析失败',
);
}
}
############ 调用示例 ############
/**
* api地址及账号信息
*/
$api_host = 'http://xxx.xxx.com/api/v1';
$company_code = 'xxx';
$user_account = 'xxx';
$user_pass = 'xxx';
/**
* 实例化客户端
*/
$client = new OpenApi_Client($api_host, $company_code, $user_account, $user_pass);
/**
* 调用指定模块的接口
*/
//模块
$module = 'Common';
//服务接口
$service = 'demo';
//请求参数
$param = [
'param_1'=>'a',
'param_2'=>'b',
];
$re = $client->callService($module, $service, $param);
print_r($re);
die;
```