0

am hitting an api call using curl and I am sending "X_ACCESSKEY" as header. Following is my code

    $url = "http://127.0.0.1:8080/user/getheader";
         $data=array();
        $data = json_encode($data);            
        $ch = curl_init($url);            
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
        $headers = array('Content-Type:application/json','X_ACCESSKEY: 1234');
        curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
        # Return response instead of printing.
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        # Send request.
        $result = curl_exec($ch);
        curl_close($ch);

Now I want to get the "X_ACCESSKEY" header value in "http://127.0.0.1:8080/user/getheader"

2
  • Can you clarify your questions a bit more? Commented May 12, 2015 at 12:16
  • I am sending header values using curl to an api, I need to get those header values in the api file. HEADERS : $headers = array('Content-Type:application/json','X_ACCESSKEY: 1234'); I want to get ACCESSKEY as "1234" in my api file. Commented May 12, 2015 at 12:21

2 Answers 2

1

You can use the function getallheaders(), this function is an alias for apache_request_headers(), but it also appears to work on other webservers.

Documentation is listed here: http://php.net/manual/en/function.getallheaders.php

You may also look at this question: Get the http headers from current request in PHP

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

1 Comment

It is working for same server and it is not working for different server.
0

You can use apache_request_headers to receive all headers sent by the client.

<?php
$headers = apache_request_headers();
print_r($headers);
echo $headers["X_ACCESSKEY"];

2 Comments

I am not using apache server and i am using nginx, does it work for me?
getting an error that undefined function apache_request_headers()

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.