0

I'm trying out HTTP Requests for the first time (also using PostMan). There, everything works fine. When exporting it with PHP cURL I get the following:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "htt..",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic ..."
  ),
));

$response = curl_exec($curl);

curl_close($curl);

I am expecting a Key in the response Header, So I'm trying to get the Response Header as a variable (pref. Array). How would I do this? I got no luck so far trying to use the following:

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

Thanks for any help in advance.

Edit 1:

Using https://incarnate.github.io/curl-to-php/ I converted the Postman cURL command to PHP cURL (instead of using Postman's generator) and got the following, which seems to work:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'htt..');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //manually added
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //manually added
curl_setopt($ch, CURLOPT_HEADER, 1); //manually added

$headers = array();
$headers[] = 'Authorization: Basic ...';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);

curl_close($ch);
echo $header;

2 Answers 2

2

Your'e really close to solving this, but you need to set the CURLOPT_HEADER to true to retrieve the headers. This change should work:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "htt..",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HEADER => true, 
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic ..."
  ),
));

$response = curl_exec($curl);
if (curl_errno($curl) !== CURLE_OK) {
    throw new \RuntimeException("curl_exec error: " . curl_error($ch) . ": " . curl_error($ch));
}

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

curl_close($curl);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion, I tried that and I seem to still get 0 on Header_Size.
@Wrsn this code should work. the only thing this code lacks is header=>array parsing and error checking. if you get 0 headers from this code, it means there's a curl exec error, and because this code is lacking error checking, the error isn't detected. try adding $response = curl_exec($curl); if(curl_errno($curl)!==CURLE_OK){throw new \RuntimeException("curl_exec error: ".curl_errno($ch).": ".curl_error($ch));} - it should throw an exception explaining what happened.
0

$response is always only response body.

Try to look at option for curl_getinfo

CURLINFO_HEADER_OUT - The request string sent. For this to work, add the CURLINFO_HEADER_OUT option to the handle by calling curl_setopt()

Here is the documentation https://www.php.net/manual/en/function.curl-getinfo.php

4 Comments

Using: $test = curl_getinfo($curl, CURLINFO_HEADER_OUT); echo $test; I see that i'm getting null. with CURLINFO_HEADER_OUT I get 0 as a response. So may it be that I may not even getting a response at all? (with a normal non-php cURL call in unix on the system I get the expected response)
Have you add CURLINFO_HEADER_OUT => true to curl_setopt_array?
Yes, Tried that. Sadly same result :/
sorry but this will fetch your own REQUEST headers, not the RESPONSE headers, but @Wrsn want the RESPONSE headers, not the REQUEST headers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.