在PHP中创建正确的SOAP调用


Creating the right SOAP call in PHP

我在PHP中创建正确的SOAP调用时遇到问题。我已经在SOAPUI中测试了以下调用,它是有效的,但我已经尝试了从创建对象、数组到SOAPHeader的所有方法,似乎都无法得到正确的调用。以下是在SOAP UI中工作的请求:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="com.dtn.aghost.API.subscriptions">
    <soapenv:Header>
        <com:ServiceCredentials>
            <usernamePasswordCombo>
                <username>[username]</username>
                <password>[password]</password>
            </usernamePasswordCombo>
        </com:ServiceCredentials>
    </soapenv:Header>
    <soapenv:Body>
        <com:SubscriptionServiceIdList>
            <visible>1</visible>
        </com:SubscriptionServiceIdList>
    </soapenv:Body>
</soapenv:Envelope>

谢谢!

我有一些问题,因为您的请求几乎没有关于您已经尝试过的内容的信息。我建议您添加一些尝试使用的代码和一些查看过的资源,以获得这些信息。此外,如果你描述了你尝试的结果,这将有助于回答你的请求。比如有错误吗?如果有,你看到了什么错误?

关于SOAP客户端的PHP文档是获取连接到SOAP web服务的PHP代码示例的好地方。

在该页面的评论中,你会发现其他人使用过的代码示例。你可能会调整它以将其用于你的目的。

作为连接到SOAP web服务的代码示例

<?php
    $http_client = 'http://example.com/MyService.asmx?wsdl';
    $client = new SoapClient($http_client, array(
        'style' => SOAP_DOCUMENT,
        'use' => SOAP_LITERAL,
        'soap_version'=>SOAP_1_1,
        'trace' => 1,
        'connection_timeout' => 300));
    $params = array( 'username' => $user_name, 'password' => $password);
    $results = $client->myFunction($params);
    // Debug code can be added after this
?>

您需要做的另一件事是调试请求的结果。在上面的示例中,它将trace的值设置为1。这允许您调试已经发生的SOAP请求。您可以使用以下代码来完成此操作。

    // Debug code
    echo $client->__getLastRequestHeaders();
    echo $client->__getLastRequest()
    echo $client->__getLastResponseHeaders();
    echo $client->__getLastResponse();

PHP SoapClient getLastResponse

最后,您可以使用is_soap_fault来确定请求是否失败。PHP文档中有一些很好的代码示例,用于获取所发生的错误代码。