skip to Main Content

I have integration with other servers using API
but I can not get value from the response header to complete authentication and get the session of the server

I try this code


$init = curl_init();

$Auth = [];

$response_headers = [];

$header_callback = function($init,$Auth) use (&$response_headers){
    $len = strlen($Auth);

    $response_headers[] = $Auth;

    return $len;
};

curl_setopt_array($init, [
    CURLOPT_URL => 'http://ip-api/v1/rest/auth',
    CURLOPT_HEADER => true,
    CURLOPT_HEADERFUNCTION => $header_callback
    
]);

$output = curl_exec($init);

$info = curl_getinfo($init);

var_dump($Auth);

echo $Auth[0];

curl_close($init);

but the result is

Warning: Undefined array key 0 in //path

how can get this value

2

Answers


  1. You are looking this php function, if I understand right apache_request_headers().

    If you have apache this will work. An you will get an array with your headers that you can loop over and take the ['Auth-Nonce'] key. If apache doesn’t have the $header['Auth-Nonce'] in the specific HTTP call, null will be returned.

    $headers = apache_request_headers();
    echo $header['Auth-Nonce'];
    

    In case of Nginx and other webservers try this getallheaders(). If you get an error that says the function doesn’t exist do it custom like this:

    if (!function_exists('getallheaders'))  {
        function getallheaders()
        {
            if (!is_array($_SERVER)) {
                return array();
            }
    
            $headers = array();
            foreach ($_SERVER as $name => $value) {
                if (substr($name, 0, 5) == 'HTTP_') {
                    $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
                }
            }
            return $headers;
        }
    }
    

    In any case you will get your webserver headers on your call.

    Login or Signup to reply.
  2. The data you need is within $response_headers, not $Auth.

    e.g.

    echo $response_headers [11];
    

    would get you the specific value.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search