How do I echo returned header location returned by Php POST?

AM using the code below but it dump a lot of data while I just need a header location from the returned header and discard the rest.

<?php

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://sandbox.kopokopo.com/api/v1/payments',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HEADER => 1,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "destination_type": "till",
    "destination_reference": "3eccf0c1-ab6b-41a4-b51a-4e8f588105f1",
    "description": "4563",
    "amount": {
        "currency": "KES",
        "value": "500"
    },
    "metadata": {
        "something": "TST-01",
        "something_else": "Something else"
    },
    "_links": {
        "callback_url": "http://portal.zarafu.com/payments"
    }
}',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer Y-lLtyDcuBHltGFN5PsoxWjZMnOzESi-D-FZn2J9WDY',
    'Accept: application/json',
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);

curl_close($curl);
//echo $response;

$headers = explode("\n", $header);
echo implode($headers);
//echo $content_type;

Hello, call returns json. Then you just specify the part you need.

Read this and it will clarify everything for you I’m sure.

https://www.php.net/manual/en/function.json-decode.php

Sponsor our Newsletter | Privacy Policy | Terms of Service