Why is it important to specify the Content-Type as "application/json" when sending JSON data from a PHP script?

Specifying the Content-Type as "application/json" when sending JSON data from a PHP script is important because it informs the receiving end (such as a client-side application or API) about the type of data being sent. This helps the receiver parse the incoming data correctly and handle it appropriately. Without specifying the Content-Type, the receiver may not be able to properly interpret the JSON data, leading to errors or unexpected behavior.

<?php
header('Content-Type: application/json');
// Your JSON data here
$data = array('key1' => 'value1', 'key2' => 'value2');
echo json_encode($data);
?>