使用SoapClient类的PHP SOAP调用问题

时间:2014-09-29 13:43:23

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

我很难坚持创建一个使用此WSDL的客户端。 (在使用PHP中的web服务时,我是一个新手。)(出于保密原因,我必须创建一个副本XML。我希望你能理解。)

http://brique.in/wsdl.xml

我使用以下代码打印出函数和类型:

$client = new SoapClient("http://brique.in/wsdl.xml");
var_dump($client->__getFunctions()); 
var_dump($client->__getTypes()); 

我正在尝试编写php代码来调用方法 inwardProcess 。它接受XML文件作为输入。 我尝试了以下方法:

$xmlInput = htmlentities('<XML><Refno>H9999999</Refno><Type>getDetails</Type><UserID>BO</UserID></XML>');
$result = $client->__soapCall("inwardProcess", array($xmlInput));

它不起作用。在查看了WSDL规范之后,我也试过了,

$xmlInput = htmlentities('<XML><Refno>H9999999</Refno><Type>getDetails</Type><UserID>BO</UserID></XML>');
class inwardProcess {
    function inwardProcess($xmlInput) 
    {
        $this->xmlInput = $xmlInput;
    }
}
$inwardProcess = new inwardProcess($xmlInput);
$webservice = new SoapClient($url, $soap_options);
echo "Attempting Inward<br/>";
try {
    var_dump($webservice->__getTypes()); 
    //I also tried passing just $inwardProcess Object in place of array($inwardProcess)
    $result = $webservice->__soapCall("inwardProcess", array($inwardProcess));
    var_dump($result); 
} catch (SOAPFault $f) {
    echo "SOAPFault".$f;
}

我一直收到错误

Server was unable to process request. ---> Object reference not set to an instance of an object

不知怎的,我无法解决这个问题。任何帮助都会受到高度赞赏,因为我处于截止日期。

1 个答案:

答案 0 :(得分:0)

当结构复杂如下:

<s:element name="inwardProcess">
    <s:complexType>
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="xmlInput">
                <s:complexType mixed="true">
                    <s:sequence>
                        <s:any/>
                    </s:sequence>
                </s:complexType>
            </s:element>
        </s:sequence>
    </s:complexType>
</s:element>

和响应一样

<s:element name="inwardProcessResponse">
    <s:complexType>
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="inwardProcessResult">
                <s:complexType mixed="true">
                    <s:sequence>
                        <s:any/>
                    </s:sequence>
                </s:complexType>
            </s:element>
        </s:sequence>
    </s:complexType>
</s:element>

您必须为每个结构创建一个类。在这种情况下,它将是:

class xmlInput
{
    public $any = null;
    public function __construct($any)
    {
        $this->any = $any;
    }
}

class inwardProcess
{
    public $xmlInput = null;
    public function __construct($xmlInput)
    {
        $this->xmlInput = $xmlInput;
    }
}
class inwardProcessResponse
{
    public $inwardProcessResult = null;
    public function __construct($inwardProcessResult)
    {
        $this->inwardProcessResult = $inwardProcessResult;
    }
}

最后打电话给...

$xmlInput = new xmlInput('<XML><Refno>H9999999</Refno><Type>getDetails</Type><UserID>BO</UserID></XML>');
$inwardProcess = new inwardProcess($xmlInput);
$soap_options = array(
    'trace'       => 1,     // traces let us look at the actual SOAP messages later
    'exceptions'  => 1 );
$url = "<WSDL URL>";
$client = new SoapClient($url, $soap_options);
try {
    $result = $client->__soapCall("inwardProcess", array($inwardProcess));
    echo htmlentities($result->inwardProcessResult->any);
} catch (SOAPFault $f) {
    echo "-1";
}

工作!