$url = 'https://www.example.com/'; $response = file_get_contents($url); $headers = get_headers($url); $content_type = ''; foreach ($headers as $header) { if (strpos($header, 'Content-Type') !== false) { $arr = explode(':', $header); $content_type = trim($arr[1]); } } echo "Content-Type: " . $content_type;
header('Content-Type: application/json'); header('Cache-Control: max-age=3600'); $data = array('message' => 'Hello, world!'); echo json_encode($data);In this example, the headers are explicitly set using the header function, and a JSON-encoded response is sent. The package library used in this example is PHP's built-in library.