【问题标题】:soap web service with symfony2带有 symfony2 的肥皂网络服务
【发布时间】:2012-08-17 18:18:49
【问题描述】:

我需要用 symfony2 创建一个 web 服务,我阅读了官方文章 http://symfony.com/doc/current/cookbook/web_services/php_soap_extension.html 在示例中,它创建了一个带有参数的 SoapServer 实例,路由一个 .wsdl 文件,这个文件是什么?我没有在 symfony 中找到太多关于肥皂的文档。请帮忙?

public function indexAction()
{
    $server = new \SoapServer('/path/to/hello.wsdl');
    $server->setObject($this->get('hello_service'));

    $response = new Response();
    $response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1');

    ob_start();
    $server->handle();
    $response->setContent(ob_get_clean());

    return $response;
}

【问题讨论】:

    标签: web-services soap symfony soapserver


    【解决方案1】:

    我不确定您是否找到了答案。仅针对可能遇到此类问题的其他任何人:

    WSDL 是定义和描述 Web 服务的语言。它基本上是一个 XML 文件,其中包含服务器提供的每个函数的输入/输出参数。它还包含有关提供服务的服务器本身的一些信息。

    为了能够创建 web 服务,您需要使用您提供的代码,这实际上会准备 Symfony 为“/path/to/hello.wsdl”上的客户端提供服务(在我的示例中路径是 /YourDesiredPathFor/services.wsdl),并且您需要提供一个有效的 WSDL 文档,其中包含正确的 WSDL 格式的上述信息。问题是 Symfony(甚至是 PHP 本身)无法自动创建文件。

    要解决这个问题,您需要使用外部 WSDL 生成器。我建议使用PHP-WSDL-Creator。它使用写在 php 文件中的注释来创建 WSDL 文件并运行 SoapServer。这意味着您甚至不需要您提供的代码。它还具有适当的接口和插件,可为您提供不同协议和语言的客户端。

    你需要稍微调整一下!如果您希望它符合 symfony 标准,我认为您需要重写它的某些部分;但如果您想将它用作外部库,它也可以工作! 我这样做的方法是将提取的文件复制到 ./vendor/php_wsdl/lib/php_wsdl/src (很长,不是吗?也许更简单的路径也可以!);然后在 ./vender/php_wsdl/lib/php_wsdl 中定义了一个 php_wsdl.php:

    <?php
    
    require_once __DIR__. '/src/class.phpwsdl.php';
    
    class WSDL_PhpWsdl extends PhpWsdl{
    }
    

    接下来,在“./app/autoload.php”中,我添加了以下行以使 Symfony 能够使用创建的扩展:

    require_once __DIR__. '/../vendor/php_wsdl/lib/php_wsdl/php_wsdl.php';
    

    只有一件事!扩展需要一个“缓存”文件夹来缓存创建的 wsdl 文件和所有文件。不幸的是,因为我需要快速完成项目,所以我没有足够的时间来管理缓存文件夹。肯定有比我更好的方法,我真的很高兴知道它们。

    无论如何,现在您需要使用扩展程序的功能!为此,我为我正在使用的包创建了一个“ServerController”:

    <?php
    namespace Tara\PageControllerBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Response;
    
    class ServiceController extends Controller
    {
        function wsdlAction(){
    
            \PhpWsdlServers::$EnableRest = false;
    
            $soap= \WSDL_PhpWsdl::CreateInstance(
                null,                               // PhpWsdl will determine a good namespace
                $this->generateUrl('service_wsdl', array(), true),  // Change this to your SOAP endpoint URI (or keep it NULL and PhpWsdl will determine it)
                __DIR__. '/cache',                  // Change this to a folder with write access
                Array(                              // All files with WSDL definitions in comments
                    dirname(__FILE__). '/../Services/MyService.php'
                ),
                null,                               // The name of the class that serves the webservice will be determined by PhpWsdl
                null,                               // This demo contains all method definitions in comments
                null,                               // This demo contains all complex types in comments
                false,                              // Don't send WSDL right now
                false                               // Don't start the SOAP server right now
            );
    
            if($soap->IsWsdlRequested())            // WSDL requested by the client?
                $soap->Optimize=false;              // Don't optimize WSDL to send it human readable to the browser
    
            $soap->RunServer();
        }
    }
    

    如您所见,缓存文件夹的路径位于本地目录中,这意味着必须在 ./src/Tara/PageControllerBundle/Controller 中手动创建它(显然在我的情况下;您需要更改路径)。我确信有更好的方法来管理缓存文件夹。 那里有一行:

    dirname(__FILE__). '/../Services/MyService.php
    

    这一行告诉扩展在哪里寻找注释以创建 WSDL 页面。您还需要定义到“service_wsdl”的路由:

    service_wsdl:
        pattern: /YourDesiredPathFor/services.wsdl
        defaults: {_controller: TaraPageControllerBundle:Service:wsdl}
    

    如你所见,控制器是ServiceController,负责它的函数是wsdlAction;定义的确切函数!

    作为一个例子,我会提供我自己的 MyService.php:

    <?php
    
    namespace Tara\PageControllerBundle\Services;
    
    
    use Tara\PageControllerBundle\Model\...
    
    /**
     * @service Tara\PageControllerBundle\Services\MyService
     */
    
    class MyService
    {
        /**
         * Function Create
         *
         * @param string $link
         * @param string $title
         * @param string $image
         * @param string $description
         * @return boolean Status of the creation
         * @pw_rest POST /YourDesiredPathForAction/create Create The Object
         *
         */
    
        public function Create($link, $title, $image, $description)
        {
    
            // your code that handles the data goes here. this is not a part of the annotations!
            return (bool)$result;
        }
    }
    

    现在,您也许可以使用 SoapClient 连接到您的网络服务

    http://your-server.something/YourDesiredPathFor/services.wsdl?wsdl

    并调用 Create 函数!你也可以通过打开上面写的地址来检查扩展的输出。该扩展还在

    处提供了“人类可读”版本

    http://your-server.something/YourDesiredPathFor/services.wsdl.

    我很高兴知道这是否对任何人有帮助! :)

    【讨论】:

      【解决方案2】:

      SOAP 是一个更一般的概念,Symfony 假设您熟悉它。您链接到的页面底部有一个示例 WSDL。查看有关 SOAP 和 WSDL 的教程,然后尝试在 Symfony 页面中重新创建他们正在执行的操作。

      SOAP Tutorial

      WSDL Tutorial

      【讨论】:

      • 好的,我假设 symfony 为你创建了这部分,必须改进 php native 中的这个扩展。谢谢!
      猜你喜欢
      • 1970-01-01
      • 2021-07-29
      • 2017-01-16
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      相关资源
      最近更新 更多