2014-02-11 23 views
0

我是新來的Symfony和Zend的PHP的響應RESTws採用PHP +的symfony + Zend公司與JSON格式

我使用symfony的1.x和Zend的1.11.6和PHP構建RESTws

這裏是我的代碼:

routine.yml -- 

api_highConfidenceHash: 
    url: /api/showList.:sf_format 
    class: sfPropelRoute 
    param: { module: api, action: showList} 
    options: { model: showListWS, type: list, method: getDeltaList} 
    requirements: 
    sf_format: (?:json) 

action.class.php -- 

public function executeshowList(sfWebRequest $request) { 

     require_once 'Zend/Rest/Server.php'; 
     $server = new Zend_Rest_Server(); 
     $server->setClass('showList'); 
     $server->handle(); 
    } 

showList.class.php -- 

class showList{ 
     public function getDeltaList($to,$from){ 
     return json_encode(array('to'=>$to,'from'=>$from); 
     } 

調用WS:

http://localost:9000/test_debug.php/api/showList.json?method=getDeltaList&to=1341705600&from=1341100800 

輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<showList generator="zend" version="1.0"><showList><response>{"from":"1341100800","to":"1341705600"}</response><status>success</status></showList></showList> 

我的要求是隻有沒有xml標籤的json響應輸出,我GOOGLE了很多,但無法找到解決方案。 你可以請我建議我怎麼才能響應只有json輸出的Ws。

回答

3

爲什麼你使用Zend_Rest_Server,我想你可以在沒有Zend的情況下使用symfony來創建REST webservice。

這裏例如:

REST webservice with symfony

sfRestWebServicePlugin

關於你的問題。我認爲xml返回Zend_Rest_Server。您可以檢查問題Make Zend_Rest_Server return JSON instead of XML using ZF,並嘗試使用答案。但在評論中,我讀到了Zend_Rest_Server is only working with XML. You can't decide the ouput format。所以,祝你好運!

+1

我已經跳過了Zend,並通過直接渲染Json直接使用Symfony來感謝您的參考。 – Shashi