使用PHP API显示MySQL信息

时间:2015-01-03 04:49:03

标签: php mysql mysqli

我目前正在使用PHP API来显示来自MySQL数据库的信息,但它并不是那么顺利。我注意到,当您通过API(EX:https://epicmc.us/api.php?task=total)查看时,信息会显示,但它根本不显示在我的网页上。我确信API正在运行,所以我认为我在HTML格式中做错了什么想法?

这是我的API代码:

编辑:由于安全问题,API已被撤消。

以下是我将此信息转发给:

的页面
<!DOCTYPE html>
<head>
  <link rel="icon" 
    type="image/png" 
    href="https://minepic.org/update/<?php echo $_GET['player']; ?>">
  <link rel="apple-touch-icon" 
    type="image/png" 
    href="https://minepic.org/update/<?php echo $_GET['player']; ?>">
  <title><?php echo "EPICMC - "; if(empty($_GET['player'])){ echo "STATS";} else { echo $_GET['player']; }?></title>
  <meta name="apple-mobile-web-app-title" content="<?php echo ""; if(empty($_GET['player'])){ echo "STATS";} else { echo $_GET['player']; }?>">
  <meta name=viewport content="width=device-width, user-scalable=no">
  <link href="https://fonts.googleapis.com/css?family=Days+One&text=EPICM" rel="stylesheet">
  <link href='https://fonts.googleapis.com/css?family=Numans' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
  <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="https://epicmc.us/dist/css/ripples.min.css" rel="stylesheet">
  <link href="https://epicmc.us/dist/css/material-wfont.min.css" rel="stylesheet">
  <style>
    body { font-family: 'Numans', sans-serif; background: #eee; -webkit-font-smoothing: antialiased; }
    p {
    font-family: 'Numans', sans-serif;
    font-size: 13px;
    font-weight: lighter;
    text-align: center;
    display: inline;
    }
    h5 {
    font-family: 'Numans', sans-serif;
    font-size: 18px;
    font-weight: lighter;
    color: #636363;
    text-align: center;
    margin-bottom: 35px;
    text-transform: uppercase;
    margin-bottom: 50px;
    }
    h2 {
    font-family: 'Numans', sans-serif;
    font-size: 28px;
    font-weight: lighter;
    color: #636363;
    text-align: center;
    margin-bottom: 35px;
    text-transform: uppercase;
    }
    .logo { font-family: 'Days One', sans-serif; font-weight: lighter; font-size: 48px; margin-bottom: 35px; text-align: center; margin-top: 58px;}
    a:link,a:visited,a:hover,a:active { color: #636363; text-decoration: none; }
    #stats {
    width: 300px;
    margin: 4em auto;
    padding: 3em 2em 2em 2em;
    background: #fafafa;
    border: 1px solid #ebebeb;
    box-shadow: rgba(0,0,0,0.14902) 0px 1px 1px 0px,rgba(0,0,0,0.09804) 0px 1px 2px 0px;
    }
    .alert-info, .alert-danger, .alert-warning, .alert-success { margin-top: -20px; background-color: #323232; }
    td, th{
    width: 33.33%;
    border-style: solid;
    border-color: #323232;
    border-width: 1px;
    background-color: #f6f6f6;
    }
    .fa-stack {
    color: #00aa00;
    font-size: 11px;
    margin-left: 4px;
    margin-right: 4px;
    }
    .fa-exclamation-triangle {
    color: #aa0000;
    margin-left: 3px;
    margin-right: 3px;
    }
    .fa-inverse {
    text-shadow: 1.5px 1.5px #007600;
    font-size: 11px;
    }
    #registered {
      margin-top: -25px;
      margin-bottom: 30px;
    }
    .avatar {
      width: 80px;
      height: 80px;
      margin-bottom: 25px;
      pointer-events: none;
    }

    .media img+span.badge {
    position: relative;
    top: -8px;
    left: 8px;
}
  </style>
</head>
<body>
  <div class="navbar navbar-inverse">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-warning-collapse">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span> 
      </button>
      <a class="navbar-brand" href="https://epicmc.us/">EPICMC</a>
    </div>
    <div class="navbar-collapse collapse navbar-warning-collapse">
      <ul class="nav navbar-nav">
        <li><a href="https://epicmc.us/network.php">NETWORK</a></li>
        <li><a href="https://epicmc.us/plugins.php">PLUGINS</a></li>
        <li><a href="https://twitter.com/EPICMC_US" target="_blank">TWITTER</a></li>
      </ul>
      <form class="navbar-form navbar-left" action="stats.php" method="GET">
        <input type="text" name="player" class="form-control col-lg-8" placeholder="USERNAME">
      </form>
      </li>
      </ul>
    </div>
  </div>
  <center>
    <div class="bs-component">
    <div class="alert alert alert-danger">
      THIS IS CURRENTLY A BETA FEATURE. WE ARE CONSTANTLY IMPROVING UPON IT!
    </div>
  </center>
  <h1 class="logo"><a href="https://epicmc.us">EPIC<font color="#00aa00">MC</font></a></h1>
  <div id="stats">

  <?php
  date_default_timezone_set('Europe/Paris');
//echo '<center><img class="avatar" src="https://minepic.org/update/';
//echo $_GET['player'];
//echo '"></center>';
    $get_player = $_GET['player'];

    $remote_api = file_get_contents('https://epicmc.us/api.php?task=view&player='.$get_player.'');
    $remote_job = json_decode($remote_api);
    $remote_api1 = file_get_contents('https://epicmc.us/api.php?task=viewstats&player='.$get_player.'');
    $remote_job1 = json_decode($remote_api1);
    // $now = time(); // or your date as well`enter code here`


    if ($get_player){
   echo '<center><img class="avatar" src="https://minepic.org/update/';
echo $_GET['player'];
echo '"></center>';  

        echo '<h5>';
          echo $get_player; 
          if(substr($remote_job->verified,0,2) == 1){
             echo '<span title="VERIFIED PLAYER" class="fa fa-stack fa-lg"><i class="fa fa-certificate fa-stack-2x"></i><i class="fa fa-check fa-stack-1x fa-inverse"></i></span>'
    ;
        }
        echo '</h5>';
        echo '<div id="registered">';
        echo '<center><p>REGISTERED</p> ';
      echo '<p>';
        if(empty($remote_job)){ echo "--";} else { 

        //echo substr($remote_job->registered,0,50); 
$date1 = new DateTime('NOW');
$date2 = new DateTime($remote_job->registered);

$difference = $date1->diff($date2)->days;


//echo $difference;

if ($difference == 0){
if ($date1->diff($date2)->h == 0){
echo $date1->diff($date2)->i;
echo " MINUTES AGO";
} else {
echo $date11->diff($date2)->h;
echo " HOURS AGO";
}
}
if ($difference == 1){
echo $difference;
echo " DAY AGO";
}
if ($difference == 2){
echo $difference;
echo " DAYS AGO";
} 
if ($difference == 3){
echo $difference;
echo " DAYS AGO";
} 
if ($difference == 4){
echo $difference;
echo " DAYS AGO";
} 
if ($difference == 5){
echo $difference;
echo " DAYS AGO";
} 
if ($difference == 6){
echo $difference;
echo " DAYS AGO";
} 
if ($difference == 7){
//echo $difference;
echo "1 WEEK AGO";
}
if ($difference > 7 && $difference < 14){
echo $difference;
echo " DAYS AGO";
}
if ($difference == 14){
//echo $difference;
echo "2 WEEKS AGO";
}
if ($difference > 14){
echo $difference;
echo " DAYS AGO";
}


}
      echo '</p><br>';
    echo '<p>LAST SEEN</p> ';

    ?>

    <?php
        echo '<p>';
          if(empty($remote_job)){ echo "--";} else { 
          //echo substr($remote_job->lastlogin,0,50); 

$date11 = new DateTime('NOW');       
$date22 = new DateTime($remote_job->lastlogin);


$difference11 = $date11->diff($date22)->days;
$difference111 = $date11->diff($date22);

if ($difference11 == 0){
if ($date11->diff($date22)->h == 0){
echo $date11->diff($date22)->i;
echo " MINUTES AGO";
} else {
echo $date11->diff($date22)->h;
echo " HOURS AGO";
}
}

if ($difference11 == 1){
echo $difference11;
echo " DAY AGO";
}
if ($difference11 == 2){
echo $difference11;
echo " DAYS AGO";
} 
if ($difference11 == 3){
echo $difference11;
echo " DAYS AGO";
} 
if ($difference11 == 4){
echo $difference11;
echo " DAYS AGO";
} 
if ($difference11 == 5){
echo $difference11;
echo " DAYS AGO";
} 
if ($difference11 == 6){
echo $difference11;
echo " DAYS AGO";
} 
if ($difference11 == 7){
//echo $difference11;
echo "1 WEEK AGO";
}
if ($difference11 > 7 && $difference11 < 14){
echo $difference11;
echo " DAYS AGO";
}
if ($difference11 == 14){
//echo $difference11;
echo "2 WEEKS AGO";
}
//print_r ($difference111);

           }
           echo '</p></center>';
           echo '</div>';
    ?>
  <div id="tables">
  <table style="width: 100%; text-align: center;">
    <tr>
      <td>
        <p>KILLS</p>
      </td>
      <td>
        <p>DEATHS</p>
      </td>
      <td>
        <p>RATIO</p>
      </td>
    </tr>
    <tr>
      <td><?php if(empty($remote_job1)){ echo "--";} else { echo substr($remote_job1->kills,0,50); } ?></td>
      <td><?php if(empty($remote_job1)){ echo "--";} else { echo substr($remote_job1->deaths,0,50); } ?></td>
      <td><?php if(empty($remote_job1)){ echo "--";} else { echo substr($remote_job1->ratio,0,50); } ?></td>
    </tr>
  </table>
  <br>
  <table style="width: 100%; text-align: center;">
    <tr>
      <td>
        <p>JOINS</p>
      </td>
      <td>
        <p>QUITS</p>
      </td>
      <td>
        <p>KICKED</p>
    </tr>
    <tr>
      <td><?php if(empty($remote_job1)){ echo "--";} else { echo substr($remote_job1->joins,0,50); } ?></td>
      <td><?php if(empty($remote_job1)){ echo "--";} else { echo substr($remote_job1->quits,0,50); } ?></td>
      <td><?php if(empty($remote_job1)){ echo "--";} else { echo substr($remote_job1->kicked,0,50); } ?></td>
    </tr>
  </table>
  <br>
  <table style="width: 100%; text-align: center;">
    <tr>
      <td>
        <p>PLACES</p>
      </td>
      <td>
        <p>BREAKS</p>
      </td>
    </tr>
    <tr>
      <td><?php if(empty($remote_job1)){ echo "--";} else { echo substr($remote_job1->places,0,50); } ?></td>
      <td><?php if(empty($remote_job1)){ echo "--";} else { echo substr($remote_job1->breaks,0,50); } ?></td>
    </tr>
  </table>
  <br>
  <?php   
           if($remote_job->verified == 1){
           echo '';
           } else { 
           echo '<center><small><i class="fa fa-exclamation-triangle"></i><a href="mailto:report@epicmc.us?subject=Impersonation Report Username: ';
           echo $get_player;
           echo '&body=Please be as detailed as possible! Username: ';
           echo $get_player;
           echo '">REPORT IMPERSONATION</a></small></center>';
           }

    } 



    else {
    echo '<h5>PLEASE ENTER A USERNAME SO WE CAN DISPLAY THE STATS!</h5>';



    }
    ?>
  <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
  <script src="https://epicmc.us/dist/js/textbox.js"></script>
  <script src="https://epicmc.us/dist/js/ripples.min.js"></script>
  <script src="https://epicmc.us/dist/js/material.min.js"></script>
  <script>
    $(document).ready(function() {
        $.material.init();
    });
  </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

清理缩进和日期逻辑,以及更改某些变量名称以使其更具描述性。没有您的页面输出,它有点难以调试,但也许某些东西是固定的。我还修正了一个未定义的变量拼写错误,这可能导致了你的问题。

<?php

// error_reporting(E_ALL);
?>   

<!DOCTYPE html>
<head>
  <link rel="icon" 
    type="image/png" 
    href="https://minepic.org/update/<?php echo $_GET['player']; ?>">
  <link rel="apple-touch-icon" 
    type="image/png" 
    href="https://minepic.org/update/<?php echo $_GET['player']; ?>">
  <title><?php echo "EPICMC - "; if(empty($_GET['player'])){ echo "STATS";} else { echo $_GET['player']; }?></title>
  <meta name="apple-mobile-web-app-title" content="<?php echo ""; if(empty($_GET['player'])){ echo "STATS";} else { echo $_GET['player']; }?>">
  <meta name=viewport content="width=device-width, user-scalable=no">
  <link href="https://fonts.googleapis.com/css?family=Days+One&text=EPICM" rel="stylesheet">
  <link href='https://fonts.googleapis.com/css?family=Numans' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
  <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="https://epicmc.us/dist/css/ripples.min.css" rel="stylesheet">
  <link href="https://epicmc.us/dist/css/material-wfont.min.css" rel="stylesheet">
  <style>
    body {
      font-family: 'Numans', sans-serif;
      background: #eee;
      -webkit-font-smoothing: antialiased;
    }
    p {
      font-family: 'Numans', sans-serif;
      font-size: 13px;
      font-weight: lighter;
      text-align: center;
      display: inline;
    }
    h5 {
      font-family: 'Numans', sans-serif;
      font-size: 18px;
      font-weight: lighter;
      color: #636363;
      text-align: center;
      margin-bottom: 35px;
      text-transform: uppercase;
      margin-bottom: 50px;
    }
    h2 {
      font-family: 'Numans', sans-serif;
      font-size: 28px;
      font-weight: lighter;
      color: #636363;
      text-align: center;
      margin-bottom: 35px;
      text-transform: uppercase;
    }
    .logo {
      font-family: 'Days One', sans-serif;
      font-weight: lighter;
      font-size: 48px;
      margin-bottom: 35px;
      text-align: center;
      margin-top: 58px;
    }
    a:link,a:visited,a:hover,a:active { 
      color: #636363;
      text-decoration: none;
    }
    #stats {
      width: 300px;
      margin: 4em auto;
      padding: 3em 2em 2em 2em;
      background: #fafafa;
      border: 1px solid #ebebeb;
      box-shadow: rgba(0,0,0,0.14902) 0px 1px 1px 0px,rgba(0,0,0,0.09804) 0px 1px 2px 0px;
    }
    .alert-info, .alert-danger, .alert-warning, .alert-success { 
      margin-top: -20px; background-color: #323232;
    }
    td, th {
      width: 33.33%;
      border-style: solid;
      border-color: #323232;
      border-width: 1px;
      background-color: #f6f6f6;
    }
    .fa-stack {
      color: #00aa00;
      font-size: 11px;
      margin-left: 4px;
      margin-right: 4px;
    }
    .fa-exclamation-triangle {
      color: #aa0000;
      margin-left: 3px;
      margin-right: 3px;
    }
    .fa-inverse {
      text-shadow: 1.5px 1.5px #007600;
      font-size: 11px;
    }
    #registered {
      margin-top: -25px;
      margin-bottom: 30px;
    }
    .avatar {
      width: 80px;
      height: 80px;
      margin-bottom: 25px;
      pointer-events: none;
    }

    .media img+span.badge {
      position: relative;
      top: -8px;
      left: 8px;
    }
  </style>
</head>
<body>
  <div class="navbar navbar-inverse">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-warning-collapse">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span> 
      </button>
      <a class="navbar-brand" href="https://epicmc.us/">EPICMC</a>
    </div>
    <div class="navbar-collapse collapse navbar-warning-collapse">
      <ul class="nav navbar-nav">
        <li><a href="https://epicmc.us/network.php">NETWORK</a></li>
        <li><a href="https://epicmc.us/plugins.php">PLUGINS</a></li>
        <li><a href="https://twitter.com/EPICMC_US" target="_blank">TWITTER</a></li>
      </ul>
      <form class="navbar-form navbar-left" action="stats.php" method="GET">
        <input type="text" name="player" class="form-control col-lg-8" placeholder="USERNAME">
      </form>
      </li>
      </ul>
    </div>
  </div>
  <center>
    <div class="bs-component">
    <div class="alert alert alert-danger">
      THIS IS CURRENTLY A BETA FEATURE. WE ARE CONSTANTLY IMPROVING UPON IT!
    </div>
    </div>
  </center>
  <h1 class="logo"><a href="https://epicmc.us">EPIC<font color="#00aa00">MC</font></a></h1>
  <div id="stats">

  <?php
  function mcus_timeAgoString($dateInterval) {
    $daysAgo = $dateInterval->days;
    //echo $difference;
    if ($daysAgo == 0){
      if ($dateInterval->h == 0){
        $diffStr = $dateInterval->i . " MINUTES AGO";
      } else {
         $diffStr = $dateInterval->h . " HOURS AGO";
      }
    } else {
      if($daysAgo === 1) {
        $diffStr = "1 DAY AGO";
      } else if($daysAgo === 7) {
        $diffStr = "1 WEEK AGO";
      } else if($daysAgo === 14) {
        $diffStr = "2 WEEKS AGO";
      } else {
        $diffStr = "$daysAgo DAYS AGO";
      }
    }
    return $diffStr;
  }

  date_default_timezone_set('Europe/Paris');
//echo '<center><img class="avatar" src="https://minepic.org/update/';
//echo $_GET['player'];
//echo '"></center>';
    $get_player = $_GET['player'];

    $playerAccountInfoJSON = file_get_contents('https://epicmc.us/api.php?task=view&player='.$get_player.'');
    // $playerAccountInfoJSON = '{"task":"view","registered":"12/23/2014","lastlogin":"01/03/2015 02:32 AM","verified":"1"}';
    $playerAccountInfo = json_decode($playerAccountInfoJSON);
    $playerStatsJSON = file_get_contents('https://epicmc.us/api.php?task=viewstats&player='.$get_player.'');
    // $playerStatsJSON = '{"task":"viewstats","kills":"1","deaths":"1","ratio":"1","joins":"22","quits":"28","kicked":"5"} ';
    $playerStats = json_decode($playerStatsJSON);

//         var_dump($playerAccountInfo);
//         var_dump($playerStats);
    // $now = time(); // or your date as well`enter code here`


    if ($get_player) {
      echo '<center><img class="avatar" src="https://minepic.org/update/';
      echo $_GET['player'];
      echo '"></center>';  

      echo '<h5>';
      echo $get_player; 
      if(!empty($playerAccountInfo->verified) && substr($playerAccountInfo->verified,0,2) == 1) {
        echo '<span title="VERIFIED PLAYER" class="fa fa-stack fa-lg">'
          . '<i class="fa fa-certificate fa-stack-2x"></i>'
          . '<i class="fa fa-check fa-stack-1x fa-inverse"></i>'
          . '</span>'
        ;
      }
      echo '</h5>';
      echo '<div id="registered">';
      echo '<center><p>REGISTERED</p> ';
      echo '<p>';

      if(empty($playerAccountInfo)) { 
        echo "--";
      } else { 
        //echo substr($playerAccountInfo->registered,0,50); 
        $dtNow = new DateTime('NOW');
        $dtReg = new DateTime($playerAccountInfo->registered);
        $diRegToNow = $dtNow->diff($dtReg);
        $daysSinceReg = $diRegToNow->days;
        //echo $difference;
        echo mcus_timeAgoString($diRegToNow);
      }
      echo '</p><br>';
      echo '<p>LAST SEEN</p> ';
      echo '<p>';
      if(empty($playerAccountInfo)){
        echo "--";
      } else { 
          //echo substr($playerAccountInfo->lastlogin,0,50); 

        $dtNow = new DateTime('NOW');       
        $dtLastLogin = new DateTime($playerAccountInfo->lastlogin);
        $diLastLoginToNow = $dtNow->diff($dtLastLogin);
        echo mcus_timeAgoString($diLastLoginToNow);
      }
      echo '</p></center>';
      echo '</div>';
  ?>
  <div id="tables">
  <table style="width: 100%; text-align: center;">
    <tr>
      <td>
        <p>KILLS</p>
      </td>
      <td>
        <p>DEATHS</p>
      </td>
      <td>
        <p>RATIO</p>
      </td>
    </tr>
    <tr>
      <td><?php echo empty($playerStats->kills) ? "--" : substr($playerStats->kills, 0, 50); ?></td>
      <td><?php echo empty($playerStats->deaths) ? "--" : substr($playerStats->deaths, 0, 50); ?></td>
      <td><?php echo empty($playerStats->ratio) ? "--" : substr($playerStats->ratio, 0, 50); ?></td>
    </tr>
  </table>
  <br>
  <table style="width: 100%; text-align: center;">
    <tr>
      <td>
        <p>JOINS</p>
      </td>
      <td>
        <p>QUITS</p>
      </td>
      <td>
        <p>KICKED</p>
    </tr>
    <tr>
      <td><?php echo empty($playerStats->joins) ? "--" : substr($playerStats->joins, 0, 50); ?></td>
      <td><?php echo empty($playerStats->quits) ? "--" : substr($playerStats->quits, 0, 50); ?></td>
      <td><?php echo empty($playerStats->kicked) ? "--" : substr($playerStats->kicked, 0, 50); ?></td>
    </tr>
  </table>
  <br>
  <table style="width: 100%; text-align: center;">
    <tr>
      <td>
        <p>PLACES</p>
      </td>
      <td>
        <p>BREAKS</p>
      </td>
    </tr>
    <tr>
      <td><?php echo empty($playerStats->places) ? "--" : substr($playerStats->places,0,50); ?></td>
      <td><?php echo empty($playerStats->breaks) ? "--" : substr($playerStats->breaks,0,50); ?></td>
    </tr>
  </table>
  <br>
  <?php   
  if(!empty($playerAccountInfo->verified) && $playerAccountInfo->verified == 1){
    echo '';
  } else { 
      echo '<center><small>'
        . '<i class="fa fa-exclamation-triangle"></i>'
        . '<a href="mailto:report@epicmc.us?subject=Impersonation Report Username: '
        . $get_player
        . '&body=Please be as detailed as possible! Username: '
        . $get_player
        . '">REPORT IMPERSONATION</a></small></center>'
      ;
    }

  } else {
    echo '<h5>PLEASE ENTER A USERNAME SO WE CAN DISPLAY THE STATS!</h5>';
  }
  ?>
  <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
  <script src="https://epicmc.us/dist/js/textbox.js"></script>
  <script src="https://epicmc.us/dist/js/ripples.min.js"></script>
  <script src="https://epicmc.us/dist/js/material.min.js"></script>
  <script>
    $(document).ready(function() {
        $.material.init();
    });
  </script>
</body>
</html>