我试图通过调用moodle url来获取json数据:
https://<moodledomain>/login/token.php?username=test1&password=Test1&service=moodle_mobile_app
Moodle系统的响应格式是这样的:
{"token":"a2063623aa3244a19101e28644ad3004"}
我尝试用 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!";
}
结果是:未定义
现在的问题是: 如何处理moodle系统的json响应格式?任何想法都会很棒。
[更新]: 我通过curl使用了另一种方法,并在php.ini中更改了以下行:*extension=php_openssl.dll*,*allow_url_include = On*,但现在出现错误:注意:尝试获取非属性目的。这是更新的代码:
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'
谁能给我建议?