公司的一个微服务系统想访问另一个系统,另一个系统是用的ecstore框架基础上做的二次开发,由于之前外部接口都是用openapi实现的,但是考虑到我们这个系统是内部使用,不想搞这么麻烦,所以就想用另一种方式实现。
1,新增api.xml
所有ECSTORE系统提供的API都需要在api.xml中进行注册,才可以被外部系统调用
api.xml中只进行API接口注册,不提供openapi接口注册,openapi还是和旧版本一样在services.xml中进行注册
修改api.xml后 cmd update 更新api.xml中注册的API接口,(也可以登录后台应用中心-手动更新)
2,创建被调用类和方法
注意文件类的名称和api.xml的配置一样
3,另一台服务器测试调用
需要用到的方法
//生成签名
public function sign($params, $token) {
return strtoupper(md5(strtoupper(md5($this->assemble($params))).$token));
}
//拼接参数
public function assemble($params) {
if(!is_array($params)) return null;
ksort($params, SORT_STRING);
$sign = '';
foreach($params AS $key=>$val){
if(is_null($val)) continue;
if(is_bool($val)) $val = ($val) ? 1 : 0;
$sign .= $key . (is_array($val) ? $this->assemble($val) : $val);
}
return $sign;
}
//curl请求
public function docurl($url,$post='',$cookie='', $returnCookie=0){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_REFERER, "http://www.baidu.com");
if($post) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
}
if($cookie) {
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
}
curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if (curl_errno($curl)) {
return curl_error($curl);
}
curl_close($curl);
if($returnCookie){
list($header, $body) = explode("\r\n\r\n", $data, 2);
preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
$info['cookie'] = substr($matches[1][0], 1);
$info['content'] = $body;
return $info;
}else{
return $data;
}
}
开始测试调用