douqiang5809 2013-10-02 16:29
浏览 16
已采纳

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

I am trying to figure out how to deal with a WSDL in PHP using SoapClient and I am getting the dreaded:

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

Here is the function I am trying to use:

CheckInventoryResponse CheckInventory(CheckInventory $parameters)

The struct:

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

What I trying to do:

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条回答 默认 最新

  • doulao1934 2013-10-03 13:40
    关注

    I ended up getting it working. The problem was two-fold, the Authentication wasn't working and the WSDL was expecting arrays instead of objects. The key to getting it working was to echo the __getLastRequest() XML it was producing.

    Here is the code:

    <?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
    ";
    var_dump($client->__getFunctions()); 
    echo "Diaplay Types
    ";
    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() . "
    ";
        var_dump($response);
    } catch (SoapFault $exception) {
        echo $exception;      
    }
    ?>
    

    Here is the XML it produces:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!