<?php
$post_data = ['foo' => 'bar', 'baz' => 48];
$url = "http://www.example.com/api/endpoint";
$ch = curl_init($url);
// tell CURL to return the response instead of sending it to stdout
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// set the POST data and corresponding method
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
// set the required header
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
// send the request and get the response
$response = curl_exec($ch);
if(curl_error($ch)) {
// handle the error, or just
throw new RuntimeException(curl_error($ch));
}