LMSouq
moodle-core Open

Getting JSON Object by calling a URL with parameters in PHP

CO
Community Contributor
1 month ago
3 views
Problem Description
I'm trying to get json data by calling moodle url: https://<moodledomain>/login/token.php?username=test1&password=Test1&service=moodle_mobile_app the response **format** of moodle system is like this: {"token":"a2063623aa3244a19101e28644ad3004"} The result I tried to process with PHP: if ( isset($_POST['username']) && isset($_POST['password']) ){ // test1 Test1 // request for a 'token' via moodle url $json_url = "https://<moodledomain>/login/token.php?username=".$_POST['username']."&password=".$_POST['password']."&service=moodle_mobile_app"; $obj = json_decode($json_url); print $obj->{'token'}; // should print the value of 'token' } else { echo "Username or Password was wrong, please try again!"; } Result is: **undefined** **Now the question:** How can I process the json response **format** of moodle system? Any idea would be great. **[UPDATE]:** I have used another approach via **curl** and changed in **php.ini** following lines: *extension=php_openssl.dll*, *allow_url_include = On*, but now there is an error: **Notice:** Trying to get property of non-object. Here is the updated code: function curl($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); curl_close($ch); return $data; } $moodle = "https://<moodledomain>/moodle/login/token.php?username=".$_POST['username']."&password=".$_POST['password']."&service=moodle_mobile_app"; $result = curl($moodle); echo $result->{"token"}; // print the value of 'token' Can anyone advise me?

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base