.NET解决方案

时间:2016-10-05 00:05:34

标签: php .net xml web-services nusoap

我使用nuSOAP WebService创建了一个Laravel,它在除.NET解决方案之外的所有环境中都能正常工作......

#PHP /app/Http/route.php

$app->group(['prefix' => 'test', 'namespace' => 'App\Http\Controllers'], function () use ($app) {

    $app->get('/', 'TestController@index');
    $app->get('/{any}', 'TestController@index');
    $app->post('/', 'TestController@index');
    $app->post('/{any}', 'TestController@index');

});

#PHP /app/Http/Controllers/TestController.php

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Common;
use SoapBox\Formatter\Formatter;

class TestController extends Controller
{
    public function __construct()
    {
        $this->server = new \soap_server;
        $end_point = env('END_POINT') . '/test/wsdl';
        $this->server->configureWSDL('test', 'urn:test', $end_point, 'rpc');

        $this->server->register(
            'testMethod',
            [
                'one' => 'xsd:string',
                'two' => 'xsd:string',
            ],
            [
                'return' => 'xsd:array'
            ],
            'urn:testWSDL',
            'urn:testWSDL#testMethod',
            'rpc',
            'encoded',
            'Test Method'
        );
    }

    function index(Request $request, $data = NULL)
    {
        $_SERVER['QUERY_STRING'] = '';
        if ( $request->segment(2) == 'wsdl' ) $_SERVER['QUERY_STRING'] = 'wsdl';
        return $this->server->service(file_get_contents('php://input'));
    }
}

#PHP /app/Http/helpers.php

<?php use SoapBox\Formatter\Formatter;

function arrayToXml($array, $remove_structure = TRUE) {

    $array = Formatter::make($array, Formatter::ARR);

    $ret = $array->toXml();

    if ( $remove_structure )
    {
        $ret = str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $ret);
        $ret = str_replace('<xml>', '', $ret);
        $ret = str_replace('</xml>', '', $ret);
    }

    return trim($ret);
}

function testMethod($one, $two)
{
    return arrayToXml([
        "one"   =>  $one,
        "two"   =>  $two
    ]);
}

如果我尝试使用SoapGUI,我会发送:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:testWSDL">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:testMethod soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <one xsi:type="xsd:string">first</one>
         <two xsi:type="xsd:string">second</two>
      </urn:testMethod>
   </soapenv:Body>
</soapenv:Envelope>

我收到了......

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:testMethodResponse xmlns:ns1="urn:testWSDL">
         <return xsi:type="xsd:array">
            <one>first</one>
            <two>second</two>
         </return>
      </ns1:testMethodResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

原始回复是:

HTTP/1.0 200 OK
Date: Tue, 02 Oct 2016 13:26:14 GMT
Server: Apache/2.4.18 (Unix) PHP/5.5.36 LibreSSL/2.2.7
X-Powered-By: PHP/5.5.36
X-SOAP-Server: NuSOAP/0.9.5 (1.56)
Content-Length: 537
Cache-Control: no-cache
Connection: close
Content-Type: text/xml; charset=UTF-8

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:testMethodResponse xmlns:ns1="urn:testWSDL"><return xsi:type="xsd:array"><one>first</one><two>second</two></return></ns1:testMethodResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

我也尝过soapclient.com/soaptest.html并且工作得很好...... 但在.NET解决方案中......我将其添加为Web Reference:

enter image description here

然后添加下一个代码......我得到intellisense ...

first param...

second param...

然后点击Click Me按钮并收到此错误:

System.Xml.dll中发生了'System.InvalidOperationException'类型的第一次机会异常 System.InvalidOperationException:错误文档XML(1,479)。 ---&GT; System.InvalidCastException:No se puede asignar el objeto de tipo System.Xml.XmlNode [] al objeto de tipo System.String。    cn Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadertest.Read2_testMethodResponse()    cn Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer3.Deserialize(XmlSerializationReader reader)    en System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,String encodingStyle,XmlDeserializationEvents事件)    --- Fin del seguimiento de la pila delaexcepcióninterna---    en System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,String encodingStyle,XmlDeserializationEvents事件)    en System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,String encodingStyle)    en System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage消息,WebResponse响应,Stream responseStream,Boolean asyncCall)    en System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName,Object [] parameters)    en testingSolution.local31.test.testMethod(String one,String two)zh C:\ Documents and Settings \ admin \ Mis documentos \ testingSolution \ Web References \ local31 \ Reference.vb:línea80    en testingSolution.Testeos.Button3_Click(Object sender,EventArgs e)zh C:\ Documents and Settings \ admin \ Mis documentos \ testingSolution \ index.vb:línea50

如果我将返回类型更改为字符串...在.NET解决方案中,我得到: enter image description here

但是在SoapGUI(以及所有其他应用程序)中:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:testMethodResponse xmlns:ns1="urn:testWSDL">
         <return xsi:type="xsd:string">&lt;one>mundo&lt;/one>&lt;two>aunque&lt;/two></return>
      </ns1:testMethodResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我该怎么做才能解决这个问题?因为我需要返回一个数组。谢谢!

1 个答案:

答案 0 :(得分:0)

最后就是这样:

//  whether $methodreturn is a string of literal XML
$this->server->methodreturnisliteralxml = true;

来源:PHP NuSOAP response