Search code examples
phpandroidcurlhttp-headers

Receive a variable in http header PHP


I know this question is not going to be so clear as most similar questions on here are talking about sending headers, also there's a lot of questions similar to but not answering this specifically, also sorry if the answer is extremely obvious.

So I've got an apk that sends X-API-Key in the header PHP conversion of this code is:

$url = 'example.something.org/test.php';
$data = "$some_data";
$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_POST, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $data);
curl_setopt($cURL, CURLOPT_HEADER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array("X-API-KEY"=>"FIND_MEEEEEEEE"));

What I want is to collect the X-API-KEY and store it as a variable so that I can then send another request from test.php similar to the one shown, its not an option to run the request directly from the apk as I don't want this data stored client side, I need the X-API-KEY for the request being sent from the server, so technically it is like this apk->my_server->external_server

I have tried using print_r for $_POST and $_SERVER echoed all keys in header() multiple other things that I can't even think of right now as I've tried everything and searched Google as much as possible.

Please link to answer if this has been answered already.

EDIT:
I've also tried using this js code which gives me the exact same headers:

var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
alert(headers);

Solution

  • Set header as:-

    $headers = array(
        'Content-type: application/json',
        "X-API-KEY"=>"FIND_MEEEEEEEE"
    );
    curl_setopt($cURL, CURLINFO_HEADER_OUT, true);
    curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers);
    

    in your test.php file write this lines:-

    foreach (getallheaders() as $name => $value) {
        echo "$name: $value\n";
    }
    

    Hope it will help you :)