<?php

/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2017/3/13
 * Time: 15:17
 */
class roomOrderUtils
{
    public $ctrip_url = 'http://127.0.0.1:20083/Ctrip/Service.php?wsdl';
    public $UserID = "123456";  //合作方编码
    public $RequestorId = "Ctrip.com";   //请求对象的编码,后备字段， 暂不处理，默认值Ctrip.com
    public $AsyncRequest = "false";      //是否异步请求
    public $UserName = "Zhilianjishuzhuanshu";    //用户名
    public $Password = "zhilianzhuanshu11!!";    //密码
    public $Version = "1.0";    //API版本号
    public $url = "";   //请求URL

    /**
     * User:Steven
     *
     * Desc:发生soap请求
     * @param $request_fun
     * @param $param
     * @return Exception|SoapFault
     */
    public function soapRequest($request_fun, $param)
    {
        $soap = new SoapClient($this->ctrip_url, array('soap_version' => SOAP_1_2, 'trace' => TRUE, 'exceptions' => FALSE));
        try {
            $response = $soap->{$request_fun}($param);
            return $response;
        } catch (SoapFault $sf) {
            return $sf;
            /*print ($soap->__getLastRequest());
            print ($soap->__getLastResponse());*/
        }
    }

    /**
     * User:Steven
     *
     * Desc:发送post请求（xml）
     * @param $url
     * @param $xmlData
     * @return mixed
     */
    public function xml_post_request($url, $xmlData)
    {
        $header[] = "Content-type: text/xml";        //定义content-type为xml,注意是数组
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            print curl_error($ch);
        }
        curl_close($ch);
        return $response;
    }

    public function execute($request, $session = null)
    {
        $this->data = $this->format == 'json' ? json_encode($request) : $this->xml_encode($request);
        $this->Url = 'http://' . str_replace(array('http://', 'https://'), '', $this->Url);
        $postUrl = $this->Url . "?method={$this->requestMethod}&data=" . urlencode($this->data);
        return $this->request_by_curl($postUrl);
    }

    /**
     * User:Steven
     *
     * Desc:编辑符合携程需要的XML格式
     * @param $data 请求体
     * @param $RequestTypeName 请求方法名称
     * @return string
     */
    public function xml_encode($data, $RequestTypeName)
    {
        $timeStamp = date("Y-m-d H:i:s", time());
        $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
        $xml .= "<request>";
        $xml .= "<HeaderInfo UserID=\"$this->UserID\" RequestorId=\"$this->RequestorId\" AsyncRequest=\"$this->AsyncRequest\" TimeStamp=\"$timeStamp\"><Authentication UserName=\"$this->UserName\" Password=\"$this->Password\"/><RequestType Name=\"$RequestTypeName\" Version=\"$this->Version\"/></HeaderInfo>  ";
        $xml .= $this->data_to_xml($data);
        $xml .= "</request>";
        return $xml;
    }

    /**
     * User:Steven
     *
     * Desc:将xml转换为数组
     * @param $xml
     * @return mixed
     */
    public function xml_to_array($xml)
    {
        $ob = simplexml_load_string($xml);
        $json = json_encode($ob);
        $array = json_decode($json, true);
        return $array;
    }

    /**
     * User:Steven
     *
     * Desc:将数组转化成xml
     * @param $data
     * @return string
     */
    public function data_to_xml($data)
    {
        if (is_object($data)) {
            $data = get_object_vars($data);
        }
        $xml = '';
        foreach ($data as $key => $val) {
            if (is_null($val)) {
                $xml .= "<$key/>\n";
            } else {
                if (!is_numeric($key)) {
                    $xml .= "<$key>";
                }
                $xml .= (is_array($val) || is_object($val)) ? $this->data_to_xml($val) : $val;
                if (!is_numeric($key)) {
                    $xml .= "</$key>";
                }
            }
        }
        return $xml;
    }
}


