Getting "Page Not Found" when trying to use web service

Getting "Page Not Found" when trying to use web service

by marcel maillet -
Number of replies: 3

I am new to moodle, and trying to figure out how to use some web services. I found this code on another post and decided to try it, but I get a page not found as result. Anybody can help me out ?

Moodle is installed on my machine (localhost), and I am making the request from another server.

Here is my code:

<?php

$params = array(
'userids' => array(2),
'wsfunction' => 'moodle_user_get_users_by_id',
'wstoken' => '<token>'
);

$postdata = http_build_query($params);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/moodle/webservice/rest/server.php');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$data = curl_exec($ch);
curl_close($ch);

print "<pre>";
var_dump($data);
print "</pre>";
?>

Average of ratings: -
In reply to marcel maillet

Re: Getting "Page Not Found" when trying to use web service

by Patrick Pollet -

@marcel,

 

   Your code seems almost good and is very close of the one we are using in autogenerated clients for OK Tech in REST mode, see https://github.com/patrickpollet/moodlews/blob/master/wspp/clients/classes/mdl_soapserverrest.php#L247

I just see two potential errors :

'wstoken' => '<token>'  ;   <token> should be replaced by the long hex digits sequence generated in user's profile (see official WS docs on setup) or by  a couple of

 'wsusername'=>'somemoodlelogin',
'wspassword'=>'itspassword ',
 

Since you are calling Moodle installed on your local machine from another server, the URL set here

curl_setopt($ch, CURLOPT_URL, 'http://localhost/moodle/webservice/rest/server.php');

should be 

curl_setopt($ch, CURLOPT_URL, 'http://DNS_OR_IP_OF_YOURMOODLE_SERVER/moodle/webservice/rest/server.php');

localhost is definitively wrong since it means 'me ' on your other server... so the 404 error

Cheers.

Edit : if you are using the login/password way, replace server.php by simpleserver.php wink 

 

 

 

In reply to Patrick Pollet

Re: Getting "Page Not Found" when trying to use web service

by marcel maillet -

Thanks, but I found the problem just after posting this question. It was localhost problem described in previous reply.