9

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?

bofanda
  • 10,386
  • 8
  • 34
  • 57

1 Answers1

32

json_decode() expects a string, not a URL. You're trying to decode that url (and json_decode() will NOT do an http request to fetch the url's contents for you).

You have to fetch the json data yourself:

$json = file_get_contents('http://...'); // this WILL do an http request for you
$data = json_decode($json);
echo $data->{'token'};
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • thank you for your reply, I've changed it to **file_get_contents('https://...')**..., but still the same result **undefined** My PHP Version 5.4.7 – bofanda Jan 10 '13 at 16:51
  • 1
    then echo out the file_get_contents results to see what you get. is it really json? maybe you've got magic_quotes enabled and PHP is "helpfully" being a jerk and mangling the string on you. Maybe that url doesn't output json at all. – Marc B Jan 10 '13 at 16:52
  • I followed to this link: http://docs.moodle.org/dev/Creating_a_web_service_client#How_to_get_a_user_token it is not a json resnponse? – bofanda Jan 10 '13 at 17:02
  • doesn't matter what the docs say - if there's a problem with the request/url, you may not get json back. dump out what your script is receiving. e.g. `echo file_get_contents(...)`. – Marc B Jan 10 '13 at 17:06
  • I've changed the code with CURL (see above), but now result says, that I'm trying to get property of non-object. Have you any idea? Thanks in advance. – bofanda Jan 10 '13 at 19:21
  • 1
    @Dozent I know it's been a long time my friend since you have asked this, but because I came across the same issue, please see the small edit of @Mark B code: `` – Stelios Voskos Jan 23 '14 at 11:45