2013-02-05 81 views
0

我有大部分的輸入參數被置於作爲一個JSON請求對象的請求JSON請求的所有輸入。出於文檔的目的,我想指定用戶可以放入的最常見的字段,但是名稱值存在很多可變性,這些可能會進入JSON請求,我不想將所有這些文檔記錄下來,因爲它會麻煩。捕獲在(使用Restler)

這裏是什麼,我現在有一個截圖:

API in Explorer

好像我想把在稱爲JSON財產「人與」並將其設置爲「[‘喬’的例子, 「保羅」,「簡」]那麼這將是容易的JSON做的,但我怎麼會挑選在我的PHP/Restler代碼現在該服務的簽名是:

/** 
* ADD an Activity 
* 
* Add a new action to a user's stream 
* 
* @url POST /{user_id} 
* 
* @param integer $user_id The user_id for whom the actions apply; you can insert the text "self" and it will resolve to the current/default user 
* @param string $start_time {@from body} The date/time that the activity was started (YYYY-MM-DD or YYYY-MM-DD HH:mm:SS) 
* @param string $action  {@from body} The action "slug name" that uniquely identifies an action 
* @param string $end_time {@from body} The date/time that the activity concluded (YYYY-MM-DD or YYYY-MM-DD HH:mm:SS) 
* @param string $app_id  {@from body} The application that captured this activity 
* @param string $proxy_user_id {@from body} The person who captured this activity for the individual 
* @param string $location {@from body} The location information associated with this activity 
*/ 
public function add_action ($user_id, $start_time, $action, $end_time=null, $app_id=null, $proxy_user_id=null, $location=null) 
{ 
    // implement 
} 

PS爲一個側面說明,我已經暫時將此API服務更改爲PUT以避免POST問題幾天前提出了這個問題,在使用POST的同時也影響了我。

回答

0

好,關鍵是解決這個位於$request_data關聯數組英寸因此,爲了實現雙方的文檔(關鍵字段),並能夠動態接收值到POST/PUT服務,你只需要做到這一點:

/** 
* ADD an Activity 
* 
* Add a new action to a user's stream 
* 
* @url PUT /{user_id} 
* 
* @param integer $user_id The user_id for whom the actions apply; you can insert the text "self" and it will resolve to the current/default user 
* @param string $start_time {@from body} The date/time that the activity was started (YYYY-MM-DD or YYYY-MM-DD HH:mm:SS) 
* @param string $action_nm {@from body} The action "slug name" that uniquely identifies an action 
* @param string $end_time {@from body} The date/time that the activity concluded (YYYY-MM-DD or YYYY-MM-DD HH:mm:SS) 
* @param string $app_id  {@from body} The application that captured this activity 
* @param string $proxy_user_id {@from body} The person who captured this activity for the individual 
* @param string $location {@from body} The location information associated with this activity 
*/ 
public function add_activity ($user_id, $start_time, $action_nm, $end_time=null, $app_id=null, $proxy_user_id=null, $location=null, $request_data=null) 

注意,沒有@param爲$ request_data但$ request_data現在掛在函數簽名的末尾。現在想象一下,我在請求的JSON中通過了以下'test' : 'me'。現在,我可以得到,在我的處理程序:

echo $request_data['test']; // prints "me" 

這工作和文檔看起來它應該也(見上面的屏幕截圖)。

最後一點,對於那些好奇的人,可以通過$ request_data數組訪問所有的JSON變量。所以這意味着:

echo ($request_data['user_id'] === $user_id); // TRUE 
+0

這樣做的唯一不良結果是$ request_data在文檔中出現。任何人都知道避免這種方式? – ken

+0

$ request_data不應該出現在那裏,它是Resources.php中的一個錯誤將很快修復 – Luracast

+0

只是推動更新來解決這個:) – Luracast