Issues with setting headers

I am having issues with setting the OAuth has a header, but seems to work fine when it is inline…

This code does not work:

$url = "https://api.twitch.tv/kraken/users/" .$_SESSION['user']['name']. "/subscriptions/trumpsc";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 'Accept: application/vnd.twitchtv.v3+json');
curl_setopt($ch, CURLOPT_HEADER, 'Authorization: OAuth ' .$_SESSION['user']['token']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

This code does work:

$url = "https://api.twitch.tv/kraken/users/" .$_SESSION['user']['name']. "/subscriptions/trumpsc?oauth_token=" .$_SESSION['user']['token'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 'Accept: application/vnd.twitchtv.v3+json');
//curl_setopt($ch, CURLOPT_HEADER, 'Authorization: OAuth ' .$_SESSION['user']['token']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

What am I doing wrong? Can someone explain?

should be:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/vnd.twitchtv.v3+json',
    'Authorization: OAuth ' .$_SESSION['user']['token']
));

The call to CURLOPT_HEADER controls if the HTTP HEADERS are passed back from the Request to curl_exec (as further more directed by CURLOPT_RETURNTRANSFER)

Please see the documentation

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.