SlideShare une entreprise Scribd logo
1  sur  37
Communications réseaux
      HTTP avec PHP
Bonjour à tous
●   Julien PAULI
    –   Utilisateur PHP depuis ~10ans
    –   Linux / Unix adopter
    –   PHP Internals reviewer depuis ~3y
        ●   I love C coding
    –   PHP 5.5 Release Manager

    –   Travaille pour blablacar Paris
    –   Ecrit des articles tech PHP et des livres
    –   @julienpauli - github.com/jpauli - jpauli@php.net
BlaBlacar
●   http://www.blablacar.com
●   http://www.covoiturage.fr
    –   2.5 Millions de membres
    –   400.000 personnes font du covoiturage
        chaque mois en France
    –   100% croissance depuis 4 ans
        ●   Nous recrutons ! Passez me voir :)
    –   European open : UK, Netherlands, Portugal,
        Poland, Italy, Spain
    –   Apps mobiles Android & Iphone
BlaBlaCar tech
●   PHP 5.3, MySQL 5.5 (percona),
    Lighttpd+FastCGI
●   Debian
●   8 front web servers (4 virtual)
●   1 MySQL master, 2 slaves
●   200Gb MySQL data

●   Redis, ElasticSearch, Symfony2,
    RabbitMQ, OpscodeChef ...
Go




Let's Go
Au menu aujourd'hui
●   Rappels généraux sur le réseau
    –   Protocoles et encapsulation

●   Communiquer sur le réseau avec PHP
    –   Différentes API offertes

●   HTTP avec PHP
    –   Différentes API offertes
Rappels sur le réseau
●   Web = modèle TCP/IP (4 couches)
●   PHP permet de jouer avec les couches 3
    et 4
Analyser le trafic réseau

●   TcpDump ou graphiquement : Wireshark

●   Mise en pratique de l'encapsulation
    protocolaire

●   Superbe outils de piratage débogage de
    réseau
Le réseau sous Unix / Linux
●   Au moins sur un point, tout le monde Unix
    s'est mis d'accord
    –   Grâce à BSD :)

●   ! SOCKETS !
    –   POSIX
Le réseau sous Unix / Linux
PHP et le réseau
●   PHP utilise l'API sockets de l'OS
    –   Via une couche intermédiaire (streams)
    –   main/network.c et main/streams


●   PHP fournit plusieurs API réseau à l'utilisateur
    –   Client ou serveur
    –   Une des API est calquée sur POSIX Sockets
Communiquer avec PHP


  Fonctionnalité     Couche réseau   Client   Serveur   Disponibilité

   fsockopen()         Transport      Oui      Non         Native
stream_socket_**()     Transport      Oui      Oui         Native
   socket_**()         Transport      Oui      Oui      ext/sockets
     curl_**()           App          Oui      Non        ext/curl
 fopen() & friends       App          Oui      Non         Native
La base du réseau en PHP
●   Sans s'embêter, on utilise les flux (streams)
fsockopen()



 fsockopen()
fsockopen() : transport
●   fsockopen() joue en couche transport
     –   Vous devez donc écrire le protocole applicatif
     –   Ici : HTTP
    $fp = fsockopen(‘www.php.net’, 80);
    fwrite($fp, “GET / HTTP/1.0rn”
          . “Host: www.php.netrnrn”);
    $data = fread($fp, 8192);
●   Liste des transports disponibles :
    stream_get_transports()
     –   souvent, TCP est utilisé
Couche Transport
●   TCP par défaut
     –   Equivalents :
    $fp = fsockopen(‘www.php.net’, 80);
    $fp = fsockopen(‘tcp://www.php.net’, 80);
●   Faux (protocole applicatif spécifié):
    $fp = fsockopen(‘http://www.php.net’, 80);
●   UDP someone ?
    $fp = fsockopen(‘udp://10.11.10.11:112');

●   Unix socket someone ?
    $fp = fsockopen(‘unix:///var/run/mysqld/mysqld.sock');
fsockopen() , quelques limites
●   fsockopen() a quelques limites
    –   Client seulement
    –   Peu d'options (pas de stream context)

●   Please, welcome stream_socket_**()
    –   stream_socket_client()
    –   stream_socket_server()
stream_socket_**()
●    API cliente et serveur
●    Couche transport toujours

●    Client
    $fp = stream_socket_client(‘www.php.net:80');
    fwrite($fp, "GET / HTTP/1.0rnHost: www.php.netrnrn");
    $data = fread($fp, 8192);

●    Serveur
    $fp = stream_socket_server(‘tcp://0.0.0.0:1234');
    /* un doux combiné socket()-bind()-listen() */
stream_socket_***() : options
●   Quelques options interessantes :
    –   stream_set_blocking()
        ●   Règle l'API bloquante / non-bloquante
    –   stream_notification_callback()
        ●   Appel une callback lorsque {xxxx} sur le flux
    –   stream_set_write/read_buffer()
        ●   Taille des buffers
    –   stream_copy_to_stream()
        ●   Genre de pipe
    –   stream_select()
        ●   syscall select() / poll()
BSD Sockets



  ext/sockets
BSD sockets
●   Extension à installer : sockets
    –   --enable-sockets
●   API équivalente à BSD sockets
    –   http://www.freebsd.org/doc/en/books/develope
        rs-handbook/sockets.html
ext/sockets
$address = '192.168.1.53';
$port = 10000;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error());
}
if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock));
}
if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock));
}
do {
    if (($msgsock = socket_accept($sock)) === false) {
     echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock))"n";
     break;
    }
    /* Send instructions. */
    $msg = "nWelcome to the PHP Test Server. n" .
        "To quit, type 'quit'. To shut down the server type 'shutdown'.n";
    socket_write($msgsock, $msg, strlen($msg));
Couche applicative
●   Couche au dessus du transport
●   Protocole applicatif déjà implémenté
●   Exemples :
    –   Réseaux
        ●   http
        ●   ftp
        ●   ssh
    –   Locaux
        ●   php
        ●   data
        ●   zip
Stream Wrapper
●   Pour passer de la couche transport à la
    couche application, PHP utilise un stream
    wrapper
    $fp Transport :
     – = fsockopen(‘www.php.net’, 80);
    fwrite($fp, “GET / HTTP/1.0rn”
      . “Host: www.php.netrnrn”);
    $data = fread($fp, 8192);


    $fp Application (wrapper http://) :
     – = fopen(‘http://www.php.net’, ‘r’);
    $data = fread($fp, 8192);
fopen() and friends
●   fopen() et ses amies agissent en couche
    applicative
●   Elles utilisent le wrapper que vous
    précisez :
     $fp = fopen(‘http://www.php.net’, ‘r’);
     $data = fread($fp, 8192);

    $data = file_get_contents(‘ftp://foo:bar@myhost.net/file.txt’);

     readfile(‘phar://my-php.phar/lib/myclass.php’);

     echo "foo" | php -r readfile(‘php://fd/0’);
Créer sa propre couche
●    Vous pouvez créer votre propre couche
     applicative et utiliser votre propre wrapper
    stream_wrapper_register('mysql', MySQLWrapperClass);

    $fp = fopen('mysql://admin:secret@server/mydb/mytable');

    while (!feof($fp)) {
      echo fread($fp);
    }
●    Exemples :
      –   http://www.hoa-project.net
      –   https://github.com/mikey179/vfsStream
Agir sur une couche
    ●   PHP propose de modifier à la volée ses
        wrappers intégrés
        –   Exemple : proxy get to post

$context = stream_context_create(
   array('http' =>array('method'=>'POST',
                         'header'=>'Content-Type: application/x-www-form-urlencoded',
                         'content'=>http_build_query($_GET)))
                                     );
echo file_get_contents("http://inject-post-data-server",null,$context);
HTTP



HTTP with PHP
PHP et HTTP
●   Plusieurs moyens de parler HTTP

    –   Utiliser le wrapper PHP intégré (http://)

    –   Ecrire soi-même la couche over transport

    –   Utiliser Curl

    –   Utiliser ext/HTTP
Wrapper intégré
    ●   Tout ce qui est http:// sur les fonctions PHP
$context = stream_context_create(
   array('http' =>array('method'=>'POST',
                         'header'=>'Content-Type: application/x-www-form-urlencoded',
                         'content'=>http_build_query($_GET)))
                                     );
echo file_get_contents("http://inject-post-data-server",null,$context);


    ●   Quelques fonctions PHP
        –   http_build_query()
        –   header() - get_headers() - get_meta_tags()
    ●   Suffisant, mais pas très poussé
Ecrire son wrapper
●   Reinvent the wheel ?
           $fp = fsockopen('myserver.net', 80);

           $http = <<<HTTP
           GET /foo HTTP/1.1rn
           Host: myserver.netrn
           Accept: text/htmlrn
           HTTP;

           fwrite($fp, $http);
           echo fgets($fp);
Curl
●   http://curl.haxx.se/
●   Pour PHP : --with-curl ou extension PECL
curl is a command line tool for transferring data with URL syntax, supporting
DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS,
POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP.
curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading,
HTTP form based upload, proxies, cookies, user+password authentication
(Basic, Digest, NTLM, Negotiate, kerberos...), file transfer resume,
proxy tunneling and a busload of other useful tricks.

●   On fait des choses très puissantes avec
    Curl
    –   Pas seulement sur HTTP
Curl example
$soap_request = "<?xml version="1.0"?>n";
$soap_request .= "<soap:Envelope xmlns:soap=" blablabla"
$header = array(
  "Content-type: text/xml;charset="utf-8"",
  "Accept: text/xml",
  "Cache-Control: no-cache",
  "SOAPAction: "foo"",
  "Content-length: ".strlen($soap_request),);
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, "http://myserver/soap-server" );
curl_setopt($soap_do, CURLOPT_TIMEOUT,             10);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_POST,             true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
if(curl_exec($soap_do) === false) {
  $err = 'Curl error: ' . curl_error($soap_do);
  echo $err;
} else { echo "yeah"; }
curl_close($soap_do);
ext/HTTP
●   Sans doute le moyen le plus puissant et le
    plus efficace de parler HTTP avec PHP

●   Extension
    –   pecl install pecl_http
    –   Ou compiler dans la source

●   Interface riche pour piloter HTTP, orientée
    objet
ext/HTTP API
●   Classes :
    –   HTTPRequest
    –   HTTPResponse
    –   HTTPQueryStream
    –   HTTPMessage
●   Gestion de la compression gzip
●   Gestion du cache
●   Fonctions de liaison avec le buffer de
    sortie PHP
ext/HTTP examples
    $http_req = new HttpRequest("http://www.example.com");
    $http_req->setOptions(array(timeout=>10,useragent=>"MyScript"));
    $http_req->send();
    echo $http_req->getResponseBody();


    $http_req = new HttpRequest("http://www.example.com");
    $http_req->setOptions(array(timeout=>10,useragent=>"MyScript"))
    try {
       $http_req->send();
    } catch (HttpException $ex) {
       echo $ex->getMessage();
    }

●   Quelques fonctions auto-parlantes au hasard :
    –   http_redirect(), http_send_data(), http_send_file(),
        http_send_stream(), http_put_data(),
        http_parse_cookie()...
Thank you for listening !

Contenu connexe

Tendances

Formation PHP
Formation PHPFormation PHP
Formation PHPkemenaran
 
Presentation langage go_19022015
Presentation langage go_19022015Presentation langage go_19022015
Presentation langage go_19022015Stéphane Legrand
 
Introduction au langage PHP (2éme partie) élaborée par Marouan OMEZZINE
Introduction au langage PHP (2éme partie) élaborée par Marouan OMEZZINEIntroduction au langage PHP (2éme partie) élaborée par Marouan OMEZZINE
Introduction au langage PHP (2éme partie) élaborée par Marouan OMEZZINEMarouan OMEZZINE
 
PHP 7.0 : aperçu des nouveautés
PHP 7.0 : aperçu des nouveautésPHP 7.0 : aperçu des nouveautés
PHP 7.0 : aperçu des nouveautésDidcode
 
Pratique de la programmation en go
Pratique de la programmation en goPratique de la programmation en go
Pratique de la programmation en gokader15
 
PHP 5.3, PHP Next
PHP 5.3, PHP NextPHP 5.3, PHP Next
PHP 5.3, PHP NextSQLI
 
PHP 1 - Apprendre les bases
PHP 1 - Apprendre les basesPHP 1 - Apprendre les bases
PHP 1 - Apprendre les basesPierre Faure
 
.php1 : les fondamentaux du PHP
.php1 : les fondamentaux du PHP.php1 : les fondamentaux du PHP
.php1 : les fondamentaux du PHPAbdoulaye Dieng
 
Migration PHP4-PHP5
Migration PHP4-PHP5Migration PHP4-PHP5
Migration PHP4-PHP5julien pauli
 
Bases de PHP - Partie 1
Bases de PHP - Partie 1Bases de PHP - Partie 1
Bases de PHP - Partie 1Régis Lutter
 
Symfony2 - Un Framework PHP 5 Performant
Symfony2 - Un Framework PHP 5 PerformantSymfony2 - Un Framework PHP 5 Performant
Symfony2 - Un Framework PHP 5 PerformantHugo Hamon
 
Monitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et PinbaMonitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et PinbaPatrick Allaert
 
Nouveautés PHP 7 : Introduction et performances - MeetUP Openska
Nouveautés PHP 7 : Introduction et performances - MeetUP OpenskaNouveautés PHP 7 : Introduction et performances - MeetUP Openska
Nouveautés PHP 7 : Introduction et performances - MeetUP OpenskaOpenska
 

Tendances (19)

Formation PHP
Formation PHPFormation PHP
Formation PHP
 
Presentation langage go_19022015
Presentation langage go_19022015Presentation langage go_19022015
Presentation langage go_19022015
 
PHP Training
PHP TrainingPHP Training
PHP Training
 
Introduction au langage PHP (2éme partie) élaborée par Marouan OMEZZINE
Introduction au langage PHP (2éme partie) élaborée par Marouan OMEZZINEIntroduction au langage PHP (2éme partie) élaborée par Marouan OMEZZINE
Introduction au langage PHP (2éme partie) élaborée par Marouan OMEZZINE
 
Cours php
Cours phpCours php
Cours php
 
PHP 7.0 : aperçu des nouveautés
PHP 7.0 : aperçu des nouveautésPHP 7.0 : aperçu des nouveautés
PHP 7.0 : aperçu des nouveautés
 
Pratique de la programmation en go
Pratique de la programmation en goPratique de la programmation en go
Pratique de la programmation en go
 
Formation python 3
Formation python 3Formation python 3
Formation python 3
 
PHP 5.3, PHP Next
PHP 5.3, PHP NextPHP 5.3, PHP Next
PHP 5.3, PHP Next
 
PHP 1 - Apprendre les bases
PHP 1 - Apprendre les basesPHP 1 - Apprendre les bases
PHP 1 - Apprendre les bases
 
Php cours
Php coursPhp cours
Php cours
 
Playing With PHP 5.3
Playing With PHP 5.3Playing With PHP 5.3
Playing With PHP 5.3
 
.php1 : les fondamentaux du PHP
.php1 : les fondamentaux du PHP.php1 : les fondamentaux du PHP
.php1 : les fondamentaux du PHP
 
Migration PHP4-PHP5
Migration PHP4-PHP5Migration PHP4-PHP5
Migration PHP4-PHP5
 
Bases de PHP - Partie 1
Bases de PHP - Partie 1Bases de PHP - Partie 1
Bases de PHP - Partie 1
 
Php seance1
Php seance1Php seance1
Php seance1
 
Symfony2 - Un Framework PHP 5 Performant
Symfony2 - Un Framework PHP 5 PerformantSymfony2 - Un Framework PHP 5 Performant
Symfony2 - Un Framework PHP 5 Performant
 
Monitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et PinbaMonitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et Pinba
 
Nouveautés PHP 7 : Introduction et performances - MeetUP Openska
Nouveautés PHP 7 : Introduction et performances - MeetUP OpenskaNouveautés PHP 7 : Introduction et performances - MeetUP Openska
Nouveautés PHP 7 : Introduction et performances - MeetUP Openska
 

Similaire à Communications Réseaux et HTTP avec PHP

Rich Desktop Applications
Rich Desktop ApplicationsRich Desktop Applications
Rich Desktop Applicationsgoldoraf
 
Cours php -partie 1.pdf
Cours php -partie 1.pdfCours php -partie 1.pdf
Cours php -partie 1.pdfssuserc46a93
 
Les Web Services en 60 diapos chrono !
Les Web Services en 60 diapos chrono !Les Web Services en 60 diapos chrono !
Les Web Services en 60 diapos chrono !Olivier Le Goaër
 
php2 : formulaire-session-PDO
php2 : formulaire-session-PDOphp2 : formulaire-session-PDO
php2 : formulaire-session-PDOAbdoulaye Dieng
 
S2-01-PHP.pptx
S2-01-PHP.pptxS2-01-PHP.pptx
S2-01-PHP.pptxkohay75604
 
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdfCours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdfRihabBENLAMINE
 
Apache server configuration & sécurisation -
Apache server configuration & sécurisation  -Apache server configuration & sécurisation  -
Apache server configuration & sécurisation -achraf_ing
 
ServeurWeb-ProtocoleHttpdefinitionet.pdf
ServeurWeb-ProtocoleHttpdefinitionet.pdfServeurWeb-ProtocoleHttpdefinitionet.pdf
ServeurWeb-ProtocoleHttpdefinitionet.pdfnebibieg
 
Presentation
PresentationPresentation
Presentationbois
 
hassclic270.ppt
hassclic270.ppthassclic270.ppt
hassclic270.pptadiouf2
 
Mieux Développer en PHP avec Symfony
Mieux Développer en PHP avec SymfonyMieux Développer en PHP avec Symfony
Mieux Développer en PHP avec SymfonyHugo Hamon
 
Developpement web dynamique_Base de donnees.pdf
Developpement web dynamique_Base de donnees.pdfDeveloppement web dynamique_Base de donnees.pdf
Developpement web dynamique_Base de donnees.pdfrachidimstapha
 
PHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_ExtensionsPHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_Extensionsjulien pauli
 
08 01 mise en place d'un serveur web
08 01 mise en place d'un serveur web08 01 mise en place d'un serveur web
08 01 mise en place d'un serveur webNoël
 
cours-gratuit.com--coursMySql-id2218.pdf
cours-gratuit.com--coursMySql-id2218.pdfcours-gratuit.com--coursMySql-id2218.pdf
cours-gratuit.com--coursMySql-id2218.pdfGroupeExcelMarrakech
 

Similaire à Communications Réseaux et HTTP avec PHP (20)

Rich Desktop Applications
Rich Desktop ApplicationsRich Desktop Applications
Rich Desktop Applications
 
Lp web tp3_idse
Lp web tp3_idseLp web tp3_idse
Lp web tp3_idse
 
Cours php -partie 1.pdf
Cours php -partie 1.pdfCours php -partie 1.pdf
Cours php -partie 1.pdf
 
Apache Open SSL
Apache Open SSLApache Open SSL
Apache Open SSL
 
Les Web Services en 60 diapos chrono !
Les Web Services en 60 diapos chrono !Les Web Services en 60 diapos chrono !
Les Web Services en 60 diapos chrono !
 
php2 : formulaire-session-PDO
php2 : formulaire-session-PDOphp2 : formulaire-session-PDO
php2 : formulaire-session-PDO
 
S2-01-PHP.pptx
S2-01-PHP.pptxS2-01-PHP.pptx
S2-01-PHP.pptx
 
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdfCours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
 
Apache server configuration & sécurisation -
Apache server configuration & sécurisation  -Apache server configuration & sécurisation  -
Apache server configuration & sécurisation -
 
ServeurWeb-ProtocoleHttpdefinitionet.pdf
ServeurWeb-ProtocoleHttpdefinitionet.pdfServeurWeb-ProtocoleHttpdefinitionet.pdf
ServeurWeb-ProtocoleHttpdefinitionet.pdf
 
Presentation
PresentationPresentation
Presentation
 
hassclic270.ppt
hassclic270.ppthassclic270.ppt
hassclic270.ppt
 
Cours 8 squid.pdf
Cours 8 squid.pdfCours 8 squid.pdf
Cours 8 squid.pdf
 
Mieux Développer en PHP avec Symfony
Mieux Développer en PHP avec SymfonyMieux Développer en PHP avec Symfony
Mieux Développer en PHP avec Symfony
 
Developpement web dynamique_Base de donnees.pdf
Developpement web dynamique_Base de donnees.pdfDeveloppement web dynamique_Base de donnees.pdf
Developpement web dynamique_Base de donnees.pdf
 
PHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_ExtensionsPHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_Extensions
 
08 01 mise en place d'un serveur web
08 01 mise en place d'un serveur web08 01 mise en place d'un serveur web
08 01 mise en place d'un serveur web
 
12-Factor
12-Factor12-Factor
12-Factor
 
PHP_intro.pdf
PHP_intro.pdfPHP_intro.pdf
PHP_intro.pdf
 
cours-gratuit.com--coursMySql-id2218.pdf
cours-gratuit.com--coursMySql-id2218.pdfcours-gratuit.com--coursMySql-id2218.pdf
cours-gratuit.com--coursMySql-id2218.pdf
 

Plus de julien pauli

Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019julien pauli
 
PHP 7 OPCache extension review
PHP 7 OPCache extension reviewPHP 7 OPCache extension review
PHP 7 OPCache extension reviewjulien pauli
 
PHP Internals and Virtual Machine
PHP Internals and Virtual MachinePHP Internals and Virtual Machine
PHP Internals and Virtual Machinejulien pauli
 
Basics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNGBasics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNGjulien pauli
 
Mastering your home network - Do It Yourself
Mastering your home network - Do It YourselfMastering your home network - Do It Yourself
Mastering your home network - Do It Yourselfjulien pauli
 
SymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performancesSymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performancesjulien pauli
 
Php and threads ZTS
Php and threads ZTSPhp and threads ZTS
Php and threads ZTSjulien pauli
 
Symfony live 2017_php7_performances
Symfony live 2017_php7_performancesSymfony live 2017_php7_performances
Symfony live 2017_php7_performancesjulien pauli
 
Php7 extensions workshop
Php7 extensions workshopPhp7 extensions workshop
Php7 extensions workshopjulien pauli
 
Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7julien pauli
 
PHP 7 performances from PHP 5
PHP 7 performances from PHP 5PHP 7 performances from PHP 5
PHP 7 performances from PHP 5julien pauli
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionjulien pauli
 
Php extensions workshop
Php extensions workshopPhp extensions workshop
Php extensions workshopjulien pauli
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objectsjulien pauli
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13julien pauli
 

Plus de julien pauli (20)

Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019
 
Php engine
Php enginePhp engine
Php engine
 
PHP 7 OPCache extension review
PHP 7 OPCache extension reviewPHP 7 OPCache extension review
PHP 7 OPCache extension review
 
Dns
DnsDns
Dns
 
PHP Internals and Virtual Machine
PHP Internals and Virtual MachinePHP Internals and Virtual Machine
PHP Internals and Virtual Machine
 
Basics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNGBasics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNG
 
Mastering your home network - Do It Yourself
Mastering your home network - Do It YourselfMastering your home network - Do It Yourself
Mastering your home network - Do It Yourself
 
SymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performancesSymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performances
 
Php and threads ZTS
Php and threads ZTSPhp and threads ZTS
Php and threads ZTS
 
Tcpip
TcpipTcpip
Tcpip
 
Symfony live 2017_php7_performances
Symfony live 2017_php7_performancesSymfony live 2017_php7_performances
Symfony live 2017_php7_performances
 
PHP 7 new engine
PHP 7 new enginePHP 7 new engine
PHP 7 new engine
 
Php7 extensions workshop
Php7 extensions workshopPhp7 extensions workshop
Php7 extensions workshop
 
Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7
 
PHP 7 performances from PHP 5
PHP 7 performances from PHP 5PHP 7 performances from PHP 5
PHP 7 performances from PHP 5
 
PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extension
 
Php extensions workshop
Php extensions workshopPhp extensions workshop
Php extensions workshop
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
 

Communications Réseaux et HTTP avec PHP

  • 1. Communications réseaux HTTP avec PHP
  • 2. Bonjour à tous ● Julien PAULI – Utilisateur PHP depuis ~10ans – Linux / Unix adopter – PHP Internals reviewer depuis ~3y ● I love C coding – PHP 5.5 Release Manager – Travaille pour blablacar Paris – Ecrit des articles tech PHP et des livres – @julienpauli - github.com/jpauli - jpauli@php.net
  • 3. BlaBlacar ● http://www.blablacar.com ● http://www.covoiturage.fr – 2.5 Millions de membres – 400.000 personnes font du covoiturage chaque mois en France – 100% croissance depuis 4 ans ● Nous recrutons ! Passez me voir :) – European open : UK, Netherlands, Portugal, Poland, Italy, Spain – Apps mobiles Android & Iphone
  • 4. BlaBlaCar tech ● PHP 5.3, MySQL 5.5 (percona), Lighttpd+FastCGI ● Debian ● 8 front web servers (4 virtual) ● 1 MySQL master, 2 slaves ● 200Gb MySQL data ● Redis, ElasticSearch, Symfony2, RabbitMQ, OpscodeChef ...
  • 6. Au menu aujourd'hui ● Rappels généraux sur le réseau – Protocoles et encapsulation ● Communiquer sur le réseau avec PHP – Différentes API offertes ● HTTP avec PHP – Différentes API offertes
  • 7. Rappels sur le réseau ● Web = modèle TCP/IP (4 couches) ● PHP permet de jouer avec les couches 3 et 4
  • 8. Analyser le trafic réseau ● TcpDump ou graphiquement : Wireshark ● Mise en pratique de l'encapsulation protocolaire ● Superbe outils de piratage débogage de réseau
  • 9. Le réseau sous Unix / Linux ● Au moins sur un point, tout le monde Unix s'est mis d'accord – Grâce à BSD :) ● ! SOCKETS ! – POSIX
  • 10. Le réseau sous Unix / Linux
  • 11. PHP et le réseau ● PHP utilise l'API sockets de l'OS – Via une couche intermédiaire (streams) – main/network.c et main/streams ● PHP fournit plusieurs API réseau à l'utilisateur – Client ou serveur – Une des API est calquée sur POSIX Sockets
  • 12. Communiquer avec PHP Fonctionnalité Couche réseau Client Serveur Disponibilité fsockopen() Transport Oui Non Native stream_socket_**() Transport Oui Oui Native socket_**() Transport Oui Oui ext/sockets curl_**() App Oui Non ext/curl fopen() & friends App Oui Non Native
  • 13. La base du réseau en PHP ● Sans s'embêter, on utilise les flux (streams)
  • 15. fsockopen() : transport ● fsockopen() joue en couche transport – Vous devez donc écrire le protocole applicatif – Ici : HTTP $fp = fsockopen(‘www.php.net’, 80); fwrite($fp, “GET / HTTP/1.0rn” . “Host: www.php.netrnrn”); $data = fread($fp, 8192); ● Liste des transports disponibles : stream_get_transports() – souvent, TCP est utilisé
  • 16. Couche Transport ● TCP par défaut – Equivalents : $fp = fsockopen(‘www.php.net’, 80); $fp = fsockopen(‘tcp://www.php.net’, 80); ● Faux (protocole applicatif spécifié): $fp = fsockopen(‘http://www.php.net’, 80); ● UDP someone ? $fp = fsockopen(‘udp://10.11.10.11:112'); ● Unix socket someone ? $fp = fsockopen(‘unix:///var/run/mysqld/mysqld.sock');
  • 17. fsockopen() , quelques limites ● fsockopen() a quelques limites – Client seulement – Peu d'options (pas de stream context) ● Please, welcome stream_socket_**() – stream_socket_client() – stream_socket_server()
  • 18. stream_socket_**() ● API cliente et serveur ● Couche transport toujours ● Client $fp = stream_socket_client(‘www.php.net:80'); fwrite($fp, "GET / HTTP/1.0rnHost: www.php.netrnrn"); $data = fread($fp, 8192); ● Serveur $fp = stream_socket_server(‘tcp://0.0.0.0:1234'); /* un doux combiné socket()-bind()-listen() */
  • 19. stream_socket_***() : options ● Quelques options interessantes : – stream_set_blocking() ● Règle l'API bloquante / non-bloquante – stream_notification_callback() ● Appel une callback lorsque {xxxx} sur le flux – stream_set_write/read_buffer() ● Taille des buffers – stream_copy_to_stream() ● Genre de pipe – stream_select() ● syscall select() / poll()
  • 20. BSD Sockets ext/sockets
  • 21. BSD sockets ● Extension à installer : sockets – --enable-sockets ● API équivalente à BSD sockets – http://www.freebsd.org/doc/en/books/develope rs-handbook/sockets.html
  • 22. ext/sockets $address = '192.168.1.53'; $port = 10000; if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()); } if (socket_bind($sock, $address, $port) === false) { echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)); } if (socket_listen($sock, 5) === false) { echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)); } do { if (($msgsock = socket_accept($sock)) === false) { echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock))"n"; break; } /* Send instructions. */ $msg = "nWelcome to the PHP Test Server. n" . "To quit, type 'quit'. To shut down the server type 'shutdown'.n"; socket_write($msgsock, $msg, strlen($msg));
  • 23. Couche applicative ● Couche au dessus du transport ● Protocole applicatif déjà implémenté ● Exemples : – Réseaux ● http ● ftp ● ssh – Locaux ● php ● data ● zip
  • 24. Stream Wrapper ● Pour passer de la couche transport à la couche application, PHP utilise un stream wrapper $fp Transport : – = fsockopen(‘www.php.net’, 80); fwrite($fp, “GET / HTTP/1.0rn” . “Host: www.php.netrnrn”); $data = fread($fp, 8192); $fp Application (wrapper http://) : – = fopen(‘http://www.php.net’, ‘r’); $data = fread($fp, 8192);
  • 25. fopen() and friends ● fopen() et ses amies agissent en couche applicative ● Elles utilisent le wrapper que vous précisez : $fp = fopen(‘http://www.php.net’, ‘r’); $data = fread($fp, 8192); $data = file_get_contents(‘ftp://foo:bar@myhost.net/file.txt’); readfile(‘phar://my-php.phar/lib/myclass.php’); echo "foo" | php -r readfile(‘php://fd/0’);
  • 26. Créer sa propre couche ● Vous pouvez créer votre propre couche applicative et utiliser votre propre wrapper stream_wrapper_register('mysql', MySQLWrapperClass); $fp = fopen('mysql://admin:secret@server/mydb/mytable'); while (!feof($fp)) { echo fread($fp); } ● Exemples : – http://www.hoa-project.net – https://github.com/mikey179/vfsStream
  • 27. Agir sur une couche ● PHP propose de modifier à la volée ses wrappers intégrés – Exemple : proxy get to post $context = stream_context_create( array('http' =>array('method'=>'POST', 'header'=>'Content-Type: application/x-www-form-urlencoded', 'content'=>http_build_query($_GET))) ); echo file_get_contents("http://inject-post-data-server",null,$context);
  • 29. PHP et HTTP ● Plusieurs moyens de parler HTTP – Utiliser le wrapper PHP intégré (http://) – Ecrire soi-même la couche over transport – Utiliser Curl – Utiliser ext/HTTP
  • 30. Wrapper intégré ● Tout ce qui est http:// sur les fonctions PHP $context = stream_context_create( array('http' =>array('method'=>'POST', 'header'=>'Content-Type: application/x-www-form-urlencoded', 'content'=>http_build_query($_GET))) ); echo file_get_contents("http://inject-post-data-server",null,$context); ● Quelques fonctions PHP – http_build_query() – header() - get_headers() - get_meta_tags() ● Suffisant, mais pas très poussé
  • 31. Ecrire son wrapper ● Reinvent the wheel ? $fp = fsockopen('myserver.net', 80); $http = <<<HTTP GET /foo HTTP/1.1rn Host: myserver.netrn Accept: text/htmlrn HTTP; fwrite($fp, $http); echo fgets($fp);
  • 32. Curl ● http://curl.haxx.se/ ● Pour PHP : --with-curl ou extension PECL curl is a command line tool for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos...), file transfer resume, proxy tunneling and a busload of other useful tricks. ● On fait des choses très puissantes avec Curl – Pas seulement sur HTTP
  • 33. Curl example $soap_request = "<?xml version="1.0"?>n"; $soap_request .= "<soap:Envelope xmlns:soap=" blablabla" $header = array( "Content-type: text/xml;charset="utf-8"", "Accept: text/xml", "Cache-Control: no-cache", "SOAPAction: "foo"", "Content-length: ".strlen($soap_request),); $soap_do = curl_init(); curl_setopt($soap_do, CURLOPT_URL, "http://myserver/soap-server" ); curl_setopt($soap_do, CURLOPT_TIMEOUT, 10); curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true ); curl_setopt($soap_do, CURLOPT_POST, true ); curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request); curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header); if(curl_exec($soap_do) === false) { $err = 'Curl error: ' . curl_error($soap_do); echo $err; } else { echo "yeah"; } curl_close($soap_do);
  • 34. ext/HTTP ● Sans doute le moyen le plus puissant et le plus efficace de parler HTTP avec PHP ● Extension – pecl install pecl_http – Ou compiler dans la source ● Interface riche pour piloter HTTP, orientée objet
  • 35. ext/HTTP API ● Classes : – HTTPRequest – HTTPResponse – HTTPQueryStream – HTTPMessage ● Gestion de la compression gzip ● Gestion du cache ● Fonctions de liaison avec le buffer de sortie PHP
  • 36. ext/HTTP examples $http_req = new HttpRequest("http://www.example.com"); $http_req->setOptions(array(timeout=>10,useragent=>"MyScript")); $http_req->send(); echo $http_req->getResponseBody(); $http_req = new HttpRequest("http://www.example.com"); $http_req->setOptions(array(timeout=>10,useragent=>"MyScript")) try { $http_req->send(); } catch (HttpException $ex) { echo $ex->getMessage(); } ● Quelques fonctions auto-parlantes au hasard : – http_redirect(), http_send_data(), http_send_file(), http_send_stream(), http_put_data(), http_parse_cookie()...
  • 37. Thank you for listening !