Deprecated: Array and string offset access syntax with curly braces is deprecated in /home/unareil/www/wiki/inc/init.php on line 557
restler [Wiki Unareil]

Outils pour utilisateurs

Outils du site


restler

Ceci est une ancienne révision du document !



Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/unareil/www/wiki/inc/parser/handler.php on line 1552

====== Créer votre propre API REST avec Restler

Ce tutoriel utilise le programme open source Restler 3.0 de Luracast pour créer une API REST.

Commençons par quelque chose de simple : nous allons créer une classe Dis.php qui quand elle sera appelé renverra bonjour suivi du paramètre passer à l'URL.

class Dis {

function bonjour($par='le monde') {

return “Bonjour $par!”;

}

}

Maintenant, nous sommes prêt à construire l'index pour le script de notre API. Commençons par télécharger le code Restler et à indiquer qu'il est requis dans notre page d'index.

require_once '../vendor/restler.php'; use Luracast\Restler\Restler; use Luracast\Restler\Defaults; Defaults::$smartAutoRouting = false;

La prochaine étape est d'initialiser l'objet Restler et de spécifier que la classe que nous utilisons est exposée à la plateforme Restler par la méthode GET

$r = new Restler(); $r→addAPIClass('Say'); $r→handle(); There you have it – the Restler platform takes care of the rest when the URL is visited and exposes the following URL: index.php/say/hello/Richard Askew Bingo – you have created a class and exposed a method to a URL that returns information to the user. Now let’s move onto something a little more useful and a little more structured.

Result from the first GET request – when you send the name parameter it is appended to a the end of the string and displayed

Next steps For this example we are going to create an API that enables us to read and manipulate data. First of all we’re going to need to create a simple database. For the time being, create just one table called person with the following fields: person_id, name and email_address.

So this time we’re going to work with a database of people that will be searched, added to, updated and deleted. As in our earlier example we’ll be creating a class that uses a GET request – but this time it models a person. This method takes an ID and returns the information we store about that person in the database. This class references and uses a database class that isn’t going to be covered here – but the files you need are in the support download. use Luracast\Restler\RestException; class Person { static $FIELDS = array('name', 'email_address'); protected $db; function __construct(){ $this→db = newMysqlDb('host', 'username', 'password', 'database'); } function get($id=NULL) { if($id != NULL){ $this→db→where('person_id', $id); } $results = $this→db→get('person'); return $results; } } To expose the method to our API we need to create an index file that is very similar to the one used in the ‘hello world’ example. Simply pass Person into your addAPIClass method call instead of Say. You will also need to connect to your database. require_once '../vendor/restler.php'; use Luracast\Restler\Defaults; Defaults::$smartAutoRouting = false; $r = new Restler(); $r→addAPIClass(‘Person’); $r→handle(); If you now visit the URL localhost/index.php/person/1, Restler uses the GET method – because that’s the HTTP method you are using – and returns the information in our database that matches an ID of 1. If you don’t specify an ID for a person then all records are returned.

The view of the person database in PhpMyAdmin – notice this very simple database has three fields and the person_id is set to auto_increment

Displaying errors As with all web-based systems providing useful and consistent error codes is extremely important, especially if you are building something that others can integrate into their own systems. Restler allows you to define an error code and output a message to the user. Error codes reflect the HTTP status codes: 1xx - Informational 2xx - Success 3xx - Redirection 4xx - Client Error 5xx - Server Error function get($id = NULL) { if($id != NULL){ $this→db→where('person_id', $id); } $results = $this→db→get('person'); if(empty($results) && $id != NULL){ throw new RestException(400, “This ID doesn't exist”); } return $results; } Now if you visit the localhost/index.php/person/10, you should be presented with the error code and error message:

Restler enables errors to be displayed to the user if you wish; when this is thrown the script doesn’t execute any further actions

Formats You may have noticed that Restler is displaying the data in JSON. This is the default setting, but you can configure your system to return JSON, XML or enable the user to select which one they want to get back. In order to do this, we simply add a new line of code immediately after we call Restler in our index page: $r→setSupportedFormats('XmlFormat','JsonFormat'); You can see that we have specified XML and JSON, the fact that XmlFormat is listed first means that the data is returned in this format by default. If you now visit localhost/index.php/person/1 the data is returned in XML format and you can switch back to JSON by using the following URL localhost/index.php/person.json/1.

If required the returned data can be displayed in XML format: you pass an array to Restler and it does the rest

Voici la source anglophone d'où provient cet article écrit par Richard Askew : https://hungrydevelopers.blogspot.com/2013/08/create-your-own-api-with-restler.html

restler.1559333989.txt.gz · Dernière modification: 2019/05/31 22:19 par unareil