DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Exploring the DBMS_DEVELOPER Package: JSON Metadata for Oracle Objects
  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Fetching Information Randomly From JSON Using Node, Nuxt, Express

Trending

  • Engineering AI Accountability: Control the Execution Path, Not the Model
  • Incident Management and the Rise of AI SRE Agents
  • The Request Timed Out, But the Payment Succeeded: Building Retry-Safe Mobile APIs
  • A Deep Dive into Tracing Agentic Workflows (Part 1)

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Using PHP Headers When Serving JSON Data

Using PHP Headers When Serving JSON Data

In this post, explore good practice to use headers to ensure your application’s output is handled correctly.

By 
Jeffrey Barke user avatar
Jeffrey Barke
·
Jun. 18, 22 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
33.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Web browsers and other similar applications rely on headers to understand the content being served to them by a web server. While modern browsers will try to “guess” what format content is in and intelligently format it, it’s still good practice to use headers to ensure your application’s output is handled correctly. When sending a JSON Payload, the header is set using the following PHP code:

 
header('Content-type: application/json');


It’s important to be aware of a few things when setting headers, however.

First, to avoid the error “Headers already sent”, the headers must be the first thing sent by the web server. It’s a good idea to put any code relating to setting headers at the top of your PHP file to avoid issues caused by your script generating other output before the headers are set.

In addition, consider how you want the browser/client to handle the data. In this sample, we don’t just inform the browser that the data is in JSON format — we also set some instructions to do with caching and expiry.

 
header('Cache-Control: no-cache, must-revalidate');


This line tells the browser not to cache the data and that it should send a revalidation request each time it wants to load the data to ensure the information it has is still fresh. These instructions are useful for data that is time-sensitive or that changes regularly.

The expires header is used to help the browser know when it needs to re-request data.

 
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');


In this example, the expiry date is set to one a long time in the past, telling the server that it should always send a revalidation request. This date and time could be changed to anything the developer wishes. For example, a JSON file detailing class timetables could be set to expire on “Friday evening.” If a user requests the timetable on a Saturday, they’ll see the information for the following week.

Once the headers have been sent, the PHP script can send the JSON payload, confident the application will process it correctly.

Common Causes of “Headers Already Sent” Errors

The most common issue when setting headers in PHP is for the headers to be sent too late in the script. If the script sends any output — even a blank line or whitespace — before the headers are sent, this will result in an error. 

It’s common to encounter this error when using a single file for HTML and PHP. Even if the headers are set at the start of the PHP code, if the file has already sent some HTML tags, this means it’s too late to set the headers.

In some cases, the source of the error is not so obvious. PHP code that uses include or require to read additional files could cause an error when setting headers if the other PHP scripts create any output.  

Always run any code to do with header instructions before any other code or markup to prevent this error.

PHP JSON

Opinions expressed by DZone contributors are their own.

Related

  • Exploring the DBMS_DEVELOPER Package: JSON Metadata for Oracle Objects
  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Fetching Information Randomly From JSON Using Node, Nuxt, Express

Partner Resources

×

Comments

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook