2

これは次のようなトークンを返します: {"token":"260e5b8adf74af6be5dfa250c5ad93c8"}

そして、コンテンツ部分が欲しいだけです: 260e ... $getInfo['token'] を使用して取得できると思いますよね?

$user="admin";
$password="Moodle15!";
$services="moodle_mobile_app";

$url = "https://localhost/moodle/login/token.php?username=".$user."&password=".$password."&service=".$services."";

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURL_RETURNTRANSFER, true);

$getURL = curl_exec($ch);

$getInfo = json_decode($getURL, true);

$displayToken = $getInfo['token'];
echo $displayToken;

curl_close($ch);

そして、CURL_RETURNTRANSFER で何が起こっているのかわかりません。

そしてエラー:

Notice: Use of undefined constant CURL_RETURNTRANSFER - assumed 'CURL_RETURNTRANSFER' in C:\xampp\htdocs\moodle\cURL_script.php on line 12

Warning: curl_setopt() expects parameter 2 to be long, string given in C:\xampp\htdocs\moodle\cURL_script.php on line 12
{"token":"260e5b8adf74af6be5dfa250c5ad93c8"}

編集:

もし私が持っていたら:

{"id":4,"username":"Jhon","firstname":"smith","lastname":"Reches","fullname":"Jhon smith","email":"Jhon.smith@example.com","department":"","firstaccess":0,"lastaccess":0,"description":"","descriptionformat":1,"profileimageurlsmall":"http:\/\/localhost\/moodle\/pluginfile.php\/25\/user\/icon\/f2","profileimageurl":"http:\/\/localhost\/moodle\/pluginfile.php\/25\/user\/icon\/f1","groups":[],"roles":[{"roleid":5,"name":"","shortname":"student","sortorder":0}],"enrolledcourses":[{"id":3,"fullname":"Game Design","shortname":"Game Design"},{"id":2,"fullname":"Grup de Recerca","shortname":"Grp. Recerca"}]}

「ユーザー名」にアクセスするにはどうすればよいですか? または、「フルネーム」がたくさんあることがわかっている場合は「フルネーム」ですか?

EDITED2:

私が今持っている新しいコード:

$token = "260e5b8adf74af6be5dfa250c5ad93c8";
$funcion="core_enrol_get_enrolled_users";
$courseid="3";
$format="json";
$url= "https://localhost/moodle/webservice/rest/server.php?wstoken=".$token."&wsfunction=".$funcion."&courseid=".$courseid."&moodlewsrestformat=".$format."";   
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$getURL = curl_exec($ch);
$getInfo = json_decode($getURL, true);
$email = $getInfo[2]['email'];
echo $email."</br>";
curl_close($ch);

$email には jhon.smith@example.com が含まれていますが、多くの結果があります。foreach でそれらすべてを表示するにはどうすればよいですか? 多次元配列だからです。下記参照。

[{"id":4,"username":"jhon1.smith1","firstname":"Jhon1","lastname":"Smith1","fullname":"jhon1 smith1","email":"jhon1.smith1@example.cat","department":"","firstaccess":0,"lastaccess":0,"description":"","descriptionformat":1,"profileimageurlsmall":"http:\/\/localhost\/moodle\/pluginfile.php\/25\/user\/icon\/f2","profileimageurl":"http:\/\/localhost\/moodle\/pluginfile.php\/25\/user\/icon\/f1","groups":[],"roles":[{"roleid":5,"name":"","shortname":"student","sortorder":0}],"enrolledcourses":[{"id":3,"fullname":"Game Design","shortname":"Game Design"},{"id":2,"fullname":"Grup de Recerca","shortname":"Grp. Recerca"}]},{"id":5,"username":"jhon1.smith1","firstname":"Josep","lastname":"jhon1 ","fullname":"jhon1 smith1","email":"jhon1.smith1@example.cat","department":"","firstaccess":0,"lastaccess":0,"description":"","descriptionformat":1,"profileimageurlsmall":"http:\/\/localhost\/moodle\/pluginfile.php\/26\/user\/icon\/f2","profileimageurl":"http:\/\/localhost\/moodle\/pluginfile.php\/26\/user\/icon\/f1","groups":[],"roles":[{"roleid":5,"name":"","shortname":"student","sortorder":0}],"enrolledcourses":[{"id":3,"fullname":"Game Design","shortname":"Game Design"},{"id":2,"fullname":"Grup de Recerca","shortname":"Grp. Recerca"}]},{"id":6,"username":"jhon1.smith1","firstname":"jhon1","lastname":"smith1","fullname":"jhon1 smith1","email":"jhon1.smith1@example.cat","department":"","firstaccess":0,"lastaccess":0,"description":"","descriptionformat":1,"profileimageurlsmall":"http:\/\/localhost\/moodle\/pluginfile.php\/32\/user\/icon\/f2","profileimageurl":"http:\/\/localhost\/moodle\/pluginfile.php\/32\/user\/icon\/f1","groups":[],"roles":[{"roleid":1,"name":"","shortname":"manager","sortorder":0}],"enrolledcourses":[{"id":3,"fullname":"Game Design","shortname":"Game Design"},{"id":2,"fullname":"Grup de Recerca","shortname":"Grp. Recerca"}]},{"id":7,"username":"jhon1.jhon1","firstname":"jhon1","lastname":"jhon1","fullname":"jhon1 smith1","email":"jhon1.smith1@example.cat","department":"","firstaccess":0,"lastaccess":0,"description":"","descriptionformat":1,"profileimageurlsmall":"http:\/\/localhost\/moodle\/pluginfile.php\/35\/user\/icon\/f2","profileimageurl":"http:\/\/localhost\/moodle\/pluginfile.php\/35\/user\/icon\/f1","groups":[],"roles":[{"roleid":5,"name":"","shortname":"student","sortorder":0}],"enrolledcourses":[{"id":3,"fullname":"Game Design","shortname":"Game Design"}]},{"id":8,"username":"jhon1.smith1","firstname":"jhon","lastname":"smith1","fullname":"jhon1 smith1","email":"jhon1.smith1@example.cat","department":"","firstaccess":0,"lastaccess":0,"description":"","descriptionformat":1,"profileimageurlsmall":"http:\/\/localhost\/moodle\/pluginfile.php\/41\/user\/icon\/f2","profileimageurl":"http:\/\/localhost\/moodle\/pluginfile.php\/41\/user\/icon\/f1","groups":[],"roles":[{"roleid":3,"name":"","shortname":"editingteacher","sortorder":0}],"enrolledcourses":[{"id":3,"fullname":"Game Design","shortname":"Game Design"}]}]

そして私が望む:

foo@example.com
bar@example.com
foo2@..
4

1 に答える 1

1

CURL_RETURNTRANSFERに変更する必要がありますCURLOPT_RETURNTRANSFER

これは によって返されるものに影響を与えているcurl_exec()ため、 で JSON 文字列を取得していません$getURL

定数が修正されると、残りのコードは正しくなります。

編集(追加):

JSON を扱うときは、読みやすいようにフォーマットすると役立ちます。私はjsonlint.com.

次に、構造を見てください。

$data = json_decode($response, true);

フルネームへのアクセスは次のenrolledcoursesようになります。

echo $data['enrolledcourses'][0]['fullname'];

または、連想配列ではなくオブジェクト表記を使用していた場合:

echo $data->enrolledcourses[0]->fullname;
于 2016-01-21T19:53:32.070 に答える