L3 Info : PHP et Applications Web
 
◃  Ch. 9 Extra ball !  ▹
 

La commande curl

  • curl (Client URL) est un outil en ligne de commande permettant de transférer des données vers ou depuis un serveur
  • Utilse divers protocoles (HTTP, HTTPS, FTP, etc.).
  • C’est un outil indispensable pour interagir avec des APIs, tester des endpoints, ou automatiser des requêtes.
  • Syntaxe de base : curl [options] [URL]
  • Exemple : Affichage du contenu de la ressource ciblée (méthode GET par défaut)
    curl http://127.0.0.1
  • Options usuelles :
    • --head ou -I : récupération de l'en-tête de réponse uniquement
      $ curl -I http://172.16.20.14
      HTTP/1.1 200 OK
      Date: Mon, 26 Jan 2026 16:39:32 GMT
      Server: Apache/2.4.52 (Ubuntu)
      Last-Modified: Thu, 04 May 2023 11:39:03 GMT
      ETag: "2aa6-5fadc9e8dc36c"
      Accept-Ranges: bytes
      Content-Length: 10918
      Vary: Accept-Encoding
      Content-Type: text/html
  • --data ou -d : envoi de données avec la méthode POST
    • formulaire shunté :
      <form action="coursPHP40.php" method="post" target="ongletCible">
      <p><b>Nom : </b>
      <input type="text" name="nom" size="10" /></p>
      <p><b> Prénom : </b>
      <input type="text" name="prenom" size="10" /></p>
      <p><input type="submit" name="envoi"   
       value="Clic" /></p>
      </form>
    • Commande curl envoyant les données
      $ curl --data "envoi=Clic&nom=toto&prenom=titi" http://localhost/www-lambda/PHP/coursPHP40.php
      curl -I http://172.16.20.14
      HTTP/1.1 200 OK
      Date: Mon, 26 Jan 2026 16:39:32 GMT
      Server: Apache/2.4.52 (Ubuntu)
      Last-Modified: Thu, 04 May 2023 11:39:03 GMT
      ETag: "2aa6-5fadc9e8dc36c"
      Accept-Ranges: bytes
      Content-Length: 10918
      Vary: Accept-Encoding
      Content-Type: text/html
    • Possibilité de transmettre les données en plusieurs morceaux
      $ curl --data "envoi=Clic" -d "nom=toto" --data "prenom=titi" http://localhost/www-lambda/PHP/coursPHP40.php
  • Upload de fichier
    curl -F "upload=@data.txt" http://localhost/www-lambda/PHP/coursPHP52.php
    curl -X POST -F "upload=@data.txt" http://localhost/www-lambda/PHP/coursPHP52.php # précision de la méthode
  • Autres options courantes pour les requêtes HTTP
    • -X <method> : Spécifie la méthode HTTP (GET, POST, PUT, DELETE, etc.)
    • -H : Ajoute un en-tête HTTP (en-tête sous forme de chaînes ou dans un fichier)
    • -i : Affiche les en-têtes de réponse
    • -o <fichier> : Enregistre la réponse dans un fichier