1

I'm working with an api that returns a number of useful variables within the header response, for example with...

Pragma → no-cache
Server → Apache
X-Pages → 1424

... I'd like to use the X-Pages variable to form some further logic. How do I retrieve this variable and use it within my php script?

My api currently call looks like this:

$username = "XXXXXXXX";
$password = "XXXXXXXX";
$remote_url = 'XXXXXXXX';

$headers = array();
$headers[] = "Authorization: Basic " . base64_encode("$username:$password");

$opts = array(
  'http'=>array(
    'method'=>"GET",
'header' => $headers

  )
);

$context = stream_context_create($opts);

$file1 = file_get_contents($remote_url, false, $context);

Thanks,

Matt

2
  • I'm calling it through PHP with a basic authorisation request, I can retrieve the body of the call and use that in my code, I just don't know how to access the header that is also being returned (I can see it's there using an api query app) ? Commented Oct 4, 2015 at 1:08
  • Apologies, I'm pretty new to working with apis, I'm currently calling the api through the following: $username = "XXXXXXXX"; $password = "XXXXXXXX"; $remote_url = 'XXXXXXXX'; $headers = array(); $headers[] = "Authorization: Basic " . base64_encode("$username:$password"); $opts = array( 'http'=>array( 'method'=>"GET", 'header' => $headers ) ); $context = stream_context_create($opts); $file1 = file_get_contents($remote_url, false, $context); Commented Oct 4, 2015 at 1:22

1 Answer 1

1

You can find the response headers in $http_response_header after making the call to file_get_contents - it'll be automatically populated in the local scope.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.