2017-06-01 70 views
0

嗨我新與moodle和調用Web服務時出現錯誤。Moodle休息時間返回錯誤的功能core_user_get_users_by_field

目前我正在嘗試從moodle中檢索用戶,使用以下函數core_user_get_users_by_field,並使用rest服務來執行此操作。我已經設法創建了一個用戶,這樣我就可以使用該服務進行身份驗證。

我正在接收所述誤差是

缺少單個結構所需的關鍵:字段

波紋管是代碼用於創建一個用戶。我從錯誤中得到的問題是我需要爲帖子發送的參數格式不正確。有誰知道如何正確使用此方法或任何其他方法進行搜索。

 String token = "token"; 

     String postData = "username=username"; 

     string createRequest = string.Format("http://domain/webservice/rest/server.php?wstoken={0}&wsfunction={1}&moodlewsrestformat=json", token, "core_user_get_users_by_field"); 

     // Call Moodle REST Service 
     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(createRequest); 
     req.Method = "POST"; 
     req.ContentType = "application/x-www-form-urlencoded"; 

     // Encode the parameters as form data: 
     byte[] formData = 
      UTF8Encoding.UTF8.GetBytes(postData); 
     req.ContentLength = formData.Length; 

     // Write out the form Data to the request: 
     using (Stream post = req.GetRequestStream()) 
     { 
      post.Write(formData, 0, formData.Length); 
     } 


     // Get the Response 
     HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
     Stream resStream = resp.GetResponseStream(); 
     StreamReader reader = new StreamReader(resStream); 
     string contents = reader.ReadToEnd(); 

     // Deserialize 
     JavaScriptSerializer serializer = new JavaScriptSerializer(); 
     if (contents.Contains("exception")) 
     { 
      // Error 
      MoodleException moodleError = serializer.Deserialize<MoodleException>(contents); 
     } 
     else 
     { 
      // Good 

     } 

回答

1

core_user_get_users_by_field需要給定的參數具有以下主要關聯數組的web服務:值
「場」:「ID」
「值」:整數數組(必須是一個數組,可能與剛一個值)

在PHP中的還要好,例如:

$parameters = array('field' => 'id', 'values' => array(13)); 

這意味着:其「ID」有13課程價值的用戶,您可以使用其他面值('field'=>'lastname','values'=> array('Smith'))
您可以選擇的參數是Moodle'用戶'表的字段。
嘗試在您的postData變量中構建這些參數。

+0

感謝幫助很大網址 –

0

這裏是與我放活的網址,郵遞員,設置HTTP方法POST方法

hostname/webservice/rest/server.php?wstoken=any_token&wsfunction=core_user_get_users_by_field&field=email&values[0][email protected] 
&moodlewsrestformat=json