使用php soap client自定义请求获取webservice封装的数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zsl10/article/details/79458817

github地址

soapClient-php

场景

公司业务需要调外部公司使用.net webservice封装的接口,因此使用php(版本:5.6)创建soap client调取接口。

外部公司规定的传输数据格式

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mob="https://www.mobile88.com" xmlns:mhp="http://schemas.datacontract.org/2004/07/MHPHGatewayService.Model">
    <soapenv:Header/>
    <soapenv:Body>
        <mob:EntryPageFunctionality>
            <mob:requestModelObj>
                <mhp:MerchantCode>M00578</mhp:MerchantCode>
                <mhp:PaymentId>234</mhp:PaymentId>
                <mhp:RefNo>100020180301105210072365</mhp:RefNo>
                <mhp:Amount>1.00</mhp:Amount>
                <mhp:Currency>MYR</mhp:Currency>
                <mhp:ProdDesc>test</mhp:ProdDesc>
                <mhp:UserName>test</mhp:UserName>
                <mhp:UserEmail>[email protected]</mhp:UserEmail>
                <mhp:UserContact>15323781483</mhp:UserContact>
                <mhp:BarcodeNo>123456789123456789</mhp:BarcodeNo>
                <mhp:Lang>UTF-8</mhp:Lang>
                <mhp:Remark>test</mhp:Remark>
                <mhp:SignatureType>SHA256</mhp:SignatureType>
                <mhp:Signature>0cb7d510a4e6599118a54bbb3b03f22bc2c63e404fa1642ccdebf34b679aff77</mhp:Signature>
            </mob:requestModelObj>
        </mob:EntryPageFunctionality>
    </soapenv:Body>
</soapenv:Envelope>

具体实现

1、规定传输的数据格式需要用到两个自定义命名空间,因此需重写php库\SoapClient

class MalIpaySoapClient extends \SoapClient {

    private $newRequest;

    function __doRequest($request, $location, $action, $version, $one_way = 0) {
        $this->newRequest = str_replace('SOAP-ENV', 'soapenv', $request);
        $this->newRequest = str_replace('xmlns:ns1="https://www.mobile88.com"', 'xmlns:mob="https://www.mobile88.com" xmlns:mhp="http://schemas.datacontract.org/2004/07/MHPHGatewayService.Model"', $this->newRequest);
        return parent::__doRequest($this->newRequest, $location, $action, $version);
    }

    function __getLastRequest() {
        return $this->newRequest;
    }

}

2、传输的数据都要添加命名空间
需要将php数组进行格式化输出:

    /**
     * 数组转xml
     * 
     * @param string $startTag  起始标记
     * @param array $arr  数据
     * @param string $endTag  结束标记
     * @param string $prefix  命名空间前缀(主要这对soap data)
     * @return string
     * @author zsl
     */
     function arrayToXml($startTag, $arr, $endTag,$prefix='') {
        $xml = $startTag;
        foreach ($arr as $key => $value) {
            $xml .= "<".$prefix.":" . $key . ">" . $value . "</" .$prefix.":". $key . ">";
        }
        $xml .= $endTag;
        return $xml;
    }

如下调用:

        $startTag = "<mob:EntryPageFunctionality xmlns:mob='https://www.mobile88.com' xmlns:mhp='http://schemas.datacontract.org/2004/07/MHPHGatewayService.Model' >   <mob:requestModelObj>";
        $endTag = "</mob:requestModelObj>   </mob:EntryPageFunctionality>";
        $startTag = "<mob:EntryPageFunctionality>  <mob:requestModelObj>";
        $endTag = "</mob:requestModelObj> </mob:EntryPageFunctionality>";
        arrayToXml($startTag, $data, $endTag, 'mhp');

猜你喜欢

转载自blog.csdn.net/zsl10/article/details/79458817