【问题标题】:How to work with SOAP web service and it's XML in php如何使用 SOAP Web 服务及其在 php 中的 XML
【发布时间】:2016-09-30 07:30:02
【问题描述】:

我想在我的 PHP 开发的项目中调用一个 Web 服务的函数。这是我的代码:

$wsdl="http://x.x.x.x:8090/charge/services/Amount?wsdl";

$client = new SoapClient($wsdl) or die("Error");

$chargeAmountArray  = array('UserIdentifier' => $number ,
                            'data' => array(
                            'description' => array("Channel=test|".$Origin),
                            'currency' => NULL,
                            'code' => $code),
                            'refrence' => $refcode);

$header = new SoapHeader('servicekey', $servicekey);

$client->__setSoapHeaders($header);

$res = $client->__call('chargeAmount',$chargeAmountArray); 

return $res->return;

我用 tcpdump 看到了我的 xml 代码,它是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.csapi.org/schema/parlayx/payment/amount_charging/v4_0/local" xmlns:ns2="servicekey">
    <SOAP-ENV:Header>
        <ns2:e0ce5ed56d7c4d60/>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:chargeAmount/>
        <param1>
            <item>
                <key>description</key>
                <value>yChannel=test|30733</value>
            </item>
            <item>
                <key>currency</key>
                <value/>
            </item>
            <item>
                <key>code</key>
                <value>MOBDY</value>
            </item>
        </param1>
        <param2>1PN1mROoZop2hAy</param2>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如您所见,UserIdentifier 未传递,refrence 参数在 XML 中没有名称。

【问题讨论】:

    标签: php xml web-services soap


    【解决方案1】:

    我在这里看到一些错误。

    constructor signature for the SoapHeader class 显示第一个参数是命名空间声明,您似乎没有设置它。

    另外,SoapClient::__call() is deprecated;相反,您应该直接调用该方法:

    $res = $client->chargeAmount($chargeAmountArray);
    

    SoapClient::__call() 需要数组中的参数,其中包含您未提供的关键“参数”。

    最后,您应该使用try/catch 进行一些错误检查,以防因任何原因无法建立连接。

    【讨论】:

    • 我不明白这句话是什么意思:SoapHeader 类的构造函数签名显示第一个参数是命名空间声明,您似乎没有设置它。我使用了SoapClient::__call(),因为我使用了nusoap。我也使用了try/catch 并以这种方式调用函数$res = $client-&gt;__call('chargeAmount',array('parameters'=&gt;$chargeAmountArray)); ,我得到了这个错误:Fatal error: Uncaught SoapFault exception: [soapenv:Server] unknown
    • 构造函数是使用new SoapHeader()时调用的函数。你叫错了。并且不要使用SoapClient__call(),它已被弃用。
    猜你喜欢
    • 2012-01-29
    • 1970-01-01
    • 2012-04-02
    • 2012-02-22
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    相关资源
    最近更新 更多