Liaison de Discord avec les includes JSON PHP sur son site.

Fichier PHP :

Il suffit simplement d’ajouter le code suivant dans votre page, bien entendu cette dernière doit être en entête de page, cet exemple est propulsé avec Bootstrap 4 & PHP 7.2, n’oublier pas de changer la ligne ‘YOURLINK’ par votre lien API JSON Discord dans les paramètres de votre serveur, ainsi que d’activer votre widget Discord.

Mon code permet :

<?php 
 
// Votre API ID 
$jsonIn = file_get_contents('YOURLINK'); 
$JSON = json_decode($jsonIn, true); 
 
// Quelques exemples de variables 
$membersCount = count($JSON['members']); 
$nomduserveur = ($JSON['name']); 
$invitation = ($JSON['instant_invite']); 
 
?> 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../../../favicon.ico">

    <title>Starter Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <!-- Custom styles for this template -->
    <style type="text/css">
        body {
            padding-top: 5rem;
        }

        .starter-template {
            padding: 3rem 1.5rem;
            text-align: center;
        }
    </style>
</head>

    
<body>


    <main role="main" class="container">

      <div class="starter-template">
        <h1>Bootstrap + API DISCORD PHP</h1>
        <p class="lead">Document démo du tutoriel PHP de l'intégration Discord API.</p>
        <p class="lead"><?php echo $membersCount ;?> Utilisateurs en ligne</p>
        <p class="lead">Nom du Serveur : <?php echo $nomduserveur ;?></p>
        <p class="lead"><a href="<?php echo $invitation ;?>">Rejoindre</a></p>

      </div>

    </main><!-- /.container -->


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

</body>
</html>

<?php
              
//echo '<pre>' . var_export($JSON, true) . '</pre>';// Pour afficher les valeurs
 
$members = $JSON['members'];
foreach ($members as $member){
  
    $member_status = $member["status"];
    $current_game = $member['game']['name'];
  ?> 
           
  
       
        <div class="media text-muted pt-3 discord-usercase status-<?php echo $member_status?>" >
          <img alt="32x32" class="mr-2 rounded" src="<?php echo $member['avatar_url']; ?>" data-holder-rendered="true" style="width: 80px; height: 80px;">
          <div class="media-body p-2 mb-0 small lh-125  border-gray">
            <div class="d-flex justify-content-between align-items-center w-100 ">
              <strong class="user-display MB-white"><?php echo $member["username"] . ""; ?></strong>
                
                <div class="status-<?php echo $member_status?>"><?php echo get_discord_member_french_status($member_status); ?></div>
            </div>
            <span class="d-block"> #<?php echo $member['discriminator']; ?></span>
                <?php if(!empty($current_game)):?>
                    <span class="d-block"> <i class="fal fa-gamepad"></i> Joue  a <span class="MB-cryo"><?php echo $current_game; ?></span></span>
                <?php endif; ?>
   
          </div>
        </div>
          
<?php
}       
function get_discord_member_french_status($member_status){
  $status_list = array(
    "online" => "En ligne",
    "idle" => "Absent",
    "dnd" => "Occupé"
  );
   
  if(isset($status_list[$member_status])){
    return $status_list[$member_status];
  }
 
}
 
?>

  • Twitter
  • Linkedin
  • Béhance
  • Discord