Thomas Riddler
Thomas Riddler

Reputation: 393

PHP Header exists but cannot retrieve

I am using this method to make apache header request work in nginx.

if (!function_exists('apache_request_headers')) { 
    function apache_request_headers() { 
        foreach($_SERVER as $key=>$value) { 
            if (substr($key,0,5)=="HTTP_") { 
                $key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5))))); 
                $out[$key]=$value; 
            }else{ 
                $out[$key]=$value; 
            } 
        } 
        return $out; 
    } 
} 

I retrieve the header like so $headers = apache_request_headers(); and use an array to hold json.

$response=array()
$response["error"] = true;
$response["message"] = $headers;

The code below is what is inside of the $response array variable:

{
error: true
message: {
CONTENT_LENGTH: "13"
CONTENT_TYPE: "application/x-www-form-urlencoded"
DOCUMENT_ROOT: "/home4/admin/public_html"
GATEWAY_INTERFACE: "CGI/1.1"
Accept: "*/*"
Accept-Encoding: "gzip, deflate"
Accept-Language: "en-US,en;q=0.8"
Connection: "close"
Cookie: "_ga=GA1.2.1266385826.1428275832"
Host: "mysite.com"
Origin: "chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo"
User-Agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36"
X-Apikey: "bca0de3e7c10cb6623ef00021caf9450"
X-Http-Proto: "HTTP/1.1"
X-Log-7528: "107.147.160.193"
X-Real-Ip: "107.147.160.193"
PATH: "/bin:/usr/bin"
PHPRC: "/home4/admin"
QUERY_STRING: ""
REDIRECT_STATUS: "200"
REDIRECT_UNIQUE_ID: "VVlk5sD@@rgAACwVT0AAAACM"
REDIRECT_URL: "/rmapp/v1/tasks"
REMOTE_ADDR: "107.147.160.193"
REMOTE_PORT: "31527"
REQUEST_METHOD: "POST"
REQUEST_URI: "/rmapp/v1/tasks"
SCRIPT_FILENAME: "/home4/admin/public_html/rmapp/v1/index.php"
SCRIPT_NAME: "/rmapp/v1/index.php"
SERVER_ADDR: "192.185.226.161"
SERVER_ADMIN: "[email protected]"
SERVER_NAME: "mysite.com"
SERVER_PORT: "80"
SERVER_PROTOCOL: "HTTP/1.1"
SERVER_SIGNATURE: "<address>Apache Server at mysite.com Port 80</address> "
SERVER_SOFTWARE: "Apache"
UNIQUE_ID: "VVlk5sD@@rgAACwVT0AAAACM"
PHP_SELF: "/rmapp/v1/index.php"
REQUEST_TIME: 1431921894
argv: [0]
argc: 0
}-
}

My problem is, I need to grab X-ApiKey from $header, but using $api_key = $headers['X-ApiKey']; returns nothing, but as you can see, X-ApiKey exists in $header. Can someone please tell me what I'm missing here?

Upvotes: 2

Views: 642

Answers (2)

Ravi
Ravi

Reputation: 877

Looks like you are using the wrong variable name. Variable names are case-sensitive.

The response returns the following (small k in Apikey): X-Apikey: "bca0de3e7c10cb6623ef00021caf9450"

while you are using capital k $api_key = $headers['X-ApiKey'];

Try the following: $api_key = $headers['X-Apikey'];

Upvotes: 1

Sougata Bose
Sougata Bose

Reputation: 31739

$response["message"] is a string. First try to explode it and then access it -

$temp = explode('X-Apikey:', $response['message']);
$temp1 = explode('X-Http-Proto:', $temp[1]);
var_dump(trim(str_replace('"', '', $temp1[0])));

Output

string(32) "bca0de3e7c10cb6623ef00021caf9450"

Upvotes: 1

Related Questions