公司项目需要通过soap请求ERP软件获得返回数据,但是目前存在一个问题,当请求ERP次数过多的时候,ERP服务器会拒绝访问,以至于后续业务进行不下去.
联系过ERP机房的人无疾而终.
后来发现,通过不同的端口又可以访问ERP服务器.
国哥做了一个ERP访问代理服务,当第一方案不通的时候,采用第二方案.
先来看ERP请求的方法
public function requestGetRecordSet($aCmdText)
{
//soap初始化
$this->connect();
//加密salt
$aCheckCode = $this->cryption->encrypt($this->checkSalt . $this->cryption->checkCode($aCmdText));
//开启代理走代理,没开启走之前
if ($this->proxy->isProxy()) {
//魔术方法将方法名和参数传入代理服务
$rawData = $this->proxy->getRecordSet($aCmdText, $aCheckCode);
} else {
$rawData = $this->client->getRecordSet($aCmdText, $aCheckCode);
if (empty($rawData)) {
throw new SoapException(SoapException::E_401);
}
}
return $this->formatRecordSetData($this->formatXMLData($rawData));
}
上面提到的代理服务是怎么接收方法名和参数的呢?
其实代理客户端除了初始化的基本逻辑外什么方法都没有,都是通过__call将外部服务的方法和参数存储起来
class SoapProxy
{
protected $proxyState = false;
protected $erpConf;
protected $auth = 'yRsIx480yhptRVR4VKaXFJgNcjMfxYZ0';
//代理要访问的服务器地址
protected $proxyUrl = 'https://boshi.unionglasses.com:8081/Soap/';
public function setErpConf($erpConf)
{
if (isset($erpConf['proxy'])) {
if (is_string($erpConf['proxy'])) {
if (stripos($erpConf['proxy'], 'http') === 0) {
$this->enableProxy();
$this->proxyUrl = $erpConf['proxy'];
}
} elseif (is_bool($erpConf['proxy'])) {
$this->enableProxy();
}
}
$this->erpConf = [
'domain' => $erpConf['domain']
];
}
public function enableProxy($enable = true)
{
$this->proxyState = $enable;
}
public function isProxy()
{
return $this->proxyState;
}
//魔术方法获得方法名和参数
public function __call($method, $args)
{
$postData = [];
$postData['soapInf'] = $method;
$postData['soapParam'] = $args;
$postData['access_token'] = $this->auth;
$postData['erpConf'] = $this->erpConf;
return $this->request($postData);
}
//curl请求带入方法名和参数,获得返回值后,返回给外部服务
public function request($postData)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->proxyUrl);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
$res = curl_exec($ch);
curl_close($ch);
$rawData = '';
if (! empty($res)) {
$res = json_decode($res, true);
if (isset($res['errcode']) && $res['errcode'] === 0 && isset($res['rawData'])) {
$rawData = $res['rawData'];
if (empty($rawData)) {
throw new SoapException(SoapException::E_401);
}
}
}
if (empty($rawData)) {
throw new SoapException(SoapException::E_402);
}
return $rawData;
}
}
网友评论