2015-10-11 21 views
1

我試圖在我的moodle webservice中構建正確的響應函數。 我印製的json響應的PHP結構:如何在moodle中爲嵌套的webservice創建正確的返回函數

[ 
 
    { 
 
     "name":"Quiz", 
 
     "id":"1", 
 
     "theme":"green", 
 
     "quizzes":[ 
 
     { 
 
      "type":"single-select-item", 
 
      "question":"Question example 1?", 
 
      "options":[ 
 
       "<p>answer1<br><\/p>", 
 
       "<p>answer2<br><\/p>", 
 
       "<p>answer3<br><\/p>", 
 
       "<p>answer4<br><\/p>" 
 
      ], 
 
      "answer":"1" 
 
     }, 
 
     { 
 
      "type":"single-select-item", 
 
      "question":"<p>Question example 1?<br><\/p>", 
 
      "options":[ 
 
       "<p>answer1<br><\/p>", 
 
       "<p>answer2<br><\/p>", 
 
       "<p>answer3<br><\/p>", 
 
       "<p>answer4<br><\/p>" 
 
      ], 
 
      "answer":"2" 
 
     } 
 
     ] 
 
    } 
 
]

,但我不能寫的Moodle web服務的權利返回功能。

我已經寫了回報funtion如下

 return new external_multiple_structure(
     new external_single_structure(
      array(
       "name"=> new external_value(PARAM_TEXT, 'quiz name'), 
       "id"=> new external_value(PARAM_TEXT, 'quiz id'), 
       "theme"=> new external_value(PARAM_TEXT, 'quiz theme'), 
       "quizzes" => new external_multiple_structure(
           new external_single_structure(
            array(
             "type" => new external_value(PARAM_TEXT, 'answer type'), 
             "question" => new external_value(PARAM_TEXT, 'question'), 
             "options" => new external_multiple_structure(
                  new external_value(PARAM_TEXT, 'options') 
                ), 
             "answer" => new external_value(PARAM_TEXT, 'right answer') 
            ) 
           ) 

          ) 


      ) 
     ) 
    ); 

,但我仍然會收到以下異常。我的返回功能出了什麼問題?

{ 「異常」: 「invalid_response_exception」, 「錯誤代碼」: 「invalidresponse」, 「消息」: 「檢測到無效的響應值」}

任何建議? 謝謝

+2

什麼例外引發? – user3791372

+1

{「exception」:「invalid_response_exception」,「errorcode」:「invalidresponse」,「message」:「檢測到的響應值無效」} – adev

回答

1

我剛發現問題!! 返回函數是正確的,但我收到異常,因爲結構中的某些文本包含HTML標記,例如破壞返回函數組合的轉義字符。 最後我用這個PARAM_RAW代替了PARAM_TEXT,它就像一個魅力!

+0

非常感謝 - 您的解決方案也適用於我。我得到課程總結字段,它有html。我改變了我的參數類型PARAM_RAW,它的工作。好極了!!! –