2017-07-27 70 views
6

我正嘗試在magneto2中創建自定義Api。它是一個POST調用,在我試圖發送關聯數組像下面,Magento 2 Rest Api - 請求參數中的關聯數組

{ 
    "data": {  
     "testData": { 
      "title": "Test 01",   
      "place": { 
       "key": "value" 
      } 
     } 
    } 
} 

對於上述格式我給帕拉姆標註爲string[]。它的工作如果我沒有通過place數據。但是,如果我傳遞place數據的投擲的錯誤像下面,

Array to string conversion 

實際的問題是我不能夠通過關聯數組中請求PARAM。任何人都可以幫助我嗎?

我也嘗試通過創建自定義對象類型。在這一點上,我也只能給string[]類型。所以我不能在其中添加更多的json數據。

回答

1

您可以在PHP中創建數組並使用函數將其編碼爲json。

$arr= array('val1'=>array('your val1', 'your val2'), 
    'val2'=>array('your val1', 'your val2'), 
    ); 

    $data = json_encode($arr); 
// if you want to set in url 
$client = new Zend_Http_Client($uri); 
$client->setRawData($data, 'application/json')->request('POST'); 

,你將需要使用json_decode後得到你的價值觀。 希望這有助於!

+0

是的。沒關係。但任何其他選項直接傳遞json數據? –

+0

你的emai編號好嗎? –

+0

爲什麼你需要另一種選擇? 直接發送,下面的鏈接可以幫助。 https://stackoverflow.com/questions/21271140/curl-and-php-how-can-i-pass-a-json-through-curl-by-put-post-get – Pallavi