Code Monkey home page Code Monkey logo

rest_api's Introduction

REST API

Installation

  1. Download the rest_api extension and add it to your extensions folder
  2. Enable the extension from the System > Extensions page in Symphony

Usage

The API is a series of plugins (in the plugins) folder which control the read/write of data from various parts of Symphony. There are presently three plugins:

  • Authors: read meta data about authors
  • Entries: read and write entries
  • Sections: read meta data about sections and fields

An API plugin is instantiated using then following URL:

/symphony/api/:plugin

For example the root of the Authors API plugin is at:

/symphony/api/authors

Details on how to create plugins is included later in this README.

Response formats

By default the API returns XML but JSON, YAML and serialised PHP arrays are also supported by appending the format variable to any URL.

/symphony/api/entries/articles/?format=xml
/symphony/api/entries/articles/?format=json
/symphony/api/entries/articles/?format=yaml
/symphony/api/entries/articles/?format=serialise

Authentication and security

The API is private. You must authenticate as a Symphony author in one of two ways:

  1. Log in to Symphony (obtain the cookie) and send that with your request
  2. Pass a auth-token querystring value in the call to the API (either GET querystring or POST values). The token is the hash portion of your "remote login" URL for your user account. This only works when "allow remote login" is enabled.

An example token might look like this. It needs to be passed with every request.

/symphony/api/entries/articles/?auth-token=8ca221bb

I suggest sandboxing the API to a single user account. I usually create an "API" user in Symphony and use this author's token for all requests.

Authors plugin

The Authors plugin provides information about your Symphony authors (user accounts).

To list all authors:

/symphony/api/authors

To read a specific author, pass the author ID or username:

/symphony/api/authors/1
/symphony/api/authors/nickdunn

Example XML response looks like:

<response>
	<author>
		<id>1</id>
		<username>nickdunn</username>
		<password>b94a8fe5cab12ba61c4c9973d391e987982fccd4</password>
		<first-name>Nick</first-name>
		<last-name>Dunn</last-name>
		<email>[email protected]</email>
		<last-seen>2011-03-28 07:45:14</last-seen>
		<user-type>developer</user-type>
		<primary>yes</primary>
		<auth-token-active>yes</auth-token-active>
	</author>
</response>

Sections plugin

Incomplete documentation. Eventually similar documentation to Section Schemas extension.

Entries plugin

The Entries plugin provides the same functionality of data sources (read entries) and events (create and update entries).

To list entries from a section:

/symphony/api/entries/:section_handle

To read a specific entry from a section:

/symphony/api/entries/:section_handle/:entry_id

The default XML response looks like:

<response>
	<pagination total-entries="3" total-pages="1" entries-per-page="10" current-page="1"/>
	<section id="1" handle="articles">Articles</section>
	<entry id="1">
		...
	</entry>
	<entry id="2">
		...
	</entry>
	<entry id="3">
		...
	</entry>
</response>

When reading entries from a section, querystring parameters can be added for finer control:

  • fields a comma-delimited list of field handles (XML element names) to include for each entry
  • limit the number of entries to return per page
  • page the page number, if pagination is being used
  • sort the field handle (XML element name) to sort by
  • order the sort direction (asc, desc, rand)
  • groupby the field handle (XML element name) to group by

For example to get a list of the latest 5 entries from a section "Articles":

/symphony/api/entries/articles/?fields=title,body,date&limit=5&sort=date&order=desc

Pagination can be returned by adding system:pagination to the value of the fields list e.g.

/symphony/api/entries/articles/?fields=title,body,system:pagination

Additionally you can filter entries using data source filtering syntax. Use a filter array in the querystring:

/symphony/api/entries/articles/?filter[title]=regexp:Nick&filter[date]=later+than+today

To create an entry you can send an HTTP POST to the section URL. The format of the POST should follow exactly the field names for a normal Symphony event, i.e. fields[title]. For example:

<form method="post" action="/symphony/api/entries/articles">
	<input name="fields[title]" />
	<textarea name="fields[content]"></textarea>
	<input type="submit" />
</form>

The XML result looks like:

<response id="..." result="success" type="created">
	<message>Entry created successfully.</message>
	<post-values>
		<title>...</title>
		<content>...</title>
	</post-values>
</response>

Multiple entries can be created by sending arrays of fields e.g. fields[0][title], fields[1][title], just as with a normal Symphony event.

To update an existing entry, you have two options. Either include an id in the POST array and post it to the section handle (as above), or omit the id and post to the entry URL directly. For example:

/symphony/api/entries/articles/31

The XML response looks like:

<response id="31" result="success" type="edited">
	<message>Entry edited successfully.</message>
	<post-values>
		...
	</post-values>
</response>

To delete an entry send an HTTP DELETE request to the entry's URL (the same URL as if reading the entry's data), such as:

curl -X DELETE /symphony/api/entries/articles/123

The XML response looks like:

<response id="123" result="success" type="deleted">
	<message>Entry deleted successfully.</message>
</response>

Anatomy of an API plugin

A plugin follows the following naming convention:

/extensions/rest_api/plugins/{name}/rest.{name}.php

This file should contain a class named REST_{Name}.

Each plugin class can implement six public methods. All are optional (but omitting all of them would lead to a pretty useless plugin...).

init()

This is the first plugin function that is called on each request. It is used to build objects and do any initial work that may be required for the additional methods.

authenticate()

If you need any custom authentication rules, put them here. Perhaps you want to limit your plugin access to a specific user account, or Developers only. You should restrict access by sending a 403:

REST_API::sendError("You are not permitted to view this plugin.", 403);

This will terminate the response and send an error to the client.

The remaining four methods represent the four HTTP methods (GET, POST, PUT and DELETE). If the method is omitted from the plugin, an "unsupported" response will be returned to the client.

get()

This function is run when the client performs a GET request to your plugin. This is for read-only requests.

post()

This function is run when the client performs a POST request to your plugin. This is for write operations, to create a new or update existing resource.

put()

This function is run when the client performs a PUT request to your plugin. This is for write operations, to create a new resource.

delete()

This function is run when the client performs a DELETE request to your plugin. This is for write operations to delete a resource.

Shell access

If you use the Symphony Shell extension you can access the REST API on the command line. It's no longer using HTTP, but access is the same. An HTTP request that previously looked like:

/symphony/api/entries/articles/2/?format=json&method=get

Maps on to the following Shell command:

php symphony -t {token} rest_api request -path entries/articles/2 -format json -method get

An explanation of the above:

  • php symphony -t {token} is saying use php to instantiate the symphony script, passing {token} (replace this with an Author access token)
  • rest_api request is saying use the request script from the rest_api extension
  • -path the/path/here passes the REST URL as if you were using the REST API via HTTP
  • -format json is the equivalent of ?format=json
  • -method get is the equivalent of an HTTP GET

rest_api's People

Contributors

bernardodiasc avatar brendo avatar macncheeze avatar nickdunn avatar nitriques avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rest_api's Issues

Error 500 class REST_Entries not found !

I've got this error when i try to access to the page Union Datasource in the backend page.
PHP Fatal error: Class 'REST_Entries' not found in /var/www/folder/subfolder/extensions/rest_api/ data-sources/data.rest_api_entries.php on line 34
-> Symphony 2.2.5

REST API for 2.6.4

I am trying using the rest API which is installed in version 2.6.4 but I am unable to access it.

Is the API supported with symphony 2.6.4?

While consuming the API with firefox RESTClient I am getting below error:


Fatal error: Class 'Symphony' not found in C:\xampp\htdocs\symphony\symphony\lib\boot\bundle.php on line 17

Please provide support

Regards
Sonam

REST API Entries Event 500 ERROR

I've just installed the REST API Extension in Symphony 2.3.2 but I get 500 Error when accessing the REST API Entries Event. How could I make the browser give me a full error report rather than a blank page in Chrome? Also any ideas what the error could be and how to fix it?

Many thanks.

Incorrect case in 'require' statements

There are several lines like this:

require_once('class.XMLToArray.php');

But actually the file is called class.xmltoarray.php. On PHP 5.3 I experienced errors because of this.

htaccess rules incorrect

When symphony is installed in a subfolder the rewrite rule is wrong. It assumes that Symphony is in the root. Needs something to detect subfolderness.

POST json schema?

Hey guys, love the extension.

I have one small issue, I can't seem to figure out the json data schema when POSTing to an endpoint for entry creation. Can you please advise? (The documentation only shows a http form POST example)

I have already tried the following...

{"first-name": "Damien", "last-name": "Oliver", email: "[email protected]"}

{fields:{"first-name": "Damien", "last-name": "Oliver", email: "[email protected]"}}

{"fields['first-name']": "Damien", "fields['last-name']": "Oliver", "fields[email]": "[email protected]"}

{"first-name":{"value":"Damien"},"email":{"value":"[email protected]"}}

...with no luck, the response is always:

{"response":{"_result":"error","message":{"_message-id":"102","value":"Entry encountered errors when saving."},"first-name":{"_label":"First Name","_type":"missing","_message-id":"301","_message":"\u2018First Name\u2019 is a required field.","value":""},"post-values":{"value":""}}}

Thanks guys!, I'm sure it is something simple.

Members Extension Compatibility

It seems Members and the REST/API extensions don't play well together?

Fatal error: Call to a member function isLoggedIn() on a non-object in /extensions/members/extension.driver.php on line 808

when making a call to /api/entries/{section}/

filter[system:creation-date] not working

Sym 2.7.10
PHP 7.3
OS Linux

API filter on system date not working same as back-end UI filter on system date

Steps to reproduce:

  1. Navigate to a section with entries created this year (or date of your choice)
  2. Use the back-end filter UI to filter on System Creation Date: is 2020 - expected entries returned
  3. Repeat step 2 with is 2019 - expected entries returned
  4. Copy the relevant URL parameter to apply it to the API URL, e.g. /symphony/api/entries/section-handle/?filter[system:creation-date]=2020 - all entries are returned unfiltered
  5. Repeat step 4 with 2019 - all entries returned unfiltered

Other field filters appear to be working as expected.

Classname clash in conjunction with SymQL

When the SymQL extension is installed in parallel, a classname clash occurs:

Fatal error: Cannot redeclare class XMLToArray in /var/www/maschine/extensions/rest_api/lib/class.xmltoarray.php on line 6

Symphony 2.6 bootstrapping

Great extension!

I did a fresh build today and came up against the following issue.

The handler will not work (in 2.6+) without adding the autoload script first, would be great if you could could create some kind of conditional DOCROOT checker so this extension continues to work on future symphony releases.

require_once(DOCROOT . '/vendor/autoload.php');

Filter Logic

I want to return all entries which have an empty/null field. Tried a few things i think should work?

/symphony/api/entries/messages?filter[syncid]=sql: NULL
/symphony/api/entries/messages?filter[syncid]=""
/symphony/api/entries/messages?filter[syncid]=not-regex=^$

Its also with pointing out that these fields down show up in the XML if they are empty/null. (Thats not a problem though)

Any ideas?

simplexml_load_string() expects parameter 1 to be string, object given

Hello, I get this error with Symphony 2.4:


Symphony Warning: simplexml_load_string() expects parameter 1 to be string, object given

148 public function sendOutput($xml) {
149
150 $dom = simplexml_load_string($xml);
151 switch(REST_API::getHTTPMethod()) {
152 case 'get':
153 $xml = $dom->xpath('/data/response');
154 if(is_array($xml)) $xml = reset($xml);


I tried to convert this object but I did not succeed.

Error when using filters

When I use a filter like: filter[category]=test, the following error is returned: Symphony Warning: urldecode() expects parameter 1 to be string, array given

An error occurred in /home/oleae/www/symphony/lib/toolkit/class.frontendpage.php around line 367

362
363                     // If the key gets replaced out then it will break the XML so prevent
364                     // the parameter being set.
365                     if(!General::createHandle($key)) continue;
366
367                     $this->_param['url-' . $key] = XMLElement::stripInvalidXMLCharacters(utf8_encode(urldecode($val)));
368                 }
369             }
370
371             if(is_array($_COOKIE[__SYM_COOKIE_PREFIX_]) && !empty($_COOKIE[__SYM_COOKIE_PREFIX_])){

using Symphony 2.3 and PHP 5.4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.