如何使用cURL调用返回数据 [英] How to return data using cURL call
问题描述
这将返回一个令牌,例如:{"token":"260e5b8adf74af6be5dfa250c5ad93c8"}
This returns a token, like: {"token":"260e5b8adf74af6be5dfa250c5ad93c8"}
我只想要内容部分:260e ...我想可以通过使用$ getInfo ['token']来获得它,对吧?
And I want just want the content part: 260e... Which I think I can get it by using $getInfo['token'], right?
$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怎么回事.
And I don't know what's going on with the 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"}]}
我怎样才能获得用户名"?或全名"(如果您知道其中有很多全名")?
How can I acces to 'username'? or 'fullname' when you know there are many 'fullname' in it?
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显示所有结果?因为它是一个多维数组.见下文.
Now $email contains jhon.smith@example.com but I have many results, how can I manage to display all of them with a foreach? Because it is a multidimentional array. See below.
[{"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@..
推荐答案
CURL_RETURNTRANSFER
需要更改为 CURLOPT_RETURNTRANSFER
.
这会影响 curl_exec()
返回的内容,因此您不会在 $ getURL
中获得JSON字符串.
That is affecting what is returned by curl_exec()
so you aren't getting the JSON string in $getURL
.
一旦常量被更正,其余的代码就正确了.
Once the constant is corrected, the rest of your code is correct.
编辑(添加):
在处理JSON时,它有助于对其进行格式化,因此更易于阅读,我使用 jsonlint.com
.
When dealing with JSON, it helps to format it so it's easier to read, I use jsonlint.com
.
然后查看结构,如果有的话:
Then looking at the structure, if you had:
$data = json_decode($response, true);
从已注册的课程
访问全名将是:
echo $data['enrolledcourses'][0]['fullname'];
或者如果您使用的是对象表示法,而不是关联数组:
Or if you were using object notation, rather than associative array:
echo $data->enrolledcourses[0]->fullname;
这篇关于如何使用cURL调用返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!