0

一般来说,我是 WebServices 的新手,到目前为止,我已经使用 Zend/Soap 开发了一个 Web Service SoapServer。我已经能够很好地使用它,问题是,我希望客户端能够以数组的形式发送数据。

到目前为止,这就是我所做的:

肥皂服务器.php

<?php
/*
 * url_helper used to check the client Access Ip adrress and Port, in case is from dev or prod enviorement. etc.
 * returns the portion of the URL dynamicly in case its accesded from a public or local location.
 * 
 */
function url_helper(){
    $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
    $sp = strtolower($_SERVER["SERVER_PROTOCOL"]);
    $protocol = substr($sp, 0, strpos($sp, "/")) . $s;
    $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
    return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port;
}
if(($_SERVER['PHP_AUTH_USER'] == "test") AND ($_SERVER['PHP_AUTH_PW'] == "secret")){
    //autoload from composer, loads all the required files for ZendSoap
    include("vendor/autoload.php");

    $serviceURL = url_helper().'/soap_server.php';

    //The class for the WebService
    class soap_server{
         /**
         *
         * @param string $str1
         * @param string $str2
         * @param string $str3
         * @return stdClass
         */
        public function TEST($str1,$str2,$str3) {
            // do some work here he

            $response = new stdClass();
            $response->message = "vars = ($str1,$str2,$str3)";
            $response->success = true;

            return $response;
        }
    }

    // Generate WSDL relevant to code
    if (isset($_GET['wsdl'])){
        $autodiscover = new Zend\Soap\AutoDiscover();
        $autodiscover->setClass('soap_server')
                     ->setUri($serviceURL)
                     ->setServiceName('soap_server');
        $autodiscover->generate();
        $autodiscover->handle();

      //Soap Server
    } else {
        $server = new Zend\Soap\Server(null,array('uri' => $serviceURL.'?wsdl'));
        $server->setClass('soap_server');
        $server->handle();
    }
}
else
{
        //Send headers to cause a browser to request
        //username and password from user
        header("WWW-Authenticate: " .
            "Basic realm=\"Protected Area\"");
        header("HTTP/1.0 401 Unauthorized");

        //Show failure text, which browsers usually
        //show only after several failed attempts
        print("This page is protected by HTTP " .
            "Authentication.<br>\nUse <b>User</b> " .
            "for the username, and <b>PW</b> " .
            "for the password.<br>\n");
}

当我按照soapServer中的预期和定义以字符串形式发送时,测试客户端工作正常:

test_client.php

include("vendor/autoload.php");
$client = new Zend\Soap\Client("http://127.0.0.1/soap_server.php?wsdl",array('login'=>'test','password'=>'secret'));
$result1 = $client->TEST('Data1','OtherString','test');
print_r($result1);

我现在需要的只是找到一种方法,以便客户端可以向我发送数据和数组,例如:

$data = array('str1'=>'Data1','str2'=>'OtherString','str3'=>'test');

但我不知道如何使用 Zend Framework Autodiscovery 进行设置并将其与工作客户端配对。尝试使用类型数组而不是字符串,但没有成功。

非常感谢您的帮助。

真诚的,丹尼尔

编辑

所以我做了进一步的测试,我确实在 docblock 中设置了 Array 或 stdClass ,它的工作方式如下:

Zend Framework Soap 服务器示例:

$serviceURL = url_helper().'/test_ws.php';


    //The class for the WebService
    class test_ws{
        /**
        *
        * @param string $str1
        * @param array $myArray
        * @param stdClass $myObject test
        * @return stdClass
        */

        public function TEST2($str1,$myArray,$myObject) {              
            // do some work here    
            $response = new stdClass();

            $response->string = $str1;
            $response->array = var_export($myArray,TRUE);            
            $response->stdClass = var_export($myObject,TRUE);
            $response->object1 = $myObject->obj1;
            $response->success = true;

            return $response;

        }

    }

    // Generate WSDL relevant to code
    if (isset($_GET['wsdl'])){
        $autodiscover = new Zend\Soap\AutoDiscover();
        $autodiscover->setClass('test_ws')
                     ->setUri($serviceURL)
                     ->setServiceName('test_ws');
        $autodiscover->generate();
        $autodiscover->handle();

      //Soap Server
    } else {
        $server = new Zend\Soap\Server(null,array('uri' => $serviceURL.'?wsdl'));
        $server->setClass('test_ws');
        $server->handle();
    }
}

肥皂电话:

<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:test="http://10.1.11.122/ws_kioskos/test_ws.php">
   <soapenv:Header/>
   <soapenv:Body>
      <test:TEST2 soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <str1 xsi:type="xsd:string">String</str1>
         <myArray xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
            <!--You may enter ANY elements at this point-->
            <elem1 xsi:type="xsd:string">pos1</elem1>
            <elem2 xsi:type="xsd:string">pos2</elem2>
         </myArray>
         <myObject xsi:type="test:stdClass">
            <obj1 xsi:type="xsd:string">HELO</obj1>
            <obj2 xsi:type="xsd:string">WORLD</obj2>
         </myObject>         
      </test:TEST2>
   </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:ns1="http://10.1.11.122/ws_kioskos/test_ws.php?wsdl" 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:TEST2Response>
         <return xsi:type="SOAP-ENC:Struct">
            <string xsi:type="xsd:string">String</string>
            <array xsi:type="xsd:string">array (0 => 'pos1', 1 => 'pos2',)</array>
            <stdClass xsi:type="xsd:string">stdClass::__set_state(array('obj1' => 'HELO', 'obj2' => 'WORLD',))</stdClass>
            <object1 xsi:type="xsd:string">HELO</object1>
            <success xsi:type="xsd:boolean">true</success>
         </return>
      </ns1:TEST2Response>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

所以有了stdClass<object1 xsi:type="xsd:string">HELO</object1>stdClass我接近我想要实现的,正如您所见: 客户知道如何与 WSDL 交互,或者是否有另一种方法可以让我进行这样的思考。

我已经阅读了一些关于使用复杂数据类型和使用 ClassMap 定义 WebService 的内容,但我无法使用它进行任何工作,因为我找不到此类实现的良好文档。

再次感谢您的帮助。

丹尼尔

4

1 回答 1

0

工作示例

经过一番研究,这里的解决方案对zend-soap在 Laravel 中构建 SOAP 服务器的任何人都有用。

Acme\Controllers\SoapController班级:

...

public function wsdl()
{
    $wsdl = new AutoDiscover(new ArrayOfTypeComplex());
    $this->populateServer($wsdl);

    return response($wsdl->toXml(), 200)
        ->header('Content-Type', 'application/wsdl+xml');
}

private function populateServer($server)
{
    $server->setClass(MyService::class);
    $server->setUri('http://host.com/soap/server');
}

public function server(Request $request)
{
    $server = new Zend\Soap\ServerServer();
    $this->populateServer($server);

    $response = $server->handle();

    return response($response, 200)->header('Content-Type', 'application/soap+xml');
}

...

Acme\Services\MyService班级:

namespace Acme\Services;

class MyService
{

    /**
     * Does something.
     * @param Acme\Types\ItemType[] $items
     * @return \StdClass
     */
    public function doSomething(array $items)
    {
        // ...
    }
}

Acme\Types\ItemType班级:

namespace Acme\Types;

class ItemType
{

    /**
     * @var string
     */
    public $propertyName;
}

请注意两点:

  1. 使用Zend\Soap\Wsdl\ComplexTypeStrategy\ArrayOfTypeComplex策略;和
  2. 在 docblock 中提供完全限定的类名。

希望有帮助。

于 2018-11-16T05:55:00.587 回答