带有symfony2的soapweb服务



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

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

我不确定你是否找到了答案。只是为了其他可能遇到这样问题的人:

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(它很长,不是吗?也许一个更简单的路径也可以!);然后在中定义了phpwsdl.php/vender/php_wsdl/lib/php_wsdl:

<?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 TaraPageControllerBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationResponse;
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 TaraPageControllerBundleServices;

use TaraPageControllerBundleModel...
/**
 * @service TaraPageControllerBundleServicesMyService
 */
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连接到的web服务

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

并调用Create函数!您也可以通过打开上面写的地址来检查扩展的输出。该扩展还在上提供了一个"人类可读"的版本

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

如果这对任何人都有帮助,我会很高兴知道的!:)

SOAP是Symfony认为您熟悉的一个更通用的概念。您链接到的页面底部有一个示例WSDL。查看有关SOAP和WSDL的教程,然后尝试在Symfony页面中重新创建它们正在做的事情。

SOAP教程

WSDL教程

最新更新