PHP
                                                     Web Services




           PHP et Web Services
11 février 2010 - Master CCI - Université de Tours




            Guillaume Jarysta-Dautel - Devolia CEO
PHP
                                                                      Web Services

                               Plan de la présentation 1/2

1.   PHP : notions d’environnement
       • Architecture CGI / CLI
       • Cron
       • Core et modules
       • Interface Apache et mod Apache
       • Interface OS
       • Interface SGBD
       • php.ini et .htaccess

2. PHP : notions d’architecture
      • Repository, Dev et Prod

3. PHP : notions de génie logiciel
      • Templating
      • Modèles de données SQL
      • Multilinguisme                         ETUDE DE CAS
      • Modèle objet PHP5                      • devolia.org
      • Documentation                          • secure.devolia.com
      • API interne / externe                  • rienamemettre.com
      • Sécurité
PHP
                                                                                     Web Services

                                Plan de la présentation 2/2

4.   Web Services : notions d’architecture
       • XML-RPC, SOAP
       • XML et JSON
       • Architecture d’un listener
       • Web Services asynchrones : ping et callback
       • Sécurité d’un Web Service                            ETUDE DE CAS
                                                              • devolia.org
5. Web Services : notions de génie logiciel en PHP            • secure.devolia.com
      • Moteur de session                                     • rienamemettre.com
      • SimpleXML                                             • geotwitt
      • JSON
      • Gestion des en-têtes

6. Web Services : architecture avancée
      • Montée en version d’une API externe
      • Gestion de la charge
PHP
                      Web Services

PHP professionnel ?
PHP
                                                                                 Notions d’environnement

                                      PHP - notions d’environnement

1.    Architecture CLI / CGI



              Système                                   Serveur             Module
           d’exploitation                                Web                 CGI

       -   Linux                                  -   Apache          -   PHP
       -   Windows                                -   Lighttpd        -   Perl
       -   FreeBSD                                -   IIS             -   C
       -   ...                                    -   ...             -   ...




                                                                            Sources
                                                        Module
                                                         CLI          - PHP :
                                                                      compilation à la
                                                  -   PHP             volée
                                                  -   Perl
                                                  -   C
                                                  -   ...

Ressources
     http://php.net/manual/fr/migration5.cli-cgi.php
PHP
                                                                                Notions d’environnement

                                     PHP - notions d’environnement

2.    Cron




              Système                             Cron                     Module
           d’exploitation                                                   CLI
                                             - Exécution
       -   Linux                             périodique              -   PHP
       -   Windows                                                   -   Perl
       -   FreeBSD                                                   -   C
       -   ...                                                       -   ...




                                                                           Sources

                                                                     - PHP :
                                                                     compilation à la
                                                                     volée



Ressources
     http://doc.ubuntu-fr.org/cron
PHP
                                                                                          Notions d’environnement

                                                  PHP - notions d’environnement

            3. Core et modules


   RESEAU          CRYPTAGE               SGBD            GRAPHISME       OUTILS     COMPRES-         BAS
                                                                                       SION          NIVEAU
soap             mcrypt              mysql               gd            calendar
xmlrpc           ...                 pdo                 jpeg          mbstring    zlib           pcntl
openssl                              postgresql          png           ...         ...            ...
ftp                                  ...                 pdf
curl                                                     ...
kerberos
...




            Ressources
              http://php.net/manual/fr/faq.installation.php
PHP
                                                                          Notions d’environnement

                               PHP - notions d’environnement

4. Modules Apache




           Apache                        Modules                     Module
                                                                      CLI
    -   Linux                          mod_ssl
    -   Windows                        mod_rewrite             -   PHP
    -   FreeBSD                        mod_proxy               -   Perl
    -   ...                            ...                     -   C
                                                               -   ...




                                                                     Sources

                                                               - PHP :
                                                               compilation à la
                                                               volée



Ressources
  http://modules.apache.org/
PHP
                                                                                             Notions d’environnement

                                   PHP - notions d’environnement

4. Modules Apache - exemple mod_rewrite

Requête
  GET /romans/suskind/le_parfum.html


mod_rewrite
  RewriteRule ^/([^/]+)/([^/]+)/([^.]+).html   store.php?rayon=$1&auteur=$2&titre=$3


PHP - store.php
  <h1>Bienvenue dans notre rayon <?php print($_REQUEST[‘rayon‘]); ?></h1>
  <h2>Votre livre : <?php print($_REQUEST[‘titre‘]); ?> (<?php print($_REQUEST[‘auteur‘]); ?>)</h2>
PHP
                                                                                 Notions d’environnement

                                   PHP - notions d’environnement

5. Interface OS

Exécution d’un programme serveur
  $return=exec('ln -s /home/');
  print($return);




pcntl
  $pid = pcntl_fork();
  if($pid == -1){ die('Cannot fork. Too bad, I wanna fork so bad !'); }
  elseif($pid){
     // le père
     print('Je suis ton père ('.posix_getpid().'), Luke ('.$pid.') !'."rn");
  }
  else{
     // le fils
     print('Noooon ('.posix_getpid().') !'."rn");
  }




Ressources
  http://fr.php.net/manual/fr/book.exec.php
  http://fr.php.net/manual/fr/book.pcntl.php
PHP
                                                                      Notions d’environnement

                                   PHP - notions d’environnement

6. Interface SGBD




Connexion à MySQL
  $link=mysql_connect('localhost','login','password');
  $db=mysql_select_db('database_name', $link);




Couche d’abstraction - PDO
  $dbh = new PDO('odbc:sample', 'db2inst1', 'ibmdb2');

  $count = $dbh->exec("DELETE FROM fruit WHERE couleur = 'rouge'");

  print("Retourne le nombre de lignes effacées :n");
  print("Effacement de $count lignes.n");




Ressources
  http://fr.php.net/manual/fr/book.pdo.php
PHP
                                                                   Notions d’environnement

                                   PHP - notions d’environnement

7. php.ini et .htaccess




Lecture de la configuration PHP
  phpinfo();




Droits limitatifs
  AuthType Basic
  AuthName "Password Required"
  AuthUserFile /www/passwords/password.file
  AuthGroupFile /www/passwords/group.file
  Require Group admins




Ressources
  http://fr.php.net/manual/fr/configuration.php
  http://httpd.apache.org/docs/1.3/howto/htaccess.html
PHP
                                                                           Notions d’architecture

                             PHP - notions d’architecture

1. Repository, dev et prod



                                            dev                     prod

                                    -   Error on            -   Error silent
                                    -   Warning on          -   Warning silent
                                    -   Audit on            -   Audit off
                                    -   Debug on            -   Debug off



      Développeurs

                                                                Repository

                                                            - Garant de
                                                            l’intégrité, de la
                                                            gestion des
                                                            versions, du
                                                            travail
                                                            collaboratif
PHP
                                                        Notions de génie logiciel

                      PHP - notions de génie logiciel

                              ETUDE DE CAS

•   Templating
•   Modèles de données SQL
•   Multilinguisme
•   Modèle objet PHP5
•   Documentation
•   API interne / externe
•   Sécurité
Web Services
                                                                    Notions d’architecture

                                           Web Services

Usages

   •     Consommation   d’un   service   tiers
   •     Consommation   d’un   service   métier
   •     Consommation   d’un   service   critique
   •     Consommation   d’un   service   asynchrone
   •     Consommation   d’un   service   en situation de mobilité
Web Services
                                                                              Notions d’architecture

                               Web Services - notions d’architecture

1. XML-RPC, SOAP, Corba, IEC, WSDL, à façon... le choix de l’architecture

        •   Type de besoin : privé / public
        •   Besoin d’une vérification simplifiée de la cohérence du message
        •   Poids moyen des échanges
        •   Nature des consommateurs du service
        •   Besoin de certification
        •   Besoin en niveau d’abstraction : WSDL
        •   Interopérabilité des Web Services




Ressources
  http://fr.wikipedia.org/wiki/SOAP
  http://fr.wikipedia.org/wiki/XML-RPC
Web Services
                                                                Notions d’architecture

                        Web Services - notions d’architecture

2. XML et JSON

      • Avantages de XML
        • Interopérabilité
        • Formalisme rigoureux
        • Parseurs multilangages
        • Explicitement UNICODE

      • Inconvénients de XML
        • Lourdeur des en-têtes
        • Rigidité du formalisme


      • Avantages de JSON
        • Interprétation simple en Javascript
        • Encodage / décodage intégrés à PHP 5
        • Flexibilité de l’encodage

      • Inconvénients de XML
        • Parseurs généralement non-natifs
        • Peu robuste
Web Services
                                                           Notions d’architecture

                   Web Services - notions d’architecture

                               ETUDE DE CAS

• Architecture d’un listener
• Web Services asynchrones : ping et callback
• Sécurité d’un Web Service
Web Services
                                                              Notions de génie logiciel

                   Web Services - notions de génie logiciel

                               ETUDE DE CAS

•   Moteur de session
•   SimpleXML
•   JSON
•   Gestion des en-têtes
Web Services
                                                         Architecture avancée

                   Web Services - Architecture avancée

• Montée en version d’une API externe
• Gestion de la charge
PHP
                                                        Web Services




Guillaume Jarysta-Dautel   Devolia Europe
+33 (0) 683 658 342        21 boulevard de Sébastopol
guillaume@devolia.fr       75001 Paris - FRANCE
                           www.devolia.fr
www.jarysta.com




                           Devolia North America
                           1138 Broadway, Suite 706
                           New York, NY 10010 – USA
                           www.devolia.com

Conference Php Web Services

  • 1.
    PHP Web Services PHP et Web Services 11 février 2010 - Master CCI - Université de Tours Guillaume Jarysta-Dautel - Devolia CEO
  • 2.
    PHP Web Services Plan de la présentation 1/2 1. PHP : notions d’environnement • Architecture CGI / CLI • Cron • Core et modules • Interface Apache et mod Apache • Interface OS • Interface SGBD • php.ini et .htaccess 2. PHP : notions d’architecture • Repository, Dev et Prod 3. PHP : notions de génie logiciel • Templating • Modèles de données SQL • Multilinguisme ETUDE DE CAS • Modèle objet PHP5 • devolia.org • Documentation • secure.devolia.com • API interne / externe • rienamemettre.com • Sécurité
  • 3.
    PHP Web Services Plan de la présentation 2/2 4. Web Services : notions d’architecture • XML-RPC, SOAP • XML et JSON • Architecture d’un listener • Web Services asynchrones : ping et callback • Sécurité d’un Web Service ETUDE DE CAS • devolia.org 5. Web Services : notions de génie logiciel en PHP • secure.devolia.com • Moteur de session • rienamemettre.com • SimpleXML • geotwitt • JSON • Gestion des en-têtes 6. Web Services : architecture avancée • Montée en version d’une API externe • Gestion de la charge
  • 4.
    PHP Web Services PHP professionnel ?
  • 5.
    PHP Notions d’environnement PHP - notions d’environnement 1. Architecture CLI / CGI Système Serveur Module d’exploitation Web CGI - Linux - Apache - PHP - Windows - Lighttpd - Perl - FreeBSD - IIS - C - ... - ... - ... Sources Module CLI - PHP : compilation à la - PHP volée - Perl - C - ... Ressources http://php.net/manual/fr/migration5.cli-cgi.php
  • 6.
    PHP Notions d’environnement PHP - notions d’environnement 2. Cron Système Cron Module d’exploitation CLI - Exécution - Linux périodique - PHP - Windows - Perl - FreeBSD - C - ... - ... Sources - PHP : compilation à la volée Ressources http://doc.ubuntu-fr.org/cron
  • 7.
    PHP Notions d’environnement PHP - notions d’environnement 3. Core et modules RESEAU CRYPTAGE SGBD GRAPHISME OUTILS COMPRES- BAS SION NIVEAU soap mcrypt mysql gd calendar xmlrpc ... pdo jpeg mbstring zlib pcntl openssl postgresql png ... ... ... ftp ... pdf curl ... kerberos ... Ressources http://php.net/manual/fr/faq.installation.php
  • 8.
    PHP Notions d’environnement PHP - notions d’environnement 4. Modules Apache Apache Modules Module CLI - Linux mod_ssl - Windows mod_rewrite - PHP - FreeBSD mod_proxy - Perl - ... ... - C - ... Sources - PHP : compilation à la volée Ressources http://modules.apache.org/
  • 9.
    PHP Notions d’environnement PHP - notions d’environnement 4. Modules Apache - exemple mod_rewrite Requête GET /romans/suskind/le_parfum.html mod_rewrite RewriteRule ^/([^/]+)/([^/]+)/([^.]+).html store.php?rayon=$1&auteur=$2&titre=$3 PHP - store.php <h1>Bienvenue dans notre rayon <?php print($_REQUEST[‘rayon‘]); ?></h1> <h2>Votre livre : <?php print($_REQUEST[‘titre‘]); ?> (<?php print($_REQUEST[‘auteur‘]); ?>)</h2>
  • 10.
    PHP Notions d’environnement PHP - notions d’environnement 5. Interface OS Exécution d’un programme serveur $return=exec('ln -s /home/'); print($return); pcntl $pid = pcntl_fork(); if($pid == -1){ die('Cannot fork. Too bad, I wanna fork so bad !'); } elseif($pid){ // le père print('Je suis ton père ('.posix_getpid().'), Luke ('.$pid.') !'."rn"); } else{ // le fils print('Noooon ('.posix_getpid().') !'."rn"); } Ressources http://fr.php.net/manual/fr/book.exec.php http://fr.php.net/manual/fr/book.pcntl.php
  • 11.
    PHP Notions d’environnement PHP - notions d’environnement 6. Interface SGBD Connexion à MySQL $link=mysql_connect('localhost','login','password'); $db=mysql_select_db('database_name', $link); Couche d’abstraction - PDO $dbh = new PDO('odbc:sample', 'db2inst1', 'ibmdb2'); $count = $dbh->exec("DELETE FROM fruit WHERE couleur = 'rouge'"); print("Retourne le nombre de lignes effacées :n"); print("Effacement de $count lignes.n"); Ressources http://fr.php.net/manual/fr/book.pdo.php
  • 12.
    PHP Notions d’environnement PHP - notions d’environnement 7. php.ini et .htaccess Lecture de la configuration PHP phpinfo(); Droits limitatifs AuthType Basic AuthName "Password Required" AuthUserFile /www/passwords/password.file AuthGroupFile /www/passwords/group.file Require Group admins Ressources http://fr.php.net/manual/fr/configuration.php http://httpd.apache.org/docs/1.3/howto/htaccess.html
  • 13.
    PHP Notions d’architecture PHP - notions d’architecture 1. Repository, dev et prod dev prod - Error on - Error silent - Warning on - Warning silent - Audit on - Audit off - Debug on - Debug off Développeurs Repository - Garant de l’intégrité, de la gestion des versions, du travail collaboratif
  • 14.
    PHP Notions de génie logiciel PHP - notions de génie logiciel ETUDE DE CAS • Templating • Modèles de données SQL • Multilinguisme • Modèle objet PHP5 • Documentation • API interne / externe • Sécurité
  • 15.
    Web Services Notions d’architecture Web Services Usages • Consommation d’un service tiers • Consommation d’un service métier • Consommation d’un service critique • Consommation d’un service asynchrone • Consommation d’un service en situation de mobilité
  • 16.
    Web Services Notions d’architecture Web Services - notions d’architecture 1. XML-RPC, SOAP, Corba, IEC, WSDL, à façon... le choix de l’architecture • Type de besoin : privé / public • Besoin d’une vérification simplifiée de la cohérence du message • Poids moyen des échanges • Nature des consommateurs du service • Besoin de certification • Besoin en niveau d’abstraction : WSDL • Interopérabilité des Web Services Ressources http://fr.wikipedia.org/wiki/SOAP http://fr.wikipedia.org/wiki/XML-RPC
  • 17.
    Web Services Notions d’architecture Web Services - notions d’architecture 2. XML et JSON • Avantages de XML • Interopérabilité • Formalisme rigoureux • Parseurs multilangages • Explicitement UNICODE • Inconvénients de XML • Lourdeur des en-têtes • Rigidité du formalisme • Avantages de JSON • Interprétation simple en Javascript • Encodage / décodage intégrés à PHP 5 • Flexibilité de l’encodage • Inconvénients de XML • Parseurs généralement non-natifs • Peu robuste
  • 18.
    Web Services Notions d’architecture Web Services - notions d’architecture ETUDE DE CAS • Architecture d’un listener • Web Services asynchrones : ping et callback • Sécurité d’un Web Service
  • 19.
    Web Services Notions de génie logiciel Web Services - notions de génie logiciel ETUDE DE CAS • Moteur de session • SimpleXML • JSON • Gestion des en-têtes
  • 20.
    Web Services Architecture avancée Web Services - Architecture avancée • Montée en version d’une API externe • Gestion de la charge
  • 21.
    PHP Web Services Guillaume Jarysta-Dautel Devolia Europe +33 (0) 683 658 342 21 boulevard de Sébastopol guillaume@devolia.fr 75001 Paris - FRANCE www.devolia.fr www.jarysta.com Devolia North America 1138 Broadway, Suite 706 New York, NY 10010 – USA www.devolia.com

Notes de l'éditeur

  • #6 CGI - Ex&amp;#xE9;cution avec les droits du serveur web - Limitation des risques CLI - Ex&amp;#xE9;cution avec des droits d&amp;#xE9;finis - Acc&amp;#xE8;s &amp;#xE0; des ressources sensibles - Script shell - Lien Cron
  • #7 Cron - Ex&amp;#xE9;cution d&amp;#x2019;un script CLI &amp;#xE0; une p&amp;#xE9;riodicit&amp;#xE9; donn&amp;#xE9;e - Pas de m&amp;#xE9;canisme de s&amp;#xE9;maphore / jeton : &amp;#xE0; coder en dur - Acc&amp;#xE8;s ressources d&amp;#xE9;finies selon l&amp;#x2019;utilisateur ex&amp;#xE9;cutant la t&amp;#xE2;che
  • #8 Core : - Syntaxe de base - Basic I/O Modules : - Fonctionnalit&amp;#xE9;s additionnelles - Choisir ce dont on a besoin / taille du binaire / stabilit&amp;#xE9; du binaire / s&amp;#xE9;curit&amp;#xE9; des modules Pear : - Modules compl&amp;#xE9;mentaires http://php.net/manual/fr/faq.installation.php
  • #9 Modules Apache - Ajoute des fonctionnalit&amp;#xE9;s &amp;#xE0; la couche serveur - D&amp;#xE9;tectable par PHP (headers) mais peuvent &amp;#xEA;tre utilis&amp;#xE9;s de mani&amp;#xE8;re transparente Mod_rewrite - R&amp;#xE9;-&amp;#xE9;criture de la cha&amp;#xEE;ne &amp;#xE0; la vol&amp;#xE9;e : SEO http://modules.apache.org/
  • #10 Mod_rewrite - Application SEO - Lisibilit&amp;#xE9; et clart&amp;#xE9; de l&amp;#x2019;arborescence - Facilit&amp;#xE9; d&amp;#x2019;architecture logique
  • #11 Ex&amp;#xE9;cution - Synchrone : exec, passthru : ex&amp;#xE9;cution d&amp;#x2019;une t&amp;#xE2;che donn&amp;#xE9;e de courte dur&amp;#xE9;e - Asynchrone : pcntl : ex&amp;#xE9;cution de t&amp;#xE2;ches longues, possibilit&amp;#xE9; de forker
  • #12 BDD support&amp;#xE9;es : MySQL, Oracle, MS SQL, Tokyo Cabinet / Tokyo Tyrant
  • #13 Configuration PHP : - PHP.ini - httpd.conf - .htaccess - Fichier .php
  • #14 CGI - Ex&amp;#xE9;cution avec les droits du serveur web - Limitation des risques CLI - Ex&amp;#xE9;cution avec des droits d&amp;#xE9;finis - Acc&amp;#xE8;s &amp;#xE0; des ressources sensibles - Script shell - Lien Cron
  • #15 CGI - Ex&amp;#xE9;cution avec les droits du serveur web - Limitation des risques CLI - Ex&amp;#xE9;cution avec des droits d&amp;#xE9;finis - Acc&amp;#xE8;s &amp;#xE0; des ressources sensibles - Script shell - Lien Cron
  • #16 Configuration PHP : - PHP.ini - httpd.conf - .htaccess - Fichier .php
  • #17 Configuration PHP : - PHP.ini - httpd.conf - .htaccess - Fichier .php
  • #18 Configuration PHP : - PHP.ini - httpd.conf - .htaccess - Fichier .php
  • #19 CGI - Ex&amp;#xE9;cution avec les droits du serveur web - Limitation des risques CLI - Ex&amp;#xE9;cution avec des droits d&amp;#xE9;finis - Acc&amp;#xE8;s &amp;#xE0; des ressources sensibles - Script shell - Lien Cron
  • #20 CGI - Ex&amp;#xE9;cution avec les droits du serveur web - Limitation des risques CLI - Ex&amp;#xE9;cution avec des droits d&amp;#xE9;finis - Acc&amp;#xE8;s &amp;#xE0; des ressources sensibles - Script shell - Lien Cron
  • #21 Configuration PHP : - PHP.ini - httpd.conf - .htaccess - Fichier .php