What on my PHP Curl call do I need to add to read a response's custom headers?
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 nginx http-headers
add a comment |
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 nginx http-headers
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 whatCURLOPT_HEADERFUNCTION
does?
– miken32
Nov 12 '18 at 21:30
Possible duplicate of 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
add a comment |
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 nginx http-headers
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 nginx http-headers
php nginx http-headers
edited Nov 12 '18 at 21:35
php_needs
asked Nov 12 '18 at 21:03
php_needsphp_needs
53
53
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 whatCURLOPT_HEADERFUNCTION
does?
– miken32
Nov 12 '18 at 21:30
Possible duplicate of 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
add a comment |
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 whatCURLOPT_HEADERFUNCTION
does?
– miken32
Nov 12 '18 at 21:30
Possible duplicate of 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
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
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
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
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?
– miken32
Nov 12 '18 at 21:31
Possible duplicate of 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
@miken32 what should the callback function I write do? do you have sample code
– php_needs
Nov 12 '18 at 21:35
add a comment |
1 Answer
1
active
oldest
votes
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);
Whoops - copied/pasted the wrong description, should be 2 - Thanks miken32
– symcbean
Nov 12 '18 at 21:35
PHP Parse error: syntax error, unexpected '===' (T_IS_IDENTICAL) in /app/catalog-sunbutter-feed.php on line 22 is code right
– php_needs
Nov 12 '18 at 23:07
any good/better code examples
– php_needs
Nov 12 '18 at 23:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270068%2fwhat-on-my-php-curl-call-do-i-need-to-add-to-read-a-responses-custom-headers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
Whoops - copied/pasted the wrong description, should be 2 - Thanks miken32
– symcbean
Nov 12 '18 at 21:35
PHP Parse error: syntax error, unexpected '===' (T_IS_IDENTICAL) in /app/catalog-sunbutter-feed.php on line 22 is code right
– php_needs
Nov 12 '18 at 23:07
any good/better code examples
– php_needs
Nov 12 '18 at 23:18
add a comment |
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);
Whoops - copied/pasted the wrong description, should be 2 - Thanks miken32
– symcbean
Nov 12 '18 at 21:35
PHP Parse error: syntax error, unexpected '===' (T_IS_IDENTICAL) in /app/catalog-sunbutter-feed.php on line 22 is code right
– php_needs
Nov 12 '18 at 23:07
any good/better code examples
– php_needs
Nov 12 '18 at 23:18
add a comment |
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);
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);
answered Nov 12 '18 at 21:32
symcbeansymcbean
41k44076
41k44076
Whoops - copied/pasted the wrong description, should be 2 - Thanks miken32
– symcbean
Nov 12 '18 at 21:35
PHP Parse error: syntax error, unexpected '===' (T_IS_IDENTICAL) in /app/catalog-sunbutter-feed.php on line 22 is code right
– php_needs
Nov 12 '18 at 23:07
any good/better code examples
– php_needs
Nov 12 '18 at 23:18
add a comment |
Whoops - copied/pasted the wrong description, should be 2 - Thanks miken32
– symcbean
Nov 12 '18 at 21:35
PHP Parse error: syntax error, unexpected '===' (T_IS_IDENTICAL) in /app/catalog-sunbutter-feed.php on line 22 is code right
– php_needs
Nov 12 '18 at 23:07
any good/better code examples
– php_needs
Nov 12 '18 at 23:18
Whoops - copied/pasted the wrong description, should be 2 - Thanks miken32
– symcbean
Nov 12 '18 at 21:35
Whoops - copied/pasted the wrong description, should be 2 - Thanks miken32
– symcbean
Nov 12 '18 at 21:35
PHP Parse error: syntax error, unexpected '===' (T_IS_IDENTICAL) in /app/catalog-sunbutter-feed.php on line 22 is code right
– php_needs
Nov 12 '18 at 23:07
PHP Parse error: syntax error, unexpected '===' (T_IS_IDENTICAL) in /app/catalog-sunbutter-feed.php on line 22 is code right
– php_needs
Nov 12 '18 at 23:07
any good/better code examples
– php_needs
Nov 12 '18 at 23:18
any good/better code examples
– php_needs
Nov 12 '18 at 23:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270068%2fwhat-on-my-php-curl-call-do-i-need-to-add-to-read-a-responses-custom-headers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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?
– 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