dtzh131555 2011-11-21 09:04
浏览 45

如何为SOAP Client功能创建适当的xml?

I've spend hours with SOAPClient and still get no result, hope someone can help me. I'm trying to create and send to web-service following code:

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>

To get this xml I use the following code:

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

But instead I get something like this:

<?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>

And get FALSE as result. In the list of server's functions (using __getFunctions()) I've got following:

CreateUserResponse CreateUser(CreateUser $parameters)

Can anyone help me or just tell where I'm wrong?

  • 写回答

1条回答 默认 最新

  • dpfw3607 2011-11-24 22:53
    关注

    Just a suggestion.

    In your good xml you've:

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

    but in your PHP you set:

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

    Then you've 2 different namespaces: http://***.***.com/PortalServices instead of http://***.com/

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line