<?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));
}