Services Web Services Web
John Samuel
CPE Lyon
Année: 2018-2019
Courriel: john(dot)samuel(at)cpe(dot)fr
https://www.example.com/index.html
ou
https://www.example.com/index.php
ou
https://www.example.com/
https://www.example.com/index.php?lang=fr
https://www.example.com/index.php?lang=en
https://www.example.com/index.php?lang=es
ou
https://www.example.com/fr/index.php
https://www.example.com/en/index.php
https://www.example.com/es/index.php
https://www.example.com/fr/index.php
https://www.example.com/en/index.php
https://www.example.com/es/index.php
ou
https://www.example.com/fr/
https://www.example.com/en/
https://www.example.com/es/
https://www.example.com/index.php?operation=listStudents
ou
https://www.example.com/students/
https://www.example.com/index.php?operation=showStudent
https://www.example.com/index.php?operation=AddStudent
https://www.example.com/index.php?operation=deleteStudent
https://www.example.com/index.php?operation=updateStudent
ou
https://www.example.com/students/1/
$ sudo apt install apache2 php7.0 libapache2-mod-php7.0
$ sudo a2enmod rewrite
$ sudo service apache2 restart
<?php
phpinfo()
?>
Vérifier le fichier /etc/apache2/sites-available/000-default.conf
DocumentRoot /var/www/html
..
<Directory "/var/www/html">
AllowOverride All
</Directory>
$ sudo service apache2 restart
DirectoryIndex index.php
DirectoryIndex accueil.php
DirectoryIndex accueil.php index.php
RewriteEngine on
RewriteRule ^test.html$ /index.php
RewriteEngine on
RewriteRule "^.+.html$" /index.php
RewriteEngine on
ErrorDocument 404 /404.php
RewriteRule "^(.+)/index.php$" /index.php?lang=$1
RewriteEngine on
RewriteRule "^(.+)/index.html$" /index.php?lang=$1
RewriteEngine on
RewriteRule ^students/(.+)/$ /students/index.php?id=$1
RewriteRule ^students/(.+)/(.+)$ /students/index.php?id=$1¶m=$2
RewriteEngine on
ErrorDocument 404 /404.php
<?php
$language = "fr";
if($_GET["lang"]) {
$language = $_GET["lang"];
}
if($language == "en") {
echo "Hello!";
}
else if($language == "fr") {
echo "Bonjour!";
}
?>
Une architecture REST doit respecter les six contraintes suivantes
À interface uniforme
Un client REST n'a besoin d'aucune connaissance préalable sur la façon d'interagir avec une application ou un serveur particulier au-delà d'une compréhension générique de l'hypermédia.
Différents niveaux [2]
https://www.example.com/students/
https://www.example.com/students/1/
https://www.example.com/students/1/module1/
Les codes les plus courants sont :
$ curl example.com
$ curl -I http://localhost/index.php
HTTP/1.1 200 OK
Date: Thu, 26 Apr 2018 18:54:18 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 14
Content-Type: text/html; charset=UTF-8
$ curl http://localhost/index.php
$ curl -X GET http://localhost/index.php
$ curl -X POST http://localhost/index.php
$ curl -X POST --data 'user:abc' http://localhost/index.php
$ curl -X POST -d @file.json http://localhost/index.php
$ curl -X PUT http://localhost/index.php
$ curl -X PUT --data '' http://localhost/index.php
$ curl -X PUT -d @file.json http://localhost/index.php
$ curl -X DELETE http://localhost/index.php
$_SERVER['REQUEST_METHOD']
$data = json_decode(file_get_contents('php://input'), true);
echo http_response_code(521);
RewriteEngine on
ErrorDocument 404 /404.php
RewriteRule ^(.+)/$ /index.php?resource=$1
RewriteRule ^(.+)/(.+)$ /index.php?resource=$1&id=$2