0

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

以下是整个代码:

<?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
?>

这是一个让查找线更容易的pastebin。此外,出于故障排除目的而显示为已删除的任何行都应视为正常的未注释行。我只是将它们注释掉以使故障排除更容易一些。

现在,它的工作方式是有三个 $_GET 变量,分别称为utw。如果设置了前两个变量,则第一个 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 警告。但是,图像仍然没有显示,它只是给了我关于它如何由于错误而无法显示的消息。

这听起来可能有很多要问的问题,但我已经走了这么远,感觉就像我在这里想念的那样简单。

让我更困惑的是我的第一个版本运行良好。我正在开发的第二个版本旨在扩展功能以显示由用户确定的第二件事的统计信息,所以我在这里不知所措。如果有帮助,这是第一个版本的代码

4

2 回答 2

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. 不要使用关闭?>。只是不要。有时它会导致奇怪的错误,因为它后面有一些看不见的符号。

否则,您的代码不会检查错误情况并天真地继续。您应该像您期望它在每个输入点上都失败一样编写代码(GET 参数可能是错误的,文件内容可能是错误的格式,......)。检查错误情况并优雅地停止。

我不会修复你的代码,对不起。但这些提示应该会有所帮助。

于 2014-04-19T20:00:45.573 回答
0

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

libxml_use_internal_errors(true);

就是这样!

于 2014-04-20T23:04:47.190 回答