PHP 8.5.10 Released!

Exemples de base avec curl

Une fois PHP compilé avec le support cURL, il est possible de commencer à utiliser les fonctions cURL. La première des choses à faire est d'initialiser une session cURL avec la fonction curl_init(), puis, il est possible de définir toutes les options pour le transfert avec la fonction curl_setopt(), et enfin, il est possible d'exécuter la session avec curl_exec().

Voici un exemple simple qui utilise les fonctions cURL pour envoyer une requête POST :

Exemple #1 Utilisation du module cURL de PHP pour envoyer une requête POST

<?php
$data = ['foo' => 'bar', 'baz' => 48];
$url = "http://www.example.com/handler.php";

$ch = curl_init($url);

// demande à CURL de retourner la réponse au lieu de l'envoyer sur la sortie standard
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// définit les données POST, la méthode correspondante et les en-têtes
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

// envoie la requête et récupère la réponse
$response = curl_exec($ch);

if(curl_error($ch)) {
    // traite l'erreur, ou simplement
    throw new RuntimeException(curl_error($ch));
}

Un autre exemple qui utilise les fonctions cURL pour envoyer une requête POST avec du JSON brut :

Exemple #2 Utilisation du module cURL de PHP pour envoyer une requête POST JSON

<?php
$post_data = ['foo' => 'bar', 'baz' => 48];
$url = "http://www.example.com/api/endpoint";

$ch = curl_init($url);

// demande à CURL de retourner la réponse au lieu de l'envoyer sur la sortie standard
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// définit les données POST et la méthode correspondante
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));

// définit l'en-tête requis
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);

// envoie la requête et récupère la réponse
$response = curl_exec($ch);

if(curl_error($ch)) {
    // traite l'erreur, ou simplement
    throw new RuntimeException(curl_error($ch));
}

Voici un autre exemple qui utilise les fonctions cURL pour récupérer la page d'accueil du site example.com dans un fichier :

Exemple #3 Utilisation du module cURL pour récupérer la page d'accueil d'example.com

<?php

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
if(curl_error($ch)) {
    fwrite($fp, curl_error($ch));
}
fclose($fp);

add a note

User Contributed Notes 1 note

up
62
Roberto Braga
11 years ago
It is important to notice that when using curl to post form data and you use an array for CURLOPT_POSTFIELDS option, the post will be in multipart format

<?php
$params=['name'=>'John', 'surname'=>'Doe', 'age'=>36];
$defaults = array(
CURLOPT_URL => 'http://myremoteservice/', 
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
?>
This produce the following post header:

--------------------------fd1c4191862e3566
Content-Disposition: form-data; name="name"

Jhon
--------------------------fd1c4191862e3566
Content-Disposition: form-data; name="surnname"

Doe
--------------------------fd1c4191862e3566
Content-Disposition: form-data; name="age"

36
--------------------------fd1c4191862e3566--

Setting CURLOPT_POSTFIELDS as follow produce a standard post header

CURLOPT_POSTFIELDS => http_build_query($params),

Which is:
name=John&surname=Doe&age=36

This caused me 2 days of debug while interacting with a java service which was sensible to this difference, while the equivalent one in php got both format without problem.
To Top