处理基于WSDL的服务器时的PHP SoapClient

时间:2013-10-02 16:29:14

标签: php soap wsdl

我试图弄清楚如何使用SoapClient处理PHP中的WSDL,我感到很害怕:

SoapFault exception: [soap:Server] Server was unable to process request. ---> Object reference not set to an instance of an object.

以下是我尝试使用的功能:

CheckInventoryResponse CheckInventory(CheckInventory $parameters)

结构:

string(47) "struct CheckInventory {
   InventoryRequest ir;
}"

我想做什么:

class PhpCheckInventory {
    public $ir;
}

$client = new SoapClient($wsdl);
$header = new SOAPHeader($wsdl, 'Authentication', array('Username' => $username, 'Password' => $password));
$client->__setSoapHeaders($header);

try {
    $parameter1 = new PhpCheckInventory();
    $parameter2 = new SoapParam($parameter1, 'CheckInventory');
    $result = $client->CheckInventory($parameter2);
} catch (SoapFault $exception) {
    echo $exception;      
}

1 个答案:

答案 0 :(得分:1)

我最终得到了它的工作。问题是双重的,身份验证不起作用,WSDL期待数组而不是对象。让它工作的关键是回显它正在生成的__getLastRequest()XML。

以下是代码:

<?php
ini_set('display_errors', true); 
ini_set("soap.wsdl_cache_enabled", "0"); 
error_reporting(E_ALL);

// ns, wsdl, username, password goes here.

$client = new SoapClient($wsdl, array('trace' => 1, 'exceptions' => 0));
$auth = new stdClass();
$auth->Username = $username;
$auth->Password = $password;
$header = new SOAPHeader($ns, 'Authentication', $auth, false);
$client->__setSoapHeaders($header);

echo "Display Funcs\n";
var_dump($client->__getFunctions()); 
echo "Diaplay Types\n";
var_dump($client->__getTypes()); 

try {
    $partIds = array('PartId' => "name");
    $parts = array('Part' => $partIds);
    $partList = array('PartList' => $parts);
    $response = $client->CheckInventory(array('ir' => $partList));
    echo $client->__getLastRequest() . "\n";
    var_dump($response);
} catch (SoapFault $exception) {
    echo $exception;      
}
?>

以下是它生成的XML:

<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns1="removed">
    <SOAP-ENV:Header>
        <ns1:Authentication>
            <ns1:Username>name</ns1:Username>
            <ns1:Password>pass</ns1:Password>
        </ns1:Authentication>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:CheckInventory>
            <ns1:ir>
                <ns1:PartList>
                    <ns1:Part>
                        <ns1:PartId>name</ns1:PartId>
                    </ns1:Part>
                </ns1:PartList>
            </ns1:ir>
        </ns1:CheckInventory>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>