[PHP] Send a POST request with a JSON body

Created on November 28, 2026

To send a POST request with a JSON body, pass an options array to stream_context_create. Set header to Content-type: application/json and content to the JSON-encoded string.

<?php

$end_point = 'https://documentroot.org';

$parameters = [
  'name' => 'Taro',
];

$options = [
  'http' => [
    'header'  => 'Content-type: application/json',
    'method'  => 'POST',
    'content' => json_encode($parameters),
  ],
];
$context  = stream_context_create($options);
$response = @file_get_contents($end_point, false, $context);
PHP