|
- <?php
- /**
- *
- * ============================================================================
- * * 版权所有 蜘蛛出行 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 倪宗锋
- * PhpStorm SoapService.php
- * Create By 2017/4/18 11:00 $
- */
-
-
- namespace backend\modules\hotel\models;
-
- use Yii;
- use backend\common\Utils;
- use common\components\zOfficeWechat;
-
- class SoapService
- {
- private $soap = null;
- private $send = '';
- private $getRe = '';
- private $version = '';
- private $bodyName = '';
- private $scTime = 0;
- private $data = '';
- private $RequestTypeName = '';
- private $user_id = 1;
-
- public function __construct($data, $RequestTypeName, $bodyName, $version = '')
- {
- libxml_disable_entity_loader(false);
- if (empty($version)) {
- $params = \Yii::$app->params['ctrip']['soap'];
- $version = $params['Version'];
- }
- $this->bodyName = $bodyName;
- $this->data = $data;
- $this->RequestTypeName = $RequestTypeName;
- $this->version = $version;
- }
-
- /**
- * Des:执行并返回结果
- * Name: exec
- * @param int $type 1:标准模式如mapping、订单推送 2:UpdateRoomQuantity(更新保留房数量) 3. 房价房态推送
- * @return array
- * @author 倪宗锋
- */
- public function exec($type = 1, $user_id = 1)
- {
- $this->user_id = $user_id;
- $startTime = microtime(TRUE);
- if ($type == 1) {
- $return = $this->doExecForCommon();
- } elseif ($type == 2) {
- $return = $this->doExecForRoomQuantity();
- } elseif ($type == 3) {
- $return = $this->doExecForPrice();
- } else {
- $return = $this->reTu(false, 'type is error');
- }
- $endTime = microtime(TRUE);
- $this->scTime = round(floatval($endTime - $startTime), 3);//时间差
- return $return;
- }
-
- /**
- * Des:标准模式调用并处理
- * Name: doExecForCommon
- * @return array
- * @author 倪宗锋
- */
- public function doExecForCommon()
- {
- $this->soap = \Yii::$app->soapClient;
- $this->setXml();
- if (Yii::$app->params['ctrip']['flag']) {
- $re = $this->soap->AdapterRequest(['requestXml' => $this->send]);
- $this->getRe = $re->AdapterRequestResult;
- $result = Utils::xml_to_array($this->getRe);
- } else {
- $result = '<>';
- }
- return $this->reTu(true, 'OK', $result);
- }
-
-
- /**
- * Des:房价房态推送
- * Name: doExecForCommon
- * @return array
- * @author 王雷刚
- * 2018-12-7 14:10:35
- */
- public function doExecForPrice()
- {
- $this->soap = \Yii::$app->soapClientPrice;
- $this->setXml();
- if (Yii::$app->params['ctrip']['flag']) {
- $re = $this->soap->AdapterRequest(['requestXml' => $this->send]);
- $this->getRe = $re->AdapterRequestResult;
- $result = Utils::xml_to_array($this->getRe);
- } else {
- $result = '<>';
- }
- return $this->reTu(true, 'OK', $result);
- }
-
- /**
- * User:Steven
- * Desc:更新保留房
- * @return array
- */
- public function doExecForRoomQuantity()
- {
- $this->soap = \Yii::$app->soapClientRemain;
- $this->setXmlNs();
- if (Yii::$app->params['ctrip']['flag']) {
- $re = $this->soap->RoomQuantityReceive(['receiveRoomQuantityXml' => $this->send]);
- $this->getRe = $re->RoomQuantityReceiveResult;
- $result = Utils::xml_to_array($this->getRe);
- } else {
- $result = '<>';
- }
- return $this->reTu(true, 'OK', $result);
- }
-
-
- /**
- * Des:设置xml报文
- * Name: setXml
- * @author 倪宗锋
- */
- private function setXml()
- {
- $params = \Yii::$app->params['ctrip']['soap'];
- $timeStamp = date("Y-m-d H:i:s", time());
- $xml = "<Request>";
- $xml .= "<HeaderInfo UserID=\"" . $params['UserID'] . "\" RequestorId=\"" . $params['RequestorId'] .
- "\" AsyncRequest=\"" . $params['AsyncRequest'] . "\" TimeStamp=\"$timeStamp\"><Authentication UserName=\"" .
- $params['UserName'] . "\" Password=\"" . $params['Password'] . "\" /><RequestType Name=\"$this->RequestTypeName\" Version=\"" . $this->version . "\" /></HeaderInfo><{$this->bodyName}>";
- $xml .= Utils::data_to_xml($this->data);
- $xml .= "</{$this->bodyName}></Request>";
- $this->send = $xml;
- }
-
- /**
- * Des:设置带有命名空间的xml报文
- * Name: setXml
- * @author 倪宗锋
- */
- private function setXmlNs()
- {
- $params = \Yii::$app->params['ctrip']['soap'];
- $timeStamp = date("Y-m-d\TH:i:s+08:00", time());
- $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
- $xml .= "<q1:Request xmlns:q1=\"http://soa.ctrip.com/hotel/vendor/RoomQuantity/v1\">";
- $xml .= "<q1:HeaderInfo UserID=\"" . $params['UserID'] . "\" RequestorId=\"" . $params['RequestorId'] .
- "\" AsyncRequest=\"" . $params['AsyncRequest'] . "\" TimeStamp=\"$timeStamp\"><q1:Authentication UserName=\"" .
- $params['UserNameRemain'] . "\" Password=\"" . $params['PasswordRemain'] . "\" /><q1:RequestType Name=\"$this->RequestTypeName\" Version=\"" . $params['VersionRemain'] . "\" /></q1:HeaderInfo><q1:{$this->bodyName}>";
- $xml .= Utils::data_to_xml_ns($this->data, 'q1:');
- $xml .= "</q1:{$this->bodyName}></q1:Request>";
- $this->send = $xml;
- }
-
- /**
- * Des:统一返回格式
- * Name: reTu
- * @param $flag
- * @param string $msg
- * @param string $data
- * @return array
- * @author 倪宗锋
- */
- private function reTu($flag, $msg = '', $data = '')
- {
- return [
- 'flag' => $flag,
- 'msg' => $msg,
- 'data' => $data
- ];
- }
-
- public function __destruct()
- {
- $string = date('Y-m-d H:i:s', time()) . ' ' . $this->version . PHP_EOL;
- $string .= "send:【{$this->user_id}】" . $this->send . PHP_EOL;
- $string .= "get:【{$this->user_id}】" . $this->getRe . PHP_EOL;
- $string .= 'time:' . $this->scTime . 's' . PHP_EOL . PHP_EOL;//时间差
- $result = Utils::xml_to_array($this->getRe);
- if (empty($result) || $result['RequestResult']['ResultCode'] != 0) {
- $arr = array(
- "agentid" => 1000002,
- "title" => '推送失败:',
- "msg" => $this->scTime . ':' . $this->send,
- "touser" => 'wangxj|shifp|yuw',
- );
- zOfficeWechat::sendMsg($arr);
-
- }
- file_put_contents(__DIR__ . '/../../../runtime/logs/soap/' . date('Y-m-d') . '.log', $string, FILE_APPEND);
- }
- }
|