file_get_contents在失败时返回字符串而不是FALSE


file_get_contents returning string instead of FALSE on fail

我使用以下代码来确定file_get_contents是否成功:

if($toon = @file_get_contents('http://us.battle.net/api/wow/character/magtheridon/ronuburggundy'))
{
}

我们最近迁移到了一个新的主机,我们正在运行php5.4.8(我不确定这是否是问题所在,我们以前有5.4.7)。

$toon = file_get_contents('http://us.battle.net/api/wow/character/magtheridon/ronuburggundy');

应失败并返回FALSE。但取而代之的是:

echo $toon;

现在打印:

{"status":"nok", "reason": "Character not found."}

我看不出导致变化的回报在哪里。

我的目录中有一个自定义的php.ini,但它实际上只将所有allow_url_fopen()从默认的Off.设置为On

感谢您的意见。

不,我想解决这个问题的方式,我仍然不明白为什么行为突然改变了。

但下面的helper函数现在用于检查请求的有效性。

$toon = request_toon("http://us.battle.net/api/wow/character/magtheridon/ronburggundy");
function request_toon($url)
{
$toon = @file_get_contents($url);
    if($toon)
    {
    $toon = json_decode($toon);
        if(array_key_exists('status', $toon))
        {
            return false;
        }
        else
        {
            return $toon;
        }
    }
    return false;
}

这看起来是在服务器端。服务器告诉你找不到你要找的角色。对file_get_contents()的调用没有失败,因此不会返回false。

如果您在浏览器中访问该页面(链接),您可以看到该文件确实包含内容。您必须测试"未找到字符"的返回内容,然后您就会知道搜索该字符失败。