【问题标题】:PHP file_get_contents Maximum execution time exceeded (Cause of emotes?)PHP file_get_contents 超过最大执行时间(表情的原因?)
【发布时间】:2015-05-22 06:45:18
【问题描述】:

我不久前开始使用 Steam API,并使用 GetPlayerSummaries 方法创建了一个显示个人资料信息的简单网站。它一直运行良好,直到我的一些朋友开始在他们的名字中添加 iOS 表情,这导致以下情况:

致命错误文件第 137 行的最大执行时间超过 300 秒

这是文件中该行的内容:

public function getPlayerSummary($steamid) {
    $contents = file_get_contents( "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".SteamAPI_APIKey."&steamids=".$steamid );
    $json = json_decode($contents, true);

    foreach($json['response']['players'] as $key => $value) {
        ...
    }
}

(这是在函数中,调用函数时定义了$steamid)

这应该返回(JSON):

{
"response": {
    "players": [
        {
            "steamid": "_removed_",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "_removed_ ????",
            "lastlogoff": 1426734965,
            "profileurl": "http://steamcommunity.com/id/_removed_/",
            "avatar": "_removed_",
            "avatarmedium": "_removed_",
            "avatarfull": "_removed_",
            "personastate": 0,
            "realname": "_removed_",
            "primaryclanid": "_removed_",
            "timecreated": 1349251405,
            "personastateflags": 0,
            "loccountrycode": "DE"
        }
    ]

}

}

但是“??????”在personaname中搞砸了,因为它对没有它的其他人都有效:/ 这是一个幽灵表情,另一个朋友在他的名字中使用猴子表情也是如此。

有人知道我该如何解决这个问题吗?

【问题讨论】:

  • 请贴出更多代码,我不认为file_get_contents 会导致这种情况(PHP 不太擅长报告错误),我猜json_decode 无法解析奇怪的字符,你可以replace not utf-8 chars in content 然后json_decode 字符串
  • 添加了一点,虽然函数中除了file_get_contents 之外什么都没有,仍然会导致它:/
  • 如果您尝试逐字符读取文件会发生什么? pastebin.com/Qr7RxvHW
  • 行得通!将字符集设置为 UTF-8,它会将表情变成一个正方形(并变成我手机上的实际表情)——我应该使用这个,然后使用 json_decode 还是一个坏主意?
  • 怎么样:pastebin.com/9FTEsjYY 逐字符读取文件不是一个好主意:)

标签: php json api file-get-contents steam


【解决方案1】:

将下面一行放在php文件的顶部,

ini_set("max_execution_time", 0);

【讨论】:

  • 我不确定这对我的案子有什么帮助?那会导致页面永远加载不出来,哈哈
  • 你可以在 php.ini 中做到这一点
  • 是的,我知道,但这有什么帮助? :/ 我做到了,但它永远不会加载
【解决方案2】:

注意:

如果您要打开带有特殊字符(例如空格)的 URI,则需要使用 urlencode() 对 URI 进行编码。

http://php.net/manual/en/function.file-get-contents.php

【讨论】:

    猜你喜欢
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    相关资源
    最近更新 更多