用于动态图像的PHP / JSON解析器无法正常工作

时间:2014-04-19 19:15:20

标签: php json parsing dynamic-image-generation

我非常接近让这个解析器/图像工作,但我有一些最终的问题阻止我发布它。更糟糕的是,我完全不知道问题是什么。

以下是整个代码:

<?php
    ini_set('display_errors','On');//Remove when done
    error_reporting(E_ALL|E_STRICT);//Remove when done
    //header("Content-Type:image/png");//Removed for troubleshooting purposes only
    function FAHUser($user){
        $dom=new DOMDocument();
        $html=file_get_contents('http://folding.extremeoverclocking.com/user_summary.php?s=&u='.$user);
        $dom->loadHTML($html);
        $td=$dom->getElementsByTagName('table')->item(6)->getElementsByTagName('tr')->item(1)->getElementsByTagName('td');
        $json=array(
            "userName"=>$dom->getElementsByTagName('h1')->item(0)->textContent,
            "userTeamRank"=>$td->item(0)->textContent,
            "userOverallRank"=>$td->item(1)->textContent,
            "userPoints"=>$td->item(6)->textContent,
            "userPPD"=>$td->item(3)->textContent
        );
        $json['date']=date("Y-m-d G:i:s");
        $userInfo=fread(fopen("scripts/users.json","r"),filesize("scripts/users.json"));
        $userInfoArr=json_decode($userInfo);
        $userInfoArr->{$user}=$json;
        $userInfoArr=json_encode($userInfoArr);
        $userInfo=fopen("scripts/users.json","w");
        fwrite($userInfo,$userInfoArr);
        fclose($userInfo);
        return json_decode($userInfoArr);
    }
    function FAHTeam($team){
        $dom=new DOMDocument();
        $html=file_get_contents('http://folding.extremeoverclocking.com/team_summary.php?s=&t='.$team);
        $dom->loadHTML($html);
        $td=$dom->getElementsByTagName('table')->item(6)->getElementsByTagName('tr')->item(1)->getElementsByTagName('td');
        $json=array(
            "teamName"=>$dom->getElementsByTagName('h1')->item(0)->textContent,
            "teamRank"=>$td->item(0)->textContent,
            "teamPoints"=>$td->item(9)->textContent,
            "teamPPD"=>$td->item(3)->textContent,
            "teamToday"=>$td->item(7)->textContent
        );
        $json['date']=date("Y-m-d G:i:s");
        $teamInfo=fread(fopen("scripts/teams.json","r"),filesize("scripts/teams.json"));
        $teamInfoArr=json_decode($teamInfo);
        $teamInfoArr->{$team}=$json;
        $teamInfoArr=json_encode($teamInfoArr);
        $teamInfo=fopen("scripts/teams.json","w");
        fwrite($teamInfo,$teamInfoArr);
        fclose($teamInfo);
        return json_decode($teamInfoArr);
    }
    function BOINCUser($user){
        $dom=new DOMDocument();
        $html=file_get_contents('http://boincstats.com/en/stats/-1/user/detail/'.$user.'/projectList');
        $dom->loadHTML($html);
        $tr=$dom->getElementsByTagName('table')->item(0)->getElementsByTagName('tr')->item(1);
        $ttl=str_replace(',','',$tr->getElementsByTagName('td')->item(1)->textContent);
        $tot=number_format(round($ttl));
        $json=array(
            "userName"=>$dom->getElementsByTagName('span')->item(0)->textContent,
            "userTeamRank"=>$tr->getElementsByTagName('td')->item(10)->textContent,
            "userOverallRank"=>$tr->getElementsByTagName('td')->item(6)->textContent,
            "userPoints"=>$tot,
            "userPPD"=>$tr->getElementsByTagName('td')->item(3)->textContent
        );
        $json['date']=date("Y-m-d G:i:s");
        $userInfo=fread(fopen("scripts/boincusers.json","r"),filesize("scripts/boincusers.json"));
        $userInfoArr=json_decode($userInfo);
        $userInfoArr->{$user}=$json;
        $userInfoArr=json_encode($userInfoArr);
        $userInfo=fopen("scripts/boincusers.json","w");
        fwrite($userInfo,$userInfoArr);
        fclose($userInfo);
        return json_decode($userInfoArr);
    }
    function BOINCTeam($team){
        $dom=new DOMDocument();
        $html=file_get_contents('http://boincstats.com/en/stats/-1/team/detail/'.$team.'/projectList');
        $dom->loadHTML($html);
        $tr=$dom->getElementsByTagName('table')->item(0)->getElementsByTagName('tr')->item(1);
        $ttl=str_replace(',','',$tr->getElementsByTagName('td')->item(1)->textContent);
        $tot=number_format(round($ttl));
        $json=array(
            "teamName"=>$dom->getElementsByTagName('span')->item(0)->textContent,
            "teamRank"=>$tr->getElementsByTagName('td')->item(6)->textContent,
            "teamPoints"=>$tot,
            "teamPPD"=>$tr->getElementsByTagName('td')->item(3)->textContent,
            "teamToday"=>$tr->getElementsByTagName('td')->item(7)->textContent
        );
        $json['date']=date("Y-m-d G:i:s");
        $teamInfo=fread(fopen("scripts/boincteams.json","r"),filesize("scripts/boincteams.json"));
        $teamInfoArr=json_decode($teamInfo);
        $teamInfoArr->{$team}=$json;
        $teamInfoArr=json_encode($teamInfoArr);
        $teamInfo=fopen("scripts/boincteams.json","w");
        fwrite($teamInfo,$teamInfoArr);
        fclose($teamInfo);
        return json_decode($teamInfoArr);
    }
    if(isset($_GET['u'])&&isset($_GET['t'])&&!isset($_GET['w'])){
        $userInfoFile=fopen('scripts/users.json','r');
        $userInfo=fread($userInfoFile,filesize('scripts/users.json'));
        fclose($userInfoFile);
        $userInfo=json_decode($userInfo);
        isset($userInfo->{$_GET['u']})?$user=$userInfo->{$_GET['u']}:$user=FAHUser($_GET['u'])->{$_GET['u']};
        $hours=(strtotime(date("Y-m-d G:i:s"))-strtotime($user->date))/3600;
        //if($hours>=3){//Removed for troubleshooting purposes only
            FAHUser($_GET['u']);
        //}//Removed for troubleshooting purposes only
        $user=$userInfo->{$_GET['u']};
        $teamInfoFile=fopen('scripts/teams.json','r');
        $teamInfo=fread($teamInfoFile,filesize('scripts/teams.json'));
        fclose($teamInfoFile);
        $teamInfo=json_decode($teamInfo);
        isset($teamInfo->{$_GET['t']})?$team=$teamInfo->{$_GET['t']}:$team=FAHTeam($_GET['t'])->{$_GET['t']};
        $hours=(strtotime(date("Y-m-d G:i:s"))-strtotime($team->date))/3600;
        //if($hours>=3){//Removed for troubleshooting purposes only
            FAHTeam($_GET['t']);
        //}//Removed for troubleshooting purposes only
        $FAHBOINC="  F@H User:";
        $pointRank="Points Today:";
    }elseif(!isset($_GET['u'])&&isset($_GET['t'])&&isset($_GET['w'])){
        $userInfoFile=fopen('scripts/boincusers.json','r');
        $userInfo=fread($userInfoFile,filesize('scripts/boincusers.json'));
        fclose($userInfoFile);
        $userInfo=json_decode($userInfo);
        isset($userInfo->{$_GET['w']})?$user=$userInfo->{$_GET['w']}:$user=BOINCUser($_GET['w'])->{$_GET['w']};
        $hours=(strtotime(date("Y-m-d G:i:s"))-strtotime($user->date))/3600;
        //if($hours>=3){//Removed for troubleshooting purposes only
            BOINCUser($_GET['w']);
        //}//Removed for troubleshooting purposes only
        $user=$userInfo->{$_GET['w']};
        $teamInfoFile=fopen('scripts/boincteams.json','r');
        $teamInfo=fread($teamInfoFile,filesize('scripts/boincteams.json'));
        fclose($teamInfoFile);
        $teamInfo=json_decode($teamInfo);
        isset($teamInfo->{$_GET['t']})?$team=$teamInfo->{$_GET['t']}:$team=BOINCTeam($_GET['t'])->{$_GET['t']};
        $hours=(strtotime(date("Y-m-d G:i:s"))-strtotime($team->date))/3600;
        //if($hours>=3){//Removed for troubleshooting purposes only
            BOINCTeam($_GET['t']);
        //}//Removed for troubleshooting purposes only
        $FAHBOINC="BOINC User:";
        $pointRank=" Ranks Risen:";
    }else{
        //Error
    }
    isset($_GET['b'])&&file_exists("images/sigimages/".$_GET['b'].".png")?$template=imagecreatefrompng("images/sigimages/".$_GET['b'].'.png'):$template=imagecreatefrompng('images/sigimages/luna1.png');
    strlen($user->userName)>14?$username=substr($user->userName,0,14)."...":$username=$user->userName;
    strlen($team->teamName)>13?$teamname=substr($team->teamName,0,13)."...":$teamname=$team->teamName;
    $white=imagecolorallocate($template,255,255,255);
    $yellow=imagecolorallocate($template,255,255,0);
    $font='images/fonts/UbuntuMono-R.ttf';
    imagettftext($template,10,0,28,25,$white,$font,$FAHBOINC);
    imagettftext($template,10,0,105,25,$yellow,$font,$username);
    imagettftext($template,10,0,15,37,$white,$font,'Rank on Team:');
    imagettftext($template,10,0,105,37,$yellow,$font,$user->userTeamRank);
    imagettftext($template,10,0,15,49,$white,$font,'Overall Rank:');
    imagettftext($template,10,0,105,49,$yellow,$font,$user->userOverallRank);
    imagettftext($template,10,0,22,61,$white,$font,'User Points:');
    imagettftext($template,10,0,105,61,$yellow,$font,$user->userPoints);
    imagettftext($template,10,0,42,73,$white,$font,'User PPD:');
    imagettftext($template,10,0,105,73,$yellow,$font,$user->userPPD);
    imagettftext($template,10,0,219,25,$white,$font,'Team Name:');
    imagettftext($template,10,0,289,25,$yellow,$font,$teamname);
    imagettftext($template,10,0,199,37,$white,$font,'Rank of Team:');
    imagettftext($template,10,0,289,37,$yellow,$font,$team->teamRank);
    imagettftext($template,10,0,206,49,$white,$font,'Team Points:');
    imagettftext($template,10,0,289,49,$yellow,$font,$team->teamPoints);
    imagettftext($template,10,0,226,61,$white,$font,'Team PPD:');
    imagettftext($template,10,0,289,61,$yellow,$font,$team->teamPPD);
    imagettftext($template,10,0,199,73,$white,$font,$pointRank);
    imagettftext($template,10,0,289,73,$yellow,$font,$team->teamToday);
    //imagepng($template);//Removed for troubleshooting purposes only
?>

Here's a pastebin to make finding lines easier.此外,为排除故障而显示为已删除的任何行都应视为正常的未注释行。我只是让他们注意到让故障排除更容易一些。

现在,它的工作方式是有三个名为utw的$ _GET变量。如果设置了前两个变量,则第一个if语句通过。如果设置了第二个和第三个变量,则第二个if语句通过。在这两种情况下,解析器只是获取数据,并将其存储在.json文件中,因此它看起来像这样(第一个数字是用户ID):

{"601869":{"userName":"hiigaran","userTeamRank":"33","userOverallRank":"6,923","userPoints":"8,297,436","userPPD":"10,656","date":"2014-04-19 20:53:00"}}

现在首先,我收到很多警告:

Warning: DOMDocument::loadHTML(): Unexpected end tag : b in Entity, line: 280 in D:\xampp\htdocs\sigimage.php on line 8

但是,我认为我可以解雇它们,因为它们不是我的问题,也不是解析或保存数据的障碍。但是,我还有很多其他问题。在第一次使用时,我有空的.json文件,这些是我通过第一个if语句时得到的消息:

Warning: fread(): Length parameter must be greater than 0 in D:\xampp\htdocs\sigimage.php on line 99
Warning: fread(): Length parameter must be greater than 0 in D:\xampp\htdocs\sigimage.php on line 18
Warning: Creating default object from empty value in D:\xampp\htdocs\sigimage.php on line 20
Warning: fread(): Length parameter must be greater than 0 in D:\xampp\htdocs\sigimage.php on line 18
Warning: Creating default object from empty value in D:\xampp\htdocs\sigimage.php on line 20
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 107
Warning: fread(): Length parameter must be greater than 0 in D:\xampp\htdocs\sigimage.php on line 109
Warning: fread(): Length parameter must be greater than 0 in D:\xampp\htdocs\sigimage.php on line 40
Warning: Creating default object from empty value in D:\xampp\htdocs\sigimage.php on line 42
Warning: fread(): Length parameter must be greater than 0 in D:\xampp\htdocs\sigimage.php on line 40
Warning: Creating default object from empty value in D:\xampp\htdocs\sigimage.php on line 42
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 145
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 145
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 153
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 155
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 157
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 159

或者如果它通过第二个if语句:

Warning: fread(): Length parameter must be greater than 0 in D:\xampp\htdocs\sigimage.php on line 121
Warning: fread(): Length parameter must be greater than 0 in D:\xampp\htdocs\sigimage.php on line 64
Warning: Creating default object from empty value in D:\xampp\htdocs\sigimage.php on line 66
Warning: fread(): Length parameter must be greater than 0 in D:\xampp\htdocs\sigimage.php on line 64
Warning: Creating default object from empty value in D:\xampp\htdocs\sigimage.php on line 66
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 129
Warning: fread(): Length parameter must be greater than 0 in D:\xampp\htdocs\sigimage.php on line 131
Warning: fread(): Length parameter must be greater than 0 in D:\xampp\htdocs\sigimage.php on line 88
Warning: Creating default object from empty value in D:\xampp\htdocs\sigimage.php on line 90
Warning: fread(): Length parameter must be greater than 0 in D:\xampp\htdocs\sigimage.php on line 88
Warning: Creating default object from empty value in D:\xampp\htdocs\sigimage.php on line 90
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 145
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 145
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 153
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 155
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 157
Notice: Trying to get property of non-object in D:\xampp\htdocs\sigimage.php on line 159

如果.json文件中已有数据,我只有DOMDocument警告。但是,图像仍然无法显示,它只是向我提供有关如何因错误而无法显示的信息。

这可能听起来有很多问题,但我已经到了这么远,感觉就像这么简单,我在这里失踪了。

让我感到困惑的是,我的第一个版本运行得很好。我正在研究的第二个版本是为了扩展功能以显示第二件事的统计数据,由用户决定,所以我在这里不知所措。如果有帮助,this is the code for the first version

2 个答案:

答案 0 :(得分:1)

一些代码建议:

  1. 你使用一个非常奇怪的习语:fread(fopen("scripts/boincusers.json","r"),filesize("scripts/boincusers.json"));。为什么不只是file_get_contents('scripts/boincusers.json');?这将解决您的许多问题。 file_put_contents也是你的朋友。
  2. 总是使用这样的绝对路径是个好主意:file_get_contents(__DIR__.'/scripts/boincusers.json');(同样适用于字体路径,包含路径......)
  3. 不要使用结束?>。只是不要。有时它会导致奇怪的错误,因为它后面有一些不可见的符号。
  4. 否则,您的代码不会检查错误情况并且天真地继续进行。您应该编码,就像您希望它在每个输入点都失败一样(GET参数可能是错误的,文件内容可能是错误的格式,......)。检查错误情况并正常停止。

    我很难修复您的代码,抱歉。但这些提示应该有所帮助。

答案 1 :(得分:0)

事实证明解决方案很简单,问题的原因正是我忽略的。即使它解析得很好,并按预期将数据保存到.json文件中,正在解析的页面上的错误以及随后的DOMDocument警告也是如果stat文件要么没有重新加载就不会出现图像的原因空的,或过期的。解决方案是在标题后面添加一行:

libxml_use_internal_errors(true);

就是这样!