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.