具有登录功能的 SOAP 界面


Soap Interface with login

我已经使用了 php.net 并遵循了在php中使用SoapClient创建SOAP请求的文档,但是我无法弄清楚如何在正确的位置使用标头正确格式化请求。 这会导致我的脚本运行时出现"使用的无效的自选类型"错误。

我需要形成的示例请求是:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Header>
    <ServiceAuthHeader xmlns="http://voicepad.com/DataServices_Vivek/DataServices">
      <Username>string</Username>
      <Password>string</Password>
    </ServiceAuthHeader>
  </soap12:Header>
  <soap12:Body>
    <GetData xmlns="http://voicepad.com/DataServices_Vivek/DataServices">
      <sQueryType>string</sQueryType>
      <StartDate>string</StartDate>
      <EndDate>string</EndDate>
      <PhoneNumber>string</PhoneNumber>
    </GetData>
  </soap12:Body>
</soap12:Envelope>

我最后一次尝试使用 php 发出请求是:

$client = new SoapClient("url", array( 'trace' => TRUE, 'exceptions'=>0 ));
$header = new SoapHeader( 'soap12', 'ServiceAuthHeader',  array('Username' => "$s_uname", 'Password' => "$s_pass"));
$client->__setSoapHeaders(array($header));
$result = $client->GetData(array('PhoneNumber'=>'##########'));
print_r($result);

此代码生成的请求是这样的:

<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://voicepad.com/DataServices_Vivek/DataServices" xmlns:ns2="soap12">
    <soap-env:header>
        <ns2:serviceauthheader>
            <item>
                <key>
                    Username
                </key>
                <value>
                    *********
                </value>
            </item>
            <item>
                <key>
                    Password
                </key>
                <value>
                    *********
                </value>
            </item>
        </ns2:serviceauthheader>
    </soap-env:header>
    <soap-env:body>
        <ns1:getdata>
            <ns1:phonenumber>
                ###########
            </ns1:phonenumber>
        </ns1:getdata>
    </soap-env:body>
</soap-env:envelope>

如您所见,此请求是错误的,但它也是我能够获得的关闭。 这是我唯一的一次试用,我至少获得了用户名和密码以显示在请求的标头中。 我迷路了,有人请帮忙!

如果您正在查看 SoapHead PHP.net 文档的第一条评论,那么除了您还需要正确设置 XML 命名空间之外,该示例正是您所需要的。

必须知道XML有几个规则来标记元素属于它们,这对XML处理器无关紧要。

因此,这是针对您的问题的调整示例。我希望它对您有用:

$client = new SoapClient(WSDL,array());
$auth = array(
     'UserName'=>'USERNAME',
     'Password'=>'PASSWORD',
     );
$header = new SoapHeader(
    'http://voicepad.com/DataServices_Vivek/DataServices',
    'ServiceAuthHeader',
    $auth,
    false
);
$client->__setSoapHeaders($header);