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

  • AI Is Making PHP Cool Again
  • Dead Letter Queue Patterns in Apache Flink: Handling Poison Messages Without Stopping Your Stream
  • Alternative Structured Concurrency
  • From Compliance Pipes to Data Streams: Modernizing Healthcare EDI for Strategic Value

Trending

  • The Request Timed Out, But the Payment Succeeded: Building Retry-Safe Mobile APIs
  • What Full-Stack AI Engineering Means in Real Projects
  • The Jobs Matrix for AI: Four Boxes Instead of 40 Tools
  • The Rollback Problem: Implementing Transactional Boundaries in Agentic Loops

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. JavaScript
  4. Setting Multiple Headers in a PHP Stream Context

Setting Multiple Headers in a PHP Stream Context

By 
Lorna Mitchell user avatar
Lorna Mitchell
·
May. 08, 13 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
13.2K 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

Last week I tried to create a PHP stream context which set multiple headers; an Authorization header and a Content-Type header. All the examples I could find showed headers built up as a string with newlines added manually, which seemed pretty clunky and not-streams-like to me.

In fact, you've been able to pass this as an array since PHP 5.2.10, so to set multiple headers in the stream context, I just used this:

<?php
$options = ["http" => [
    "method" => "POST",
    "header" => ["Authorization: token " . $access_token,
        "Content-Type: application/json"],
    "content" => $data
    ]];
$context = stream_context_create($options);

The $access_token had been set elsewhere (in fact I usually put credentials in a separate file and exclude it from source control in an effort not to spread my access credentials further than I mean to!), and $data is already encoded as JSON. For completeness, you can make the POST request like this:

<?php
// make the request
$response = file_get_contents($url, false, $context);

Hopefully this will help someone else doing the same thing next time (or at least I know I can come back here when I can't remember!), the array approach seems more elegant and maintainable to me.




PHP Stream (computing)

Published at DZone with permission of Lorna Mitchell. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • AI Is Making PHP Cool Again
  • Dead Letter Queue Patterns in Apache Flink: Handling Poison Messages Without Stopping Your Stream
  • Alternative Structured Concurrency
  • From Compliance Pipes to Data Streams: Modernizing Healthcare EDI for Strategic Value

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