Php Zend Soap自动发现允许阵列-Web服务

Php Zend Soap自动发现允许阵列-Web服务,php,web-services,zend-framework,soap,zend-soap,Php,Web Services,Zend Framework,Soap,Zend Soap,一般来说,我不熟悉Web服务,到目前为止,我已经使用Zend/Soap开发了一个Web服务SoapServer。我已经能够很好地使用它了,问题是,我希望客户端能够以和数组的形式发送数据 到目前为止,我所做的就是: soap_server.php <?php /* * url_helper used to check the client Access Ip adrress and Port, in case is from dev or prod enviorement. etc. *

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

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

soap_server.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");
}
我现在需要的是找到一种方法,以便客户端可以将数据以数组形式发送给我,如:

$data = array('str1'=>'Data1','str2'=>'OtherString','str3'=>'test');
但我不知道如何使用Zend Framework Autodiscovery设置它,并将其与工作客户端配对。尝试使用类型数组而不是字符串,但没有成功

非常感谢你的帮助

真诚地, 丹尼尔

编辑

因此,我做了进一步的测试,并在docblock中设置和排列or和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();
    }
}
Soap调用:

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

一串
位置1
位置2
希洛
世界
以及Soap响应:

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

一串
数组(0=>'pos1',1=>'pos2',)
stdClass::_设置_状态(数组('obj1'=>'HELO','obj2'=>'WORLD',))
希洛
真的
所以对于stdClass,我已经接近了我想要实现的目标,正如您可以看到的:
HELO
唯一缺少的想法是,是否有一种方法可以告诉自动发现方法stdClass中的元素是什么,以便客户机知道如何与WSDL交互,或者是否有另一种方法可以让我这样想

我读过一些关于使用复杂数据类型和使用类映射来定义Web服务的文章,但是我无法使用它,因为我找不到这种实现的好文档

再次,, 非常感谢你的帮助

丹尼尔

工作示例 经过一些研究,下面的解决方案对于在Laravel中使用
zend SOAP
构建SOAP服务器的任何人都是有用的

Acme\Controllers\SoapController
class:

...

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');
}

...
namespace Acme\Services;

class MyService
{

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

class ItemType
{

    /**
     * @var string
     */
    public $propertyName;
}
Acme\Services\MyService
class:

...

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');
}

...
namespace Acme\Services;

class MyService
{

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

class ItemType
{

    /**
     * @var string
     */
    public $propertyName;
}
Acme\Types\ItemType
class:

...

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');
}

...
namespace Acme\Services;

class MyService
{

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

class ItemType
{

    /**
     * @var string
     */
    public $propertyName;
}
请注意两件事:

  • 使用
    Zend\Soap\Wsdl\ComplexTypeStrategy\ArrayOftTypeComplex
    策略;及
  • 在docblock中提供完全限定的类名

  • 希望能有所帮助。

    当您说“尝试使用类型数组而不是字符串”时,您在docblock中到底有什么?因为ZF根据它在docblock中看到的内容生成WSDL。@JamesG感谢您指出这一点,正如我在编辑部分中发布的那样,它帮助完成了更多的工作。很酷-很高兴我能提供帮助。我能补充的另外两件事是,当我遇到类似的问题时,我解决问题的两件事是使用@var respItem[]定义数组元素,而不是使用array关键字并用Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex实例化自动发现对象(不确定现在ZF2的等价物是什么)。我希望这有帮助。