Question

I'm so close to getting this parser/image working, but I have a few final issues that stop me from releasing it. To make matters worse, I have absolutely no idea what the issue could be.

The following is the entire code:

<?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. Also, any line shown as removed for troubleshooting purposes should be treated as normal, uncommented lines. I just had them commented out to make troubleshooting a little easier.

Now, the way this works is that there are three $_GET variables called u, t, and w. If the first two variables are set, the first if statement passes. If the second and third variables are set, the second if statement passes. In both cases, the parser simply takes the data, and stores it in the .json files, so it looks something like this (where the first number is the user ID):

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

Now first off, I'm getting a lot of warnings like so:

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

However, I think I can dismiss them, since they aren't problems on my end, and are not a hindrance to parsing or saving data. However, I have a heap of other issues. Upon first use, where I have empty .json files, these are the messages I get when it passes the first if statement:

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

Or if it passes the second if statement:

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

If there is already data in the .json files, I just have DOMDocument warnings. However, the image still doesn't display, and it just gives me the message about how it can't display due to errors.

This might sound like a lot to ask about, but I've come so far, and it feels like something so simple that I'm missing here.

What makes things more confusing for me is that my first version works just fine. This second version I'm working on is meant to expand the functionality to show statistics for a second thing, as determined by the user, so I'm at a loss here. If it helps, this is the code for the first version.

Was it helpful?

Solution 2

Turns out the solution was simple, and the cause of the problem was exactly what I was ignoring. Even though it parses just fine, and saves the data into the .json files as expected, the errors on the page being parsed and the subsequent DOMDocument warnings were the reason why the image wouldn't appear without a reload if the stat files were either empty, or expired. The solution was to add a line right after the header:

libxml_use_internal_errors(true);

That was it!

OTHER TIPS

Some code advices:

  1. You use a very strange idiom: fread(fopen("scripts/boincusers.json","r"),filesize("scripts/boincusers.json"));. Why not just file_get_contents('scripts/boincusers.json'); ? This will solve a lot of your issues. file_put_contents is your friend too.
  2. It is a good idea to always use absolute paths like this: file_get_contents(__DIR__.'/scripts/boincusers.json'); (same applies to font paths, include paths…)
  3. do not use the closing ?>. just don't. sometimes it leads to weird errors because of some invisible symbols after it.

Otherwise, your code just doesn't check for error-cases and naively proceeds. You should code like you expect it to fail on every point of input (GET-parameter can be erroneous, file-contents can be in the wrong format, …). Check for error conditions and stop gracefully.

I won't be fixing your code, sorry. But these hints should help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top