Permalink
Browse files

fix(web_services): web services again can output xml/php

Pulls in data_views via composer.
Updates docs to indicate only JSON works by default.

Fixes #8053
  • Loading branch information...
mrclay committed Feb 22, 2016
1 parent fd87e73 commit 9bf27a4b56be335b42d923ac62155cfe7437ddfb
Showing with 107 additions and 93 deletions.
  1. +1 −0 composer.json
  2. +69 −56 docs/guides/web-services.rst
  3. +37 −37 mod/web_services/tests/ElggCoreWebServicesApiTest.php
View
@@ -18,6 +18,7 @@
"tedivm/stash": "0.14.*",
"roave/security-advisories": "dev-master",
"elgg/login_as": "~1.9",
+ "elgg/data_views": "~1.0",
"zendframework/zend-mail": "~2.4",
"league/flysystem": "~1.0",
"bower-asset/jquery": "^2.1.4",
@@ -58,38 +58,40 @@ API framework:
.. code:: php
- elgg_ws_expose_function("test.echo",
- "my_echo",
- array("string" => array('type' => 'string')),
- 'A testing method which echos back a string',
- 'GET',
- false,
- false
- );
+ elgg_ws_expose_function(
+ "test.echo",
+ "my_echo",
+ [
+ "string" => [
+ 'type' => 'string',
+ ]
+ ],
+ 'A testing method which echos back a string',
+ 'GET',
+ false,
+ false
+ );
If you add this code to a plugin and then go to
-http://yoursite.com/services/api/rest/xml/?method=system.api.list, you
+http://yoursite.com/services/api/rest/json/?method=system.api.list, you
should now see your test.echo method listed as an API call. Further, to
test the exposed method from a web browser, you could hit the url:
-http://yoursite.com/services/api/rest/xml/?method=test.echo&string=testing
-and you should see xml data like this:
+http://yoursite.com/services/api/rest/json/?method=test.echo&string=testing
+and you should see JSON data like this:
-.. code:: xml
+.. code:: json
- <elgg>
- <status>0</status>
- <result>testing</result>
- </elgg>
+ {"status":0,"result":"testing"}
Plugins can filter the output of individual API methods by registering a handler
for ``'rest:output',$method`` plugin hook.
Response formats
~~~~~~~~~~~~~~~~
-The web services API framework provides three different response formats
-by default: xml, json, and serialized php. You can request the different
-formats for substituting “json” or “php” for “xml” in the above URLs.
+JSON is the default format, however XML and serialized PHP can be fetched by enabling the ``data_views``
+plugin and substituting ``xml`` or ``php`` in place of ``json`` in the above URLs.
+
You can also add additional response formats by defining new viewtypes.
Parameters
@@ -113,26 +115,27 @@ You can use additional fields to describe your parameter, e.g. ``description``.
.. code:: php
- elgg_ws_expose_function('test.greet',
- 'my_greeting',
- array(
- 'name' => array(
- 'type' => 'string',
- 'required' => true,
- 'description' => 'Name of the person to be greeted by the API',
- ),
- 'greeting' => array(
- 'type' => 'string',
- 'required' => false,
- 'default' => 'Hello',
- 'description' => 'Greeting to be used, e.g. "Good day" or "Hi"',
- ),
- ),
- 'A testing method which greets the user with a custom greeting',
- 'GET',
- false,
- false
- );
+ elgg_ws_expose_function(
+ 'test.greet',
+ 'my_greeting',
+ [
+ 'name' => [
+ 'type' => 'string',
+ 'required' => true,
+ 'description' => 'Name of the person to be greeted by the API',
+ ],
+ 'greeting' => [
+ 'type' => 'string',
+ 'required' => false,
+ 'default' => 'Hello',
+ 'description' => 'Greeting to be used, e.g. "Good day" or "Hi"',
+ ],
+ ],
+ 'A testing method which greets the user with a custom greeting',
+ 'GET',
+ false,
+ false
+ );
API authentication
------------------
@@ -174,15 +177,20 @@ parameter:
.. code:: php
- elgg_ws_expose_function("users.active",
- "count_active_users",
- array("minutes" => array('type' => 'int',
- 'required' => false)),
- 'Number of users who have used the site in the past x minutes',
- 'GET',
- true,
- false
- );
+ elgg_ws_expose_function(
+ "users.active",
+ "count_active_users",
+ [
+ "minutes" => [
+ 'type' => 'int',
+ 'required' => false,
+ ],
+ ],
+ 'Number of users who have used the site in the past x minutes',
+ 'GET',
+ true,
+ false
+ );
This function is now available and if you check ``system.api.list``, you
will see that it requires API authentication. If you hit the method with
@@ -252,14 +260,19 @@ GET HTTP requests.
.. code:: php
- elgg_ws_expose_function("thewire.post",
- "my_post_to_wire",
- array("text" => array('type' => 'string')),
- 'Post to the wire. 140 characters or less',
- 'POST',
- true,
- true
- );
+ elgg_ws_expose_function(
+ "thewire.post",
+ "my_post_to_wire",
+ [
+ "text" => [
+ 'type' => 'string',
+ ],
+ ],
+ 'Post to the wire. 140 characters or less',
+ 'POST',
+ true,
+ true
+ );
Please note that you will not be able to test this using a web browser
as you did with the other methods. You need to write some client code to
Oops, something went wrong.

0 comments on commit 9bf27a4

Please sign in to comment.