【发布时间】:2021-02-19 05:18:12
【问题描述】:
我正在尝试创建 api 来显示我的服务器状态
最近我创建了一个非常粗糙的api,你可能会笑太多,这是我所做的代码
$Status2 = "Failed";
$Status3 = "Failed";
$Status4 = "Failed";
$info1 = "Opps! looks like our Server One is Down. Please contact our support team for more information";
$info2 = "Opps! looks like our Server Two is Down. Please contact our support team for more information";
$info3 = "Opps! looks like our Server Three is Down. Please contact our support team for more information";
$info4 = "Opps! looks like our Server Four is Down. Please contact our support team for more information";
$serverOne = "http://firstserver.com";
$serverTwo = "http://2ndserver.com";
$serverThree = "http://3rdserver.com";
$serverFour = "http://4thserver.com";
// Takes raw data from the request
$jsonOne = file_get_contents($serverOne);
$jsonTwo = file_get_contents($serverTwo);
$jsonThree = file_get_contents($serverThree);
$jsonFour = file_get_contents($serverFour);
// Converts it into a PHP object
$serverOneBody = @json_decode($jsonOne, true);
$serverTwoBody = @json_decode($jsonTwo, true);
$serverThreeBody = @json_decode($jsonThree, true);
$serverFourBody = @json_decode($jsonFour, true);
$statusOne = $serverOneBody["status"];
$statusTwo = $serverTwoBody["status"];
$statusThree = $serverThreeBody["status"];
$statusFour = $serverFourBody["status"];
if($statusOne == 1){$Status1 = "Success"; $info1 = "Success! Server One is live and working!";}
if($statusTwo == 1){$Status2 = "Success"; $info2 = "Success! Server Two is live and working!";}
if($statusThree == 1){$Status3 = "Success"; $info3 = "Success! Server Three is live and working!";}
if($statusFour == 1){$Status4 = "Success"; $info4 = "Success! Server Three is live and working!";}
$respon = array( "serverOne" => array ('response' => 200,'status' => $Status1,'info' => $info1),
"serverTwo" => array ('response' => 200,'status' => $Status2,'info' => $info2),
"serverThree" => array ('response' => 200,'status' => $Status3,'info' => $info3),
"serverFour" => array ('response' => 200,'status' => $Status4,'info' => $info4)
);
$this->response($this->json($respon), 200);
我知道它非常原始。
我想收到一个json
{
"serverOne": {
"response": 200,
"status": "Success",
"info": "Success! Server One is live and working!"
},
"serverTwo": {
"response": 200,
"status": "Success",
"info": "Success! Server Two is live and working!"
},
"serverThree": {
"response": 200,
"status": "Success",
"info": "Success! Server Three is live and working!"
},
"serverFour": {
"response": 200,
"status": "Success",
"info": "Success! Server Four is live and working!"
}
}
现在我正在尝试
$items = array();
$servers = array(
"one" => "sv1.com",
"two" => "sv2.com",
"three" => "sv3.com"
);
foreach ($servers as $server) {
$json = file_get_contents($server);
// Converts it into a PHP object
$jsonBody = @json_decode($json, true);
$status = $jsonBody["status"];
if($status == 1){
array_push($items,"Success");
array_push($items,"Success! Server number is live and working!");
}else{
array_push($items,"Error");
array_push($items,"Error! Server number is Not working properly!");
}
}
$this->response($this->json($items), 200);
但它只是循环它没有显示服务器一二和三。
我怎样才能得到上面给定的 json 格式的可能输出?
请帮助我,我在互联网上到处搜索,但没有找到答案。
使用我收到的当前代码
[
"Success",
"Success! Server number is live and working!",
"Success",
"Success! Server number is live and working!",
"Error",
"Error! Server number is Not working properly!",
"Success",
"Success! Server number is live and working!"
]
【问题讨论】:
-
还是没有人帮忙?
标签: php json phpmyadmin