PHP SoapClient请求

时间:2009-11-24 14:16:28

标签: php soap request

我正在尝试使用this WSDL向新闻稿服务发送SOAP请求。

这是我的PHP:

$client = new SoapClient($wsdl_url, array(
    'login' => 'myusername',
    'password' => 'mypassword',
    'trace' => true
));

$client->AddSubscriber(
    new SoapParam('MyFirstName', 'FirstName'),
    new SoapParam('MyLastName', 'LastName'),
    new SoapParam('myemail@someaddress.com', 'Email')
);

我得到了例外:

End element 'Body' from namespace 'schemas.xmlsoap.org/soap/envelope/' expected. Found element 'LastName' from namespace ''. Line 2, position 156.

以下是AddSubscriber服务的期望:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AuthHeader xmlns="admin.ekeryx.com">
      <Username>string</Username>
      <Password>string</Password>
      <AccountID>string</AccountID>
    </AuthHeader>
  </soap:Header>
  <soap:Body>
    <AddSubscriber xmlns="admin.ekeryx.com">
      <subscriber>
        <ID>string</ID>
        <FirstName>string</FirstName>
        <LastName>string</LastName>
        <Email>string</Email>
      </subscriber>
      <overwritable>boolean</overwritable>
    </AddSubscriber>
  </soap:Body>
</soap:Envelope>

这是发送的内容:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="tempuri.org/">
    <SOAP-ENV:Body>
        <ns1:AddSubscriber/>
            <LastName>MyLastName</LastName>
            <Email>myemail@someaddress.com</Email>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我对SOAP并不熟悉,而且我一直在寻找文档,但我似乎无法找到一个非常好的参考资料。

非常感谢任何指导!


感谢。你能举个例子吗?我正在查看PHP网站上显示的示例:

<?php
class SOAPStruct {
    function SOAPStruct($s, $i, $f) 
    {
        $this->varString = $s;
        $this->varInt = $i;
        $this->varFloat = $f;
    }
}
$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/"));
$struct = new SOAPStruct('arg', 34, 325.325);
$soapstruct = new SoapVar($struct, SOAP_ENC_OBJECT, "SOAPStruct", "http://soapinterop.org/xsd");
$client->echoStruct(new SoapParam($soapstruct, "inputStruct"));
?>

您是说我必须创建订阅者PHP类,分配所有变量$this->FirstName = $first_name等等...然后将其放入编码为SOAP_ENC_OBJECT的SoapVar中?如何更好地代表用户结构?

2 个答案:

答案 0 :(得分:3)

有两种可能的Params订阅者和可覆盖的

<soap:Body>
<AddSubscriber xmlns="admin.ekeryx.com">
  <subscriber>
    <ID>string</ID>
    <FirstName>string</FirstName>
    <LastName>string</LastName>
    <Email>string</Email>
  </subscriber>
  <overwritable>boolean</overwritable>
</AddSubscriber>

因此,您需要使用SoapVar来表示子网格结构,从而做一些更复杂的构造。

http://www.php.net/manual/en/soapvar.soapvar.php

我认为应该看起来像这样,虽然你想检查XSD对抗肥皂:身体产生......

$subscriber = new StdClass();
$subscriber->ID = 'myid';
$subscriber->FirstName = 'First';
$subscriber->LastName = 'Last';

$subscriber = new SoapParam(new SoapVar($subscriber, SOAP_ENC_OBJECT, $type, $xsd), 'subscriber');

$ type应该是api的XSD / WSDL缺省类型,$ xsd是XSD的URI。

我认为应该这样做,但我只使用原生PHP库一次用于eBay(这是一场噩梦哈哈),这差不多是2年前所以我有点生疏。

答案 1 :(得分:0)

我在寻找类似答案的同时遇到了这个问题。我相信您遇到的问题是以下代码将传递HTTP标头中的登录名和密码而不是SOAP标头

$client = new SoapClient($wsdl_url, array(
    'login' => 'myusername',
    'password' => 'mypassword',
    'trace' => true
));

我相信这就是你要找的东西

$headerbody = array('Token' => $someToken, 
                    'Version' => $someVersion, 
                    'MerchantID'=>$someMerchantId, 
                      'UserCredentials'=>array('UserID'=>$UserID, 
                                             'Password'=>$Pwd));