symfony2的肥皂网服务

1 soap web-services soapserver symfony

我需要使用symfony2创建一个web服务我已经阅读了官方文章http://symfony.com/doc/current/cookbook/web_services/php_soap_extension.html ,在示例中它使用参数路由.wsdl文件创建了一个SoapServer 实例,这是什么文件?我没有在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;
}
Run Code Online (Sandbox Code Playgroud)

Ara*_*lir 5

我不确定你是否找到了答案.只是对于可能遇到这样一个问题的其他人:

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{
}
Run Code Online (Sandbox Code Playgroud)

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

require_once __DIR__. '/../vendor/php_wsdl/lib/php_wsdl/php_wsdl.php';
Run Code Online (Sandbox Code Playgroud)

就一件事!扩展需要一个"缓存"文件夹,以便缓存创建的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

dirname(__FILE__). '/../Services/MyService.php
Run Code Online (Sandbox Code Playgroud)

此行告诉扩展在哪里查找注释以创建WSDL页面.您还需要定义到"service_wsdl"的路由:

service_wsdl:
    pattern: /YourDesiredPathFor/services.wsdl
    defaults: {_controller: TaraPageControllerBundle:Service:wsdl}
Run Code Online (Sandbox Code Playgroud)

如您所见,控制器是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;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用SoapClient连接到您的Web服务

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

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

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

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