Perl SOAP :: Lite修复XML以与PHP SoapClient生成的兼容

时间:2015-11-02 11:02:28

标签: php xml perl soap

如何使SOAP :: Lite像PHP SoapClient一样生成XML?

实施例

Perl代码:

#!/usr/bin/perl
use strict;
use warnings;

use SOAP::Lite on_action => sub{sprintf '%s', $_[0]};
SOAP::Lite->import(+trace => "all");
my $client =  SOAP::Lite->new;

$client->service("https://www.just-example.test/dir/JustTest/Service.asmx?WSDL");
$client->proxy("https://www.just-example.test/dir/JustTest/Service.asmx");
$client->uri("http://example.test/ExampleMethod");

my %params = (
    'TestParam' => "",
     );
$client->ExampleMethod(\%params);

由Perl代码发送的XML(似乎与服务兼容):

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Body>
        <ExampleMethod
            xmlns="http://example.test/ExampleMethod">
            <c-gensym3>
                <TestParam xsi:type="xsd:string" />
            </c-gensym3>
        </ExampleMethod>
    </soap:Body>
</soap:Envelope>

PHP代码:

<?php
$client = new SoapClient("https://www.just-example.test/dir/test/JustTest/Service.asmx?WSDL",
        array(
        'trace' => 1,
        'uri' => "http://example.test/ExampleMethod"

         )
);
$params = array (
    'TestParam' => ""
        );

$response = $client->ExampleMethod($params);

echo $client->__getLastRequest() . "\n";

var_dump($response);

PHP代码发送的XML(似乎与服务兼容):

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://example.test/">
    <SOAP-ENV:Body>
        <ns1:ExampleMethod>
            <ns1:TestParam>0</ns1:TestParam>
        </ns1:ExampleMethod>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

2 个答案:

答案 0 :(得分:1)

您只需根据规范更改xml的前缀,查看类似问题here以获取更多信息。

为了解决您的特殊问题,我认为您只需要在代码的最开头添加以下代码(在SOAP :: Lite中设置wsdl之前):

$SOAP::Constants::PREFIX_ENV = 'SOAP-ENV';

答案 1 :(得分:0)

使用SOAP :: Lite的代码非常简单:

$uri = 'http://example.test';
$proxy = 'https://www.just-example.test/dir/JustTest/Service.asmx';

$client = SOAP::Lite->new(
              uri   => $uri,
              proxy => $proxy,
             );

$client->autotype(0);
$client->readable(1);
$client->ns($uri,'ns1');
$client->envprefix('SOAP-ENV');

$params = SOAP::Data->name(TestParam => 0)->prefix('ns1');

$data = $client->ExampleMethod($params);