phphttp-status-code-404responsebattlenet-api

Get response body from 404 in PHP


I am interacting with the battle.net API and I need to get the response body when I hit a 404 page. The problem is I get an error.

I know the page has a body as in the documentation there is an example that has the body (screenshot below). I have also put in a screenshot of my php error. Any help would be appreciated.

Battle.net Docs

here is my code:

$responseCode = get_http_response_code($api); //Checks page exists

if($responseCode == 200) { //Page exists
    $api = "battle.net/apirequest.......";
    $json = file_get_contents($api);
    $schema = json_decode($json, true);

    $completeQuests = $schema['quests'];
} else if ($responseCode == 404) {
     ...Trying to find out why here.....
}

Thanks, James McNee

EDIT: Also when I go directly to the URL I get the JSON response so I really dont know where this error is coming from.


Solution

  • file_get_content would not allow you to get the content on failure conditions. To get the content you would have to use a lower level library; something like say curl.