<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Llagerlof</id>
	<title>MoodleDocs - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Llagerlof"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/Special:Contributions/Llagerlof"/>
	<updated>2026-04-13T10:58:56Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Creating_a_web_service_client&amp;diff=54953</id>
		<title>Creating a web service client</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Creating_a_web_service_client&amp;diff=54953"/>
		<updated>2018-11-06T13:22:12Z</updated>

		<summary type="html">&lt;p&gt;Llagerlof: PHP library for REST added to the list&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle_2.0}}&lt;br /&gt;
[[Image:Moodle web service function documentation.jpg|thumb]]You need to know how to [https://docs.moodle.org/en/How_to_create_and_enable_a_web_service setup a web service] first.&lt;br /&gt;
To see the API Documentation, connect as Admin and go to &#039;&#039;&#039;Administration &amp;gt; Plugins &amp;gt; Web services &amp;gt; API Documentation&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Simple REST request example ==&lt;br /&gt;
&lt;br /&gt;
To quickly test the web service works you can visit the end point from the browser or via curl. For example to call a function via REST protocol:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
$ curl &amp;quot;https://your.site.com/moodle/webservice/rest/server.php?wstoken=...&amp;amp;wsfunction=...&amp;amp;moodlewsrestformat=json&amp;quot;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customise the parameters &amp;lt;tt&amp;gt;wstoken&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;wsfunction&amp;lt;/tt&amp;gt; to match the server side setup. Append additional parameters for the function call as needed. The &amp;lt;tt&amp;gt;moodlewsrestformat&amp;lt;/tt&amp;gt; parameter affects the response format and can be either &amp;lt;tt&amp;gt;xml&amp;lt;/tt&amp;gt; (default) or &amp;lt;tt&amp;gt;json&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Officially supported protocols ==&lt;br /&gt;
&lt;br /&gt;
; REST : The Moodle REST server accepts GET/POST parameters and return XML/JSON values. This server is not RESTfull.&lt;br /&gt;
; SOAP : The Moodle SOAP server is based on the Zend SOAP server (itself based on the PHP SOAP server). Zend publishes [http://framework.zend.com/manual/en/zend.soap.client.html a Zend SOAP client]. The current server implementation doesn&#039;t fully work with Java/.Net because we didn&#039;t generated a fully describe WSDL yet. If you are working on a Java/.Net client, follow or participate to the tracker issues MDL-28988 / MDL-28989&lt;br /&gt;
; XML-RPC : The Moodle XML-RPC server is based on Zend XML-RPC server. Zend also publishes [http://framework.zend.com/manual/en/zend.xmlrpc.client.html a Zend XML-RPC client].&lt;br /&gt;
; AMF : the Moodle AMF server is based on the Zend AMF server. The test client can be found in &#039;&#039;Settings blocks &amp;gt; Site Administration &amp;gt; Development &amp;gt; Web service test client &amp;gt; AMF Test client&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== Demo client examples ==&lt;br /&gt;
&lt;br /&gt;
Demo client sample codes can be downloaded on [https://github.com/moodlehq/sample-ws-clients Github].&lt;br /&gt;
&lt;br /&gt;
For HTML5 app creators, you can also find:&lt;br /&gt;
* a nice [https://github.com/jleyva/umm phonegap / Jquery mobile template]&lt;br /&gt;
* a proof of concept of [http://moodle.org/mod/forum/discuss.php?d=189882 javascript cross-domain with Sencha Touch 1.1]&lt;br /&gt;
&lt;br /&gt;
Node.js apps can use the [https://github.com/mudrd8mz/node-moodle-client moodle-client] module.&lt;br /&gt;
&lt;br /&gt;
A [http://moodle.org/mod/forum/discuss.php?d=199453 Java Library for REST] can be found on [http://sourceforge.net/projects/moodlerestjava/ Sourceforge].&lt;br /&gt;
&lt;br /&gt;
A [https://github.com/llagerlof/MoodleRest PHP Library for REST] can be found on GitHub.&lt;br /&gt;
&lt;br /&gt;
== How to get a user token ==&lt;br /&gt;
{{Moodle_2.2}}&lt;br /&gt;
Your client can call the script located in /login/token.php with a simple HTTP request. We highly recommend to do it securely with HTTPS.&lt;br /&gt;
The required parameters are:&lt;br /&gt;
* username&lt;br /&gt;
* password&lt;br /&gt;
* service shortname - The service shortname is usually hardcoded in the pre-build service (db/service.php files). Moodle administrator will be able to edit shortnames for service created on the fly: MDL-29807. If you want to use the Mobile service, its shortname is &amp;lt;tt&amp;gt;moodle_mobile_app&amp;lt;/tt&amp;gt;. Also useful to know, the database shortname field can be found in the table named external_services.&lt;br /&gt;
&lt;br /&gt;
Request:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
https://www.yourmoodle.com/login/token.php?username=USERNAME&amp;amp;password=PASSWORD&amp;amp;service=SERVICESHORTNAME&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Response:&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
{token:4ed876sd87g6d8f7g89fsg6987dfh78d}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Difference between Moodle versions ===&lt;br /&gt;
&lt;br /&gt;
* Moodle 2.2 and later: the script can generate user tokens for any service shortname (of course users must be allowed on the service, see [[:en:How to create and enable a web service|How to create and enable a web service]]).&lt;br /&gt;
* Moodle 2.1: the script can only generate tokens for the official built-in mobile service. However the script can returns tokens for other services, they just need to have been previously generated.&lt;br /&gt;
&lt;br /&gt;
=== About service shortname ===&lt;br /&gt;
&lt;br /&gt;
At the moment a service can have a shortname if you:&lt;br /&gt;
* create the service as a built-in service (in db/services.php files) &lt;br /&gt;
* add the shortname manually in the DB. Note: we&#039;ll add the admin UI for shortname later (MDL-30229)&lt;br /&gt;
&lt;br /&gt;
== Text formats ==&lt;br /&gt;
=== Moodle 2.0 to 2.2 ===&lt;br /&gt;
{{Moodle_2.0}}&lt;br /&gt;
HTML is the format sent/received by web service functions. All returned file urls are converted to &#039;http://xxxx/webservice/pluginfile.php/yyyyyyyy&#039;&lt;br /&gt;
&lt;br /&gt;
=== Moodle 2.3 and later ===&lt;br /&gt;
{{Moodle_2.3}}&lt;br /&gt;
Since Moodle 2.3 you can add few GET/POST parameters to your request (for devs who have a good knowledge of File API and format_text()):&lt;br /&gt;
* &#039;&#039;moodlewssettingraw&#039;&#039; =&amp;gt; false by default. If true, the function will not apply format_text() to description/summary/textarea. The function will return the raw content from the DB.&lt;br /&gt;
* &#039;&#039;moodlewssettingfileurl&#039;&#039; =&amp;gt; true by default, returned file urls are converted to &#039;http://xxxx/webservice/pluginfile.php/yyyyyyyy&#039;. If false the raw file url content from the DB is returned (e.g. @@PLUGINFILE@@)&lt;br /&gt;
* &#039;&#039;moodlewssettingfilter&#039;&#039; =&amp;gt; false by default. If true, the function will filter during format_text()&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Web services|Web services developer documentation]]&lt;br /&gt;
* [[:en:Web_services|Web services user documentation]]&lt;br /&gt;
* [[Creating a web service and a web service function | Implement a web service and a web service function]]&lt;br /&gt;
* [[Web_services_Roadmap|Web service Roadmap]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Web Services]]&lt;/div&gt;</summary>
		<author><name>Llagerlof</name></author>
	</entry>
</feed>