我正忙于和朋友们一起玩《使命召唤:黑色行动4》。
现在我可以使用它了,但是我想合并代码,因为我为每个用户名重复了整个cURL信息。有人可以帮我清理我的代码:)
我在两次重复输入的代码之间插入
现在我有6个用户名,这意味着我每次cUrl请求和一行都重复一次。
我确信它可以比我更轻松地工作。
<?php
if(isset($_GET['user']) && $_GET['user'] != '') {
$url = 'https://cod-api.tracker.gg/v1/standard/bo4/profile/2/'.$_GET['user'];
}else{
$url = 'https://cod-api.tracker.gg/v1/standard/bo4/profile/2/gurdt';
}
<!--- REPEAT FOR EVERY USERNAME !--->
$userGurdt = 'https://cod-api.tracker.gg/v1/standard/bo4/profile/2/gurdt';
<!--- END REPEAT FOR EVERY USERNAME !--->
$userRay = 'https://cod-api.tracker.gg/v1/standard/bo4/profile/2/the_green_ray';
$userRamon = 'https://cod-api.tracker.gg/v1/standard/bo4/profile/2/hilhorst95';
$userMark = 'https://cod-api.tracker.gg/v1/standard/bo4/profile/2/r6dutchmennl';
$userLeon = 'https://cod-api.tracker.gg/v1/standard/bo4/profile/2/theaxeman777';
$userDan = 'https://cod-api.tracker.gg/v1/standard/bo4/profile/2/fscdan';
<!--- REPEAT FOR EVERY USERNAME !--->
// Gurdt
$chGurdt = curl_init($userGurdt);
curl_setopt($chGurdt, CURLOPT_USERPWD, 'TRN-Api-Key: XXXXXXX-XXXX-XXXXX-XXXX-XXXXXXXXXXXX');
curl_setopt($chGurdt, CURLOPT_RETURNTRANSFER, true);
$responseGurdt = curl_exec($chGurdt);
if(curl_errno($chGurdt)){
//If an error occured, throw an Exception.
throw new Exception(curl_error($chGurdt));
}
$outputGurdt = json_decode($responseGurdt);
$usernameGurdt = $outputGurdt->data->metadata->platformUserHandle;
$levelGurdt = $outputGurdt->data->stats[0]->value;
$levelImgGurdt = $outputGurdt->data->stats[0]->metadata->iconUrl;
$killsGurdt = $outputGurdt->data->stats[2]->value;
$deathsGurdt = $outputGurdt->data->stats[3]->value;
$kdGurdt = $outputGurdt->data->stats[1]->value;
<!--- END REPEAT FOR EVERY USERNAME !--->
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The_Green_Ray
$chRay = curl_init($userRay);
curl_setopt($chRay, CURLOPT_USERPWD, 'TRN-Api-Key: XXXXXXX-XXXX-XXXXX-XXXX-XXXXXXXXXXXX');
curl_setopt($chRay, CURLOPT_RETURNTRANSFER, true);
$responseRay = curl_exec($chRay);
if(curl_errno($chRay)){
//If an error occured, throw an Exception.
throw new Exception(curl_error($chRay));
}
$outputRay = json_decode($responseRay);
$usernameRay = $outputRay->data->metadata->platformUserHandle;
$levelRay = $outputRay->data->stats[0]->value;
$levelImgRay = $outputRay->data->stats[0]->metadata->iconUrl;
$killsRay = $outputRay->data->stats[2]->value;
$deathsRay = $outputRay->data->stats[3]->value;
$kdRay = $outputRay->data->stats[1]->value;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Ramon
$chRamon = curl_init($userRamon);
curl_setopt($chRamon, CURLOPT_USERPWD, 'TRN-Api-Key: XXXXXXX-XXXX-XXXXX-XXXX-XXXXXXXXXXXX');
curl_setopt($chRamon, CURLOPT_RETURNTRANSFER, true);
$responseRamon = curl_exec($chRamon);
if(curl_errno($chRamon)){
//If an error occured, throw an Exception.
throw new Exception(curl_error($chRamon));
}
$outputRamon = json_decode($responseRamon);
$usernameRamon = $outputRamon->data->metadata->platformUserHandle;
$levelRamon = $outputRamon->data->stats[0]->value;
$levelImgRamon = $outputRamon->data->stats[0]->metadata->iconUrl;
$killsRamon = $outputRamon->data->stats[2]->value;
$deathsRamon = $outputRamon->data->stats[3]->value;
$kdRamon = $outputRamon->data->stats[1]->value;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Mark
$chMark = curl_init($userMark);
curl_setopt($chMark, CURLOPT_USERPWD, 'TRN-Api-Key: XXXXXXX-XXXX-XXXXX-XXXX-XXXXXXXXXXXX');
curl_setopt($chMark, CURLOPT_RETURNTRANSFER, true);
$responseMark = curl_exec($chMark);
if(curl_errno($chMark)){
//If an error occured, throw an Exception.
throw new Exception(curl_error($chMark));
}
$outputMark = json_decode($responseMark);
$usernameMark = $outputMark->data->metadata->platformUserHandle;
$levelMark = $outputMark->data->stats[0]->value;
$levelImgMark = $outputMark->data->stats[0]->metadata->iconUrl;
$killsMark = $outputMark->data->stats[3]->value;
$deathsMark = $outputMark->data->stats[4]->value;
$kdMark = $outputMark->data->stats[2]->value;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Leon
$chLeon = curl_init($userLeon);
curl_setopt($chLeon, CURLOPT_USERPWD, 'TRN-Api-Key: XXXXXXX-XXXX-XXXXX-XXXX-XXXXXXXXXXXX');
curl_setopt($chLeon, CURLOPT_RETURNTRANSFER, true);
$responseLeon = curl_exec($chLeon);
if(curl_errno($chLeon)){
//If an error occured, throw an Exception.
throw new Exception(curl_error($chLeon));
}
$outputLeon = json_decode($responseLeon);
$usernameLeon = $outputLeon->data->metadata->platformUserHandle;
$levelLeon = $outputLeon->data->stats[0]->value;
$levelImgLeon = $outputLeon->data->stats[0]->metadata->iconUrl;
$killsLeon = $outputLeon->data->stats[3]->value;
$deathsLeon = $outputLeon->data->stats[4]->value;
$kdLeon = $outputLeon->data->stats[2]->value;
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////
// Danny
$chDan = curl_init($userDan);
curl_setopt($chDan, CURLOPT_USERPWD, 'TRN-Api-Key: XXXXXXX-XXXX-XXXXX-XXXX-XXXXXXXXXXXX');
curl_setopt($chDan, CURLOPT_RETURNTRANSFER, true);
$responseDan = curl_exec($chDan);
if(curl_errno($chDan)){
//If an error occured, throw an Exception.
throw new Exception(curl_error($chDan));
}
$outputDan = json_decode($responseDan);
$usernameDan = $outputDan->data->metadata->platformUserHandle;
$levelDan = $outputDan->data->stats[0]->value;
$levelImgDan = $outputDan->data->stats[0]->metadata->iconUrl;
$killsDan = $outputDan->data->stats[2]->value;
$deathsDan = $outputDan->data->stats[3]->value;
$kdDan = $outputDan->data->stats[1]->value;
///////////////////////////////////////////////////////////////////////////////
?>
<!doctype html>
<html lang="nl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.0/css/bootstrap.min.css" integrity="sha384-PDle/QlgIONtM1aqA2Qemk5gPOE7wFq8+Em+G/hmo5Iq0CCmYZLv3fVRDJ4MMwEA" crossorigin="anonymous">
<title>N033 Stats</title>
</head>
<body>
<div class="container">
<div class="col-sm-6 offset-sm-4">
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Level</th>
<th>Kills</th>
<th>Deaths</th>
<th>K/D</th>
</tr>
</thead>
<tbody>
<!--- REPEAT FOR EVERY USERNAME !--->
<tr>
<td valign="middle"><?php echo $usernameGurdt;?></td>
<td valign="middle"><?php echo '<img src="'.$levelImgGurdt.'" width="40" height="40"/> '.$levelGurdt;?></td>
<td valign="middle"><?php echo $killsGurdt;?></td>
<td valign="middle"><?php echo $deathsGurdt;?></td>
<td valign="middle"><?php echo $kdGurdt;?></td>
</tr>
<!--- END REPEAT FOR EVERY USERNAME !--->
<tr>
<td valign="middle"><?php echo $usernameRay;?></td>
<td valign="middle"><?php echo '<img src="'.$levelImgRay.'" width="40" height="40"/> '.$levelRay;?></td>
<td valign="middle"><?php echo $killsRay;?></td>
<td valign="middle"><?php echo $deathsRay;?></td>
<td valign="middle"><?php echo $kdRay;?></td>
</tr>
<tr>
<td valign="middle"><?php echo $usernameRamon;?></td>
<td valign="middle"><?php echo '<img src="'.$levelImgRamon.'" width="40" height="40"/> '.$levelRamon;?></td>
<td valign="middle"><?php echo $killsRamon;?></td>
<td valign="middle"><?php echo $deathsRamon;?></td>
<td valign="middle"><?php echo $kdRamon;?></td>
</tr>
<tr>
<td valign="middle"><?php echo $usernameMark;?></td>
<td valign="middle"><?php echo '<img src="'.$levelImgMark.'" width="40" height="40"/> '.$levelMark;?></td>
<td valign="middle"><?php echo $killsMark;?></td>
<td valign="middle"><?php echo $deathsMark;?></td>
<td valign="middle"><?php echo $kdMark;?></td>
</tr>
<tr>
<td valign="middle"><?php echo $usernameLeon;?></td>
<td valign="middle"><?php echo '<img src="'.$levelImgLeon.'" width="40" height="40"/> '.$levelLeon;?></td>
<td valign="middle"><?php echo $killsLeon;?></td>
<td valign="middle"><?php echo $deathsLeon;?></td>
<td valign="middle"><?php echo $kdLeon;?></td>
</tr>
<tr>
<td valign="middle"><?php echo $usernameDan;?></td>
<td valign="middle"><?php echo '<img src="'.$levelImgDan.'" width="40" height="40"/> '.$levelDan;?></td>
<td valign="middle"><?php echo $killsDan;?></td>
<td valign="middle"><?php echo $deathsDan;?></td>
<td valign="middle"><?php echo $kdDan;?></td>
</tr>
</tbody>
</table>
</div>
</div>
<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.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.0/js/bootstrap.min.js" integrity="sha384-7aThvCh9TypR7fIc2HV4O/nFMVCBwyIUKL8XCtKE+8xgCgl/PQGuFsvShjr74PBp" crossorigin="anonymous"></script>
</body>
</html>
答案 0 :(得分:2)
我通过对您的问题的评论中所述的内容,稍微重写了您的代码:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$baseUrl = 'https://cod-api.tracker.gg/v1/standard/bo4/profile/2/';
$apiKey = 'XXXXXXX-XXXX-XXXXX-XXXX-XXXXXXXXXXXX';
$users = [
'gurdt' => [
'id' => 'gurdt'
],
'ray' => [
'id' => 'the_green_ray'
],
'ramon' => [
'id' => 'hilhorst95'
],
'mark' => [
'id' => 'r6dutchmennl'
],
'leon' => [
'id' => 'theaxeman777'
],
'dan' => [
'id' => 'fscdan'
]
];
$result = [];
foreach ($users as $user) {
$url = $baseUrl . $user['id'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'TRN-Api-Key: ' . $apiKey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
//If an error occured, throw an Exception.
throw new Exception(curl_error($ch));
}
$output = json_decode($response, true);
$result[$user['id']]['username'] = $output['data']['metadata']['platformUserHandle'];
$result[$user['id']]['level'] = $output['data']['stats'][0]['value'];
$result[$user['id']]['levelImg'] = $output['data']['stats'][0]['metadata']['iconUrl'];
$result[$user['id']]['kd'] = $output['data']['stats'][1]['value'];
$result[$user['id']]['kills'] = $output['data']['stats'][2]['value'];
$result[$user['id']]['deaths'] = $output['data']['stats'][3]['value'];
}
var_dump($result);
?>
编辑:我也稍微整理了一下表示层,您不必在HTML的各处使用<?php echo ?>
,这有点难以理解。相反,您可以使用Heredoc之类的东西使其更具可读性:
<div class="container">
<div class="col-sm-6 offset-sm-4">
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Level</th>
<th>Kills</th>
<th>Deaths</th>
<th>K/D</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $r) {
$username = $r['username'];
$levelImg = $r['levelImg'];
$level = $r['level'];
$kills = $r['kills'];
$deaths = $r['deaths'];
$kd = $r['kd'];
echo <<<EOT
<tr>
<td valign="middle">$username</td>
<td valign="middle"><img src="$levelImg" width="40" height="40"/>$level</td>
<td valign="middle">$kills</td>
<td valign="middle">$deaths</td>
<td valign="middle">$kd</td>
</tr>
EOT;
} ?>
</tbody>
</table>
</div>
</div>