在一个项目中需要对接一个短信功能,给的是WebService接口,给了java的demo,但是没有PHP的demo,在一些简单的传参时候可以使用Soap来实现对接,但是当一些复杂传参的时候,soap无法传递过去,因此还是可以使用curl方式来实现对接。
对于简单的参数传递可以使用以下方式实现,先在服务器上面开启soap扩展,重新启动apache。
//get authorization
public static function getAuthorization(){
$data = self::jiami(); //获取加密数据
//WebService接口数据调用 需要开启soap扩展
$client = new \SoapClient("http://127.0.0.1:8888/ws/v1?wsdl");
// print_r($client->__getFunctions()); //查看对外的方法 以及参数
// print_r($client->__getTypes()); // 查看类型
// 组织参数 根据接口具体提供参数组织对应参数
$param = [
'userCode' => $data['userCode'],
'password' => $data['password'],
'unixtick' => $data['unixtick']
];
$rst =$client->authorization($param);//请求入口函数为【authorization】
$rst = json_encode($rst);
$rst = json_decode($rst ,true);
if ($rst['return']['code'] == 0){
return $rst['return']['result']['token'];
}
}
其中wsdl为对接方提供一种XML Application,可以用来查看对接方的接口,同时调用的时候也需要用到。
对用复杂的传参,例如需要传递header,或者传递数组的时候遇到问题可以使用curl。
public static function sendMsg($content,$receiveNumber)
{
$curl = curl_init();
$token = self::getAuthorization();
$xml ='<?xml version="1.0" encoding="utf-16"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<token xmlns="http://v1.ws.api.service.yxt.gooben.com/">'.$token.'</token>
</soap:Header>
<soap:Body>
<submitTask xmlns="http://v1.ws.api.service.yxt.gooben.com/">
<details xmlns="">
<content>'.$content.'</content>
<receiveNumber>'.$receiveNumber.'</receiveNumber>
<transactionId />
</details>
<atTime xmlns="" />
<itfOptions xmlns="">
<itfSmsOption>
<content />
<extCode />
<hasSms>true</hasSms>
<routeName />
</itfSmsOption>
</itfOptions>
</submitTask>
</soap:Body>
</soap:Envelope>';
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://127.0.0.1:8888/ws/v1?wsdl',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>$xml,
CURLOPT_HTTPHEADER => array(
'Content-Type: charset=UTF-8'
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}