【问题标题】:PHP SOAP request with wsdl not working, it gives me an error "SOAP-ERROR: Encoding: object has no 'phoneNumber' property"带有 wsdl 的 PHP SOAP 请求不起作用,它给了我一个错误“SOAP-ERROR: Encoding: object has no 'phoneNumber' property”
【发布时间】:2017-04-18 19:04:27
【问题描述】:

我需要帮助才能使用 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: Encoding: object has no 'phoneNumber' property

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

 Exceptions Break  :SoapFault Object
(
[message:protected] => SOAP-ERROR: Encoding: object has no 'phoneNumber' property
[string:Exception:private] => 
[code:protected] => 0
[file:protected] => C:\xampp\htdocs\soapDemo\opt3.php
[line:protected] => 18
[trace:Exception:private] => Array
    (
        [0] => Array
            (
                [file] => C:\xampp\htdocs\soapDemo\opt3.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:\xampp\htdocs\soapDemo\opt3.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>

谁能帮我解决我的问题?我也尝试使用 CURL,但我得到了同样的错误响应。

谢谢:)

【问题讨论】:

    标签: php xml web-services wsdl soap-client


    【解决方案1】:

    你需要传递数组作为参数。

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

    然后调用方法

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

    【讨论】:

    • 感谢您的回复....但我仍然收到同样的错误:SOAP-ERROR: Encoding: object has no 'phoneNumber' property
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多