0

I tried a few several ways to read the responses custom header but have not been able to. I know the response I get is served by nginx and the custom header names start with X-......

$endpoint =  'url here';
$ch = curl_init( $endpoint );

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'cbFunc');                                                                    


$result = curl_exec($ch);

print_r( curl_getinfo($ch ) );
php_needs
  • 45
  • 4
  • 1
    Show what you tried (in your question) along with describing the actual result vs the expected result. – Patrick Q Nov 12 '18 at 21:07
  • ok @PatrickQ I updated w/ my code sample above – php_needs Nov 12 '18 at 21:15
  • Have you read the documentation for these things? Are you aware of what `CURLOPT_HEADERFUNCTION` does? – miken32 Nov 12 '18 at 21:30
  • Possible duplicate of [Can PHP cURL retrieve response headers AND body in a single request?](https://stackoverflow.com/questions/9183178/can-php-curl-retrieve-response-headers-and-body-in-a-single-request) – miken32 Nov 12 '18 at 21:31
  • @miken32 what should the callback function I write do? do you have sample code – php_needs Nov 12 '18 at 21:35

1 Answers1

0

The PHP manual is an excellent reference guide an a good starting point when you run into problems like this.

CURLOPT_HEADERFUNCTION [Set value to] A callback accepting five parameters.

hence

log_headers('init');
$endpoint =  'url here';
$ch = curl_init( $endpoint );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'log_headers');                                                                   
$result = curl_exec($ch);
$headers=log_headers();
print_r($headers);

function log_headers($ch=false, $headers=false)
{
    static $hdrs;
    if (is_array($hrs) && $ch===$headers===false) {
        return $hdrs[];
    } elseif ($ch==='init') {
        $hdrs=array();
        return 0;
    } 
    $hdrs[]=$headers;
    return strlen($headers);
}  
symcbean
  • 47,736
  • 6
  • 59
  • 94