phparraysget-headers

Need php get_headers() results assigned to individual variables


So i can display the response headers using

           $url = 'http://example.com';

           $headers = get_headers($url,0);

           foreach ($headers as $header => $key) {
           echo "$header: $key <br />\n";

And I get back all the headers. However I just need a few of them. What I need to do is assign each result in the array to a variable. I have tried just trying to get them individually using

           $url = 'http://example.com';

           $headers = get_headers($url,0);

           $results = json_decode($headers, true);

           $RateLimit = $results['X-RateLimit-Limit'];
           $LimitRemain = $results['X-RateLimit-Remaining'];

And this obviously didn't work. I also am confused how to pull the HTTP/1.1 200 OK response out of the array as well as it seems to me that all the results are in JSON but there is no key and value structure to this particular header result.


Solution

  • use extract(get_headers($url, 1));

    Passing 1 into get_headers returns it as an associative array. Then extract converts that to variables.