401 Access Denied (PHP)

I normally play with web design in my spare time, and don’t normally deal with APIs so I figured this could be good for me. Unfortunately I tripped over the first hurdle. I wrote a very short script just to make sure that I was actually doing this right, and I managed to hit a 401 error. Any help is appreciated, thanks!

Code:

require_once ‘HTTP/Request2.php’;
$request = new HTTP_Request2(‘https://www.haloapi.com/profile/h5/profiles/{player}/spartan’, HTTP_Request2::METHOD_GET);
$headers = array(
// Request headers
‘Ocp-Apim-Subscription-Key’ => ‘****’,
);
try {
$response = $request->send();
if (302 == $response->getStatus()) {
echo $response->getBody();
} else {
echo 'Error: ’ . $response->getStatus() . ’ ’ .
$response->getReasonPhrase();
}
} catch (HTTP_Request2_Exception $e) {
echo 'Error: ’ . $e->getMessage();
}
?>

You seem to be missing this line

> $request->setHeader($headers);

before your try/catch block so your API key is never sent. Also make sure you replace {player} in the URL with a Gamertag :slight_smile:

> 2533274792173269;2:
> You seem to be missing this line
>
>
> > $request->setHeader($headers);
>
>
> before your try/catch block so your API key is never sent. Also make sure you replace {player} in the URL with a Gamertag :slight_smile:

Thanks for the reply (didn’t even realize I forgot to include that), but it didn’t seem to work. I still hit the 401 error.

> 2533274877535909;3:
> > 2533274792173269;2:
> > You seem to be missing this line
> >
> >
> > > $request->setHeader($headers);
> >
> >
> > before your try/catch block so your API key is never sent. Also make sure you replace {player} in the URL with a Gamertag :slight_smile:
>
>
> Thanks for the reply (didn’t even realize I forgot to include that), but it didn’t seem to work. I still hit the 401 error.

Seems to be working on my end so not sure what’s causing that. Could you try this: spartan.php · GitHub
Made a few slight changes so it should result in the URL to your Spartan image. If you get any SSL errors, you’ll need a cacert.pem file (from here: http://curl.haxx.se/docs/caextract.html) and drop that into the same directory as the .php file.