0

I'm trying to get a Token in the header of an POST request. With Postman it works but when I run this in PHP I get the right content but cannot extract the header information.

I have copied the PHP code generated in Postman but I cannot extract the header information. I only get the content.

The code I used:

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://xxxx.yyyy.xy/xxxxxx/xxxxxxxxxxx",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "Content-Type: application/x-www-form-urlencoded",
    "Postman-Token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
?>

The header should include "Token". I cannot recieve any header information.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
PelleH
  • 1
  • 1

1 Answers1

-1

I found a solution on the problem:

<?php

$curl = curl_init();

$Username = "XXXXXXXXX";
$Password = "YYYYYYYYYY";
$Credentials = base64_encode($Username.':'.$Password);
$Credentials_Auth = "Authorization: Basic ".$Credentials;
// echo "<br>Cred-auth: ".$Credentials_Auth."<br>";

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://XXXXXXXXX/YYYYYYYYYYY/ZZZZZZZZZZZ",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  **CURLOPT_HEADER => 1,**
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
    $Credentials_Auth,
    "Content-Type: application/x-www-form-urlencoded",
    "cache-control: no-cache"
  ),
));
$response = curl_exec($curl);
$err = curl_error($curl);

if ($err) { echo "cURL Error #:" . $err;}
else { echo $response; }

**$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);**

curl_close($curl);

**header("Content-Type:text/plain; charset=UTF-8");
echo $headers;
echo $body;**
?>
PelleH
  • 1
  • 1