0

I'am trying to send via curl a rest request with a header consisting of authorization key and api key.

Therefore I build following curl call:

$api_key = 'abc';
$token = 'xyz';


$authorization = 'Authorization: Bearer ' . $token;
$api = 'Api_key: ' . $api_key;

curl_setopt($curl, CURLOPT_HTTPHEADER, array($authorization, $api));

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLINFO_HEADER_OUT, true); // Detail information for debugging
curl_setopt($curl,CURLOPT_VERBOSE,true);
$curl_response = curl_exec($curl);

Debugging the header on the server side shows a header collection with:

expect (array)
accept (array)
host (array)
authorization (array)

A closer look to the authorization array shows that there is only one item. This is the value of the authorization key from the client. The second value, the api_key is missing in the header/authorization array.

Request_header of the client shows, Api_key is available:

Host: localhost
Authorization: Basic RG9yaXMgS3JhenM6S3JhdXMxMDAw
Accept: */*
Api_key: abc
Content-Length: 458
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------224938f738738f42

Why is the server not able to recieve the full http header authentication information, which is build on an array. Development environment is apache, php 7, yii, phpstorm.

2
  • How are you checking/debugging the headers on the server side to get that result? Have you checked if there's anything installed on the server that clears custom headers? Commented Apr 11, 2018 at 9:25
  • I'am debugging with xdebug and phpstorm. Basically there is nothing installed on the apache server which is not needed.
    – Perino
    Commented Apr 11, 2018 at 9:41

1 Answer 1

0

I've solved the problem with putting the API key as 'x-api-key' in the HTTP Header.

This looks like the following code:

$api_key = 'abc';
$token = 'xyz';

$authorization = 'Authorization: Bearer ' . $token;
$api = 'x-api-key: ' . $api_key;
curl_setopt($curl, CURLOPT_HTTPHEADER, array($authorization, $api));

Your Answer

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

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