php – 如何为SOAP Client函数创建适当的xml?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何为SOAP Client函数创建适当的xml?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我花了几个小时使用SOAPClient仍然没有结果,希望有人可以帮助我.我正在尝试创建并发送到以下代码的Web服务:

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)

任何人都可以帮助我或只是告诉我哪里错了吗?

解决方法

只是一个建议.

在你的好xml你有:

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

但在你的PHP中你设置:

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

然后你有2个不同的命名空间:http://***.***.com/PortalServices而不是http://***.com/

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何为SOAP Client函数创建适当的xml?全部内容,希望文章能够帮你解决php – 如何为SOAP Client函数创建适当的xml?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。