0

A custom created HTTP header whose value I need to fetch and for this, I am using following WordPress function

$response = wp_remote_get('http://example.com/page/');
$auth = wp_remote_retrieve_header( $response, 'Cache-Control' ); //Calling http header value of name "cach-control"

In above code, I am fetching a value of "Cache-Control" which is a default HTTP header. I know WordPress supports only limited HTTP header. I found one plugin that allow us to use more HTTP headers. But even with this plugin, I can't fetch the value of the custom created header.

Is there any way in which I can get the value of custom header?

1 Answer 1

2

I checked amazon.com website for retrive X-Amz-Cf-Id header which I belive is not a standard header and for me it is working fine:

function wpse_288865_featch_header() {

    $response = wp_remote_get('https://www.amazon.com/');
    $custom_header = wp_remote_retrieve_header($response, 'X-Amz-Cf-Id');

    var_dump($custom_header);
    exit;
}

add_action('init', 'wpse_288865_featch_header');

Sometimes amazon.com is not returning this header so please refresh your WordPress site couple of times.

I also inspected WordPress core for parsing request and didn't found some kind of whitelist of headers. It is only removing transfer-encoding and connection headers.

Please check if you make proper request to your site. For the first time I have checked google.com and have similar issue. I couldn't get the headers which I've seen that google.com should return. Problem was that google.com do not allow me to make such a request and in return I get HTTP/1.1 403 Forbidden response which obviously return completely different headers that HTTP/1.1 200 response.

5
  • Ok after doing some more testing I found the real issue. In normal way it is working but when I send http header from outside the website i.e. from different domain then it don't fetch the http header. I tried your amazon http header and in normal it was working But when I tried to send it via Postman(chrom's extension) for testing purpose then didn't work. In my case http header will come from outside of site that's why testing on postman. Any suggestion? For additional detail, I am using it on custom template file, not on functions.php So doing it without hooks.
    – Rishabh
    Dec 18, 2017 at 12:11
  • What is in the response array? $response['response']? It should be 200 code and OK message.
    – kierzniak
    Dec 18, 2017 at 12:15
  • I made a custom template and put this in it $response = wp_remote_get('https://www.amazon.com/'); $custom_header = wp_remote_retrieve_header($response, 'X-Amz-Cf-Id');. When I do refresh page(in whch I attach this template) then it fetch header. But when I do this via postman then it don't fetch header.
    – Rishabh
    Dec 18, 2017 at 12:19
  • To check if $custom_header is getting any value or not I am creating post with pending status like this (just for testing) $my_post = array(); $my_post['post_title'] = 'Post2'; $my_post['post_content'] = $custom_header; $my_post['post_status'] = 'pending'; $my_post['post_author'] = 1; wp_insert_post( $my_post ); And storing value of $custom_header in content of post.
    – Rishabh
    Dec 18, 2017 at 12:23
  • I'm sorry but I don't know postman. Maybe this is problem of postman not WordPress or other site?
    – kierzniak
    Dec 18, 2017 at 12:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.