从PHP获取404的响应正文

时间:2015-07-07 11:29:17

标签: php http-status-code-404 response battlenet-api

我正在与battle.net API进行交互,当我点击404页面时,我需要获取响应正文。问题是我收到了错误。

我知道页面有一个正文,在文档中有一个包含正文的示例(截图如下)。我还提供了我的php错误的截图。任何帮助将不胜感激。

Battle.net Docs

这是我的代码:

$ responseCode = get_http_response_code($ api); //检查页面是否存在

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.....
}

谢谢, 詹姆斯麦克尼

编辑:当我直接访问URL时,我得到了JSON响应,所以我真的不知道这个错误来自哪里。

1 个答案:

答案 0 :(得分:2)

file_get_content不允许您在失败条件下获取内容。要获取内容,您必须使用较低级别的库;像卷曲一样的东西。