【问题标题】:How to create proper xml for SOAP Client function?如何为 SOAP 客户端功能创建正确的 xml?
【发布时间】:2011-11-21 09:04:49
【问题描述】:

我已经在 SOAPClient 上花费了几个小时,但仍然没有结果,希望有人可以帮助我。我正在尝试创建以下代码并将其发送到网络服务:

POST /PortalServices/PortalServices.asmx HTTP/1.1
Host:***.***.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://***.com/CreateUser"

<?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="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <UserCredentials xmlns="http://***.com/">
      <UserName>string</UserName>
      <PassWord>string</PassWord>
      <ServiceName>string</ServiceName>
    </UserCredentials>
  </soap:Header>
  <soap:Body>
    <CreateUser xmlns="http://***.com/">
      <user>
        <Name>string</Name>
        <Email>string</Email>
        <PassWord>string</PassWord>
        <WsServices>
          <WsService>
            <Id>int</Id>
          </WsService>
        </WsServices>
      </user>
    </CreateUser>
  </soap:Body>
</soap:Envelope>

要获取这个 xml,我使用以下代码:

<?php

class WsService {
public $Id = "****";
}

class WsServices {
public $WsService;
public function __construct() {
         $this->WsService = new WsService();
        }
}


class WsUser {

public $Name = 'Sasha Lavasov';
public $Email = 'imsashko@gmail.com';
public $PassWord = 'test';
public $WsServices;

public function __construct() {
        $this->WsServices = new WsService();
        }
}

   $ini = ini_set("soap.wsdl_cache_enabled","0");

   $soap_url = 'http://***.***.com/PortalServices/PortalServices.asmx?wsdl';
   $soapClient = new \SoapClient($soap_url, array(
       'soap_version'=>SOAP_1_2,
       'trace' => 1,
       'exceptions' => true,
       'cache_wsdl' => WSDL_CACHE_NONE
       ));

   $hdd = new stdClass();
   $hdd->UserName =   '***';
   $hdd->PassWord = '***';
   $hdd->ServiceName = '***';

   $hd = new SoapVar($hdd, SOAP_ENC_OBJECT);


        $headers = new \SoapHeader('http://***.***.com/PortalServices', 'UserCredentials', $hd);

        // Prepare Soap Client
        $soapClient->__setSoapHeaders(array($headers));

        $user = new WsUser();
        // Setup the CreateUser parameters
        $ap_param = array(
        'Name' => 'Sasha Lavasov',
        'WsUser' => $user
                    );


        $param = new \SoapParam($user, 'WsUser');
        // Call CreateUser ()
        $error = 0;

        try {
        //Build user entity
        $usr  = new WsUser();
        $wsServices = new StdClass();

        $id = new StdClass();
        $id->Id = '***';
        $wsServices->WsService = $id;
        $usr->WsServices = $wsServices;

        $createUser = new stdClass();
        $createUser->user = $usr;

        $createUser = new SoapVar($createUser, SOAP_ENC_OBJECT, "WsUser", "http://***.***.com/PortalServices/PortalServices.asmx?op=CreateUser");

        $pm = new SoapParam($createUser, "user");

        $soapClient->CreateUser($pm);
            //Here I get xml
            var_dump( $soapClient->__getLastRequest()); die();
        } catch (\SoapFault $fault) {
            $error = 1;
            print($fault->faultcode." - ".$fault->faultstring); die();
        }

        if ($error == 0) {
        //             DO SOMETHING
            } else {

            }

         $res = $soapClient->__getLastResponse();
         print_r($res); die();

但我得到的是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://***.***.com/PortalServices/PortalServices.asmx?op=CreateUser" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://***.com/" xmlns:ns3="http://***.***.com/PortalServices">
<env:Header>

<ns3:UserCredentials>
<UserName>***</UserName>
<PassWord>***</PassWord>
<ServiceName>***</ServiceName>
</ns3:UserCredentials></env:Header>

<env:Body>
<ns2:CreateUser xsi:type="ns1:WsUser">
<user>
<Name>Sasha Lavasov</Name>
<Email>imsashko@gmail.com</Email>
<PassWord>test</PassWord>
<WsServices><WsService><Id>***</Id></WsService></WsServices>
</user>
</ns2:CreateUser>
</env:Body>
</env:Envelope>

结果为 FALSE。 在服务器函数列表中(使用 __getFunctions())我有以下内容:

CreateUserResponse CreateUser(CreateUser $parameters)

谁能帮助我或者告诉我哪里错了?

【问题讨论】:

  • 如果你对你认为很牛逼的 SOAP XML 和你不喜欢的可怜的 PHP SoapClient() 做一个详细的比较,唯一的区别是两个命名空间 ns1 和 ns3,这两个命名空间在你的PHP 代码。
  • @sasha 你也可以发布 WSDL 吗? Tnx
  • 问题是我使用了错误的命名空间,是的,WSDL 有效。但我使用 CURL 和适当的 xml 重写了它。这已经足够了,因为我只收到 1 个布尔响应并且很容易解析。谢谢,伙计们!

标签: php asp.net web-services soap-client


【解决方案1】:

只是一个建议。

在你的 good xml 中你已经:

<UserCredentials xmlns="http://***.com/">

但是在你的 PHP 中你设置了:

$headers = new SoapHeader('http://***.***.com/PortalServices', 'UserCredentials', $hd);

那么你有 2 个不同的命名空间:http://***.***.com/PortalServices 而不是 http://***.com/强>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    相关资源
    最近更新 更多