Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8313695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:34:37+00:00 2026-06-08T20:34:37+00:00

I need to send a file_get_contents() to an API endpoint with the client’s cookies

  • 0

I need to send a file_get_contents() to an API endpoint with the client’s cookies that are set by WordPress to show that the user is logged into the wordpress site. I know I need to use stream_context_create() roughly as follows:

$cookies = ??? //THIS IS THE QUESTION (see answer below)!

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: {$cookies}\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://example.dev/api/autho/', false, $context);

As you can see from the comment on the first line, I’m stuck on how to send this request so that the correct cookies are sent. I know the correct cookies are sent because I can print out $_COOKIES and see them there. But if I try to insert that same array into the headers, it doesn’t work.

Thanks in advance!

ps: I’ve read that I should use cURL for this, but I’m not sure why and I don’t know how to use it… but I’m open to the idea.

UPDATE:
I got this to work. It’s basically the same thing I was doing, with another important cookie . See my answer below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:34:39+00:00Added an answer on June 8, 2026 at 8:34 pm

    It turns out I was doing it correctly, but I didn’t know that WP needs a second cookie sent in order for the request to work properly.

    Here’s the code that works for me:

    $cookies = $_COOKIE;
    $name;
    $value;
    foreach ($_COOKIE as $key => $cookie ) {
        if ( strpos( $key, 'wordpress_logged_in') !== FALSE ) {
            $name = $key;
            $value = $cookie;
        } 
    }
    
    // Create a stream
    $opts = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n" .
                  "Cookie: {$key}={$cookie}; wordpress_test_cookie=WP Cookie check \r\n"
      )
    );
    $context = stream_context_create($opts);
    // Open the file using the HTTP headers set above
    $file = file_get_contents('http://mydomain.dev/api/autho/', false, $context);
    
    var_dump($file);
    

    It’s basically the same thing as you see in my question, but with an important addition: wordpress_test_cookie=WP Cookie check. I haven’t seen it documented anywhere, but WP needs this cookie as well as the actual wordpress_logged_in cookie in order for the call to happen as an logged in user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a custom form (created with form API) that need send an uploaded file
I need to send a file from one PHP page (on which client uploads
I need to send a file to MobileMe via Cocoa. I stumbled across a
I need to send a file to a web service (ebridge) using their SendFile
Dear friends, I need your help. I need to send .bmp file to another
I need to be able to send a file to another website from server-side
I have a file, and I need to send its contents to a function.
HI I have an xml file with 500KB size which i need to send
I am opening a file which has 100,000 URL's. I need to send an
In a powerbuilder 12.5 I need to generate a zip file to send by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.