2012-08-17 72 views
1

我需要創建一個symfony2的Web服務我閱讀官方文章http://symfony.com/doc/current/cookbook/web_services/php_soap_extension.html 在這個例子中它創建一個SoapServer的實例與參數路由.wsdl文件,這是什麼文件?我沒有在symfony上找到關於肥皂的太多文檔。一些幫助,請嗎?soap web服務與symfony2

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

回答

0

SOAP是Symfony假定您熟悉的更一般的概念。在您鏈接到的頁面底部有一個示例WSDL。查看關於SOAP和WSDL的教程,然後嘗試重新創建他們在Symfony頁面中執行的操作。

SOAP Tutorial

WSDL Tutorial

+0

好吧,我已經假設symfony爲你創建這個部分,這個在PHP本地擴展必須改進。謝謝! – user1607625 2012-08-17 18:49:06

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

下,在「./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。

我很高興知道這是否對任何人都有幫助! :)