带有 wsdl 的 PHP SOAP 请求不起作用,它给了我一个错误"SOAP-ERROR: Encoding: object has no 'phoneNumber' property"



我需要帮助使用PHP中的WSDL将请求数据发送到SOAP API。我已经为PHP安装了Soapclient扩展名,并且工作正常。这是脚本:

<?php
$client = new SoapClient($wsdlUrl, array('username' => USER_NAME, 'password'  => PASSWORD,'trace'=>'1','exception'=>0));
$method = "templateSMS";
$xmlr = new SimpleXMLElement("<Sms></Sms>");
$xmlr->addChild("phoneNumber",PHONE_NUMBER);
$xmlr->addChild('message', 'Hi how are you');
$xmlr->addChild('unicodeMessage', 0);
$xmlr->addChild('sms_type_id', 1);
$xmlr->addChild('senderId', SENDER_ID);
$xmlr->addChild('notify', 0);
$xmlr->addChild('priority', 1);
$xmlr->addChild('vbApp', 'SoapRequest');
$xmlr->addChild('vbIdTime', time());
$params = new stdClass();
$params->xml = $xmlr->asXML();
try {    
   $data = $client->$method($params);
   var_dump($data);
}catch (SoapFault $e) {    
   echo "<pre> Exceptions Break  :";
     print_r($e);
   echo "</pre>";
}
?>

当我在浏览器中运行代码时,它会给我一个错误" soap-error:编码:对象没有'PhoneNumber'属性'属性"

这是错误日志,可能更有帮助地解决我的问题:

 Exceptions Break  :SoapFault Object
(
[message:protected] => SOAP-ERROR: Encoding: object has no 'phoneNumber' property
[string:Exception:private] => 
[code:protected] => 0
[file:protected] => C:xampphtdocssoapDemoopt3.php
[line:protected] => 18
[trace:Exception:private] => Array
    (
        [0] => Array
            (
                [file] => C:xampphtdocssoapDemoopt3.php
                [line] => 18
                [function] => __call
                [class] => SoapClient
                [type] => ->
                [args] => Array
                    (
                        [0] => templateSMS
                        [1] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [xml] => 
    Hi how are you0101SoapRequest1480835353
                                    )
                            )
                    )
            )
        [1] => Array
            (
                [file] => C:xampphtdocssoapDemoopt3.php
                [line] => 18
                [function] => templateSMS
                [class] => SoapClient
                [type] => ->
                [args] => Array
                    (
                        [0] => stdClass Object
                            (
                                [xml] => 
      Hi how are you0101SoapRequest1480835353
                            )
                    )
            )
    )
[previous:Exception:private] => 
[faultstring] => SOAP-ERROR: Encoding: object has no 'phoneNumber' property
[faultcode] => Client
[faultcodens] => http://schemas.xmlsoap.org/soap/envelope/
)

支持团队为我提供了一个示例XML代码以传递请求格式,这是示例XML请求格式,请求应与下面相同:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:templateSMS" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="urn:ElbaridTNS" xmlns:ns3="namespace" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<ns3:AuthHeader>
<username>username </username>
<password>password</password>
</ns3:AuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:templateSMS>
<Sms xsi:type="ns2:Sms"><phoneNumber xsi:type="xsd:string">xxxxxx</phoneNumber>
<message xsi:type="xsd:string">test SMS </message>
<unicodeMessage xsi:type="xsd:string">test SMS</unicodeMessage>
<sms_type_id xsi:type="xsd:string">1</sms_type_id>
<notify xsi:type="xsd:string">0</notify>
<senderId xsi:type="xsd:string">SenderId</senderId>
<priority xsi:type="xsd:string">2</priority>
<vbApp xsi:type="xsd:string">SoapRequest</vbApp>
<vbIdTime xsi:type="xsd:string">20161130101112</vbIdTime>
<destinationPort xsi:type="xsd:string">-1</destinationPort></Sms>
</ns1:templateSMS>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

响应应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:templateSMS" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:templateSMSResponse>
<templateSMSResponse xsi:type="xsd:string">0#!#200#!#http://198.101.210.203/Soap/service/elbarid|http://212.98.137.180/Soap/service/elbarid#!#SenderId </templateSMSResponse>
</ns1:templateSMSResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

有人可以帮助我解决问题吗?我也尝试使用卷发,但我发现同样的错误响应。

谢谢:)

您需要通过作为参数。

$params = array('Sms'=>array('phoneNumber'=>'123456879','message'=>'test')); //add all params. in Sms array.

然后调用方法

$data = $client->$method($params);

相关内容

最新更新