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"

CodeWithCoffee
  • 1,896
  • 2
  • 14
  • 36
Sagar
  • 259
  • 2
  • 3
  • 14
  • Can you clarify your questions a bit more? – take May 12 '15 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. – Sagar May 12 '15 at 12:21

2 Answers2

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

Community
  • 1
  • 1
maxdaniel98
  • 6,623
  • 6
  • 19
  • 21
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"];
take
  • 2,202
  • 2
  • 19
  • 36