PHP SoapClient请求

3

我正在尝试使用这个 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的期望内容:

以下是服务对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"));
?>

你是说我需要创建一个Subscriber PHP类,分配所有变量$this->FirstName = $first_name等等...然后将其放入具有编码SOAP_ENC_OBJECT的SoapVar中吗?我如何更好地表示订阅者结构?

请注意,我已经从代码中删除了所有URL的http://部分,以符合每篇帖子最多只能有1个超链接的规定。 - Aaron
2个回答

3

有两个可能的参数:subscriber 和 overwriteable。

<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

我认为应该看起来像这样,尽管您需要根据生成的Soap:Body检查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(那是一场噩梦哈哈),而且那已经快两年了,所以我有点生疏。

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)); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接