2014-04-19 8 views
0

파서/이미지가 작동하는 데 너무 가깝지만 몇 가지 최종 문제로 인해 출시되지 않습니다. 설상가상으로 나는 그 문제가 무엇인지 전혀 모른다.동적 이미지에 대한 PHP/JSON 구문 분석기가 올바르게 작동하지 않습니다.

<?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=" [email protected] 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. 또, 문제 해결을 위해 제거 된 것으로 도시 어떤 라인이 정상 주석 라인으로 처리되어야한다

다음은 전체 코드이다. 문제 해결을 좀 더 쉽게하기 위해 주석을 달았습니다.

이제이 작동 방식은 u, tw이라는 3 개의 $ _GET 변수가 있다는 것입니다. 처음 두 변수가 설정되면 첫 번째 if 문이 전달됩니다. 두 번째 변수와 세 번째 변수가 설정되면 두 번째 if 문이 전달됩니다. 난, 우선 지금

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

: 두 경우 모두, 파서는 단순히 .json 파일의 데이터 저장을합니다, 그래서 그것은 (첫 번째 숫자는 사용자의 ID가) 다음과 같이 보입니다 과 같이 경고를 많이 받고 m :

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

그러나 나는 그들이 내 말에 문제가 아니기 때문에 내가 그들을 해고 할 수 있다고 생각하고, 분석 또는 데이터 저장에 방해가되지 않습니다. 그러나 나는 다른 문제가있다.

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 파일이 처음으로 사용하면, 이러한 메시지는 if 문 첫 번째 통과 할 때 내가 얻을 수 있습니다 .json 파일에 이미 데이터가 있습니다. DOMDocument 경고 만 있습니다. 그러나 이미지는 여전히 표시되지 않으며 오류로 인해 표시되지 않는 메시지가 표시됩니다.

이것은 많이 묻는 것처럼 들릴지 모르지만, 나는 지금까지 왔으며 여기에서 빠져있는 것처럼 아주 단순한 느낌을줍니다.

나를 혼란스럽게 만드는 것은 제 첫 번째 버전이 정상적으로 작동한다는 것입니다. 이 두 번째 버전은 사용자가 결정한 두 번째 통계를 표시하는 기능을 확장하기위한 것이므로 여기를 잃어 버렸습니다. 도움이된다면 this is the code for the first version.

+0

와우 - "나를 읽지 마십시오"라고 외치는 질문입니다. 붙여 넣은 코드가 모두 필요한지 고려하십시오. –

+0

무언가에 대해 무의미한 의견을 말하면 어떤 점이 있는지 생각해보십시오. 이미 해결 됐어. 진지하게, 당신이 질문을 읽으면, 나는 당신이 어떤 부분이 문제가있는 부분인지 구체적으로 말하지 않았 음을 알게 될 것이다. 내가 뭘해야 했어? 코드를 게시하지 않으시겠습니까? – Hiigaran

답변

0

솔루션은 간단하고 문제의 원인은 정확히 내가 무시한 것입니다. 그냥 잘 파싱하고 예상대로 .json 파일에 데이터를 저장하더라도 페이지가 분석되는 오류와 후속 DOMDocument 경고가 통계 파일이 둘 중 하나 인 경우 이미지가 다시로드되지 않고 표시되지 않는 이유입니다 비어 있거나 만료되었습니다. 해결책은 헤더 바로 뒤에 줄을 추가하는 것이 었습니다.

libxml_use_internal_errors(true); 

그게 전부입니다!

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 매개 변수는 오류가 있거나 파일 내용이 잘못된 형식 일 수 있습니다 ...). 오류 조건을 확인하고 정상적으로 중지하십시오.

코드를 수정하지 않습니다. 죄송합니다. 그러나 이러한 힌트는 도움이 될 것입니다.

+0

알았어, 나는 너의 제안을 구현 했어. 이제 상황이 좀 나아질 것 같아. 거의 완벽하게 작동합니다. 데이터 파일이 비어 있으면 이미지가 표시되지 않고 오류 메시지로 인해 표시 할 수 없음을 표시하지만 이미지를 다시로드하면 이미지가 올바른 데이터와 함께 올바르게 표시됩니다. 여기에 코드가 있습니다 (이 버전에서?>를 삭제하는 것을 잊었습니다) : http://pastebin.com/qjj1HWN3 어떤 아이디어가 문제를 일으킬 수 있습니까? 그렇지 않다면 초기 문제를 피하기 위해 다시로드하는 방법이 있습니까? – Hiigaran