Last active
November 17, 2017 00:16
-
-
Save camaleaun/6d38579a67e5be3bbbabd055fd74a9a3 to your computer and use it in GitHub Desktop.
Interface HTML by PHP to show info from endpoint JSON API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require('Requirements.php'); | |
$req = new \GilbertoTavares\Requirements(); | |
$req->apiCall(); | |
$req->convertJson(); | |
$req->outputData(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* Interface to JSON endpoint. | |
*/ | |
namespace GilbertoTavares; | |
/** | |
* Class to GET JSON and print. | |
*/ | |
class Requirements | |
{ | |
/** Endpoint URL */ | |
const URL = 'https://api.wp-healthcheck.com/v1/requirements'; | |
/** Store JSON object **/ | |
private $json; | |
/** Parameters **/ | |
private $params; | |
/** | |
* Get content json of endpoint | |
* | |
* @return null | |
**/ | |
public function apiCall() | |
{ | |
if ($this->httpCode() === 200) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_URL, self::URL); | |
$this->json = curl_exec($ch); | |
curl_close($ch); | |
} | |
} | |
private function httpCode() | |
{ | |
$ch = curl_init(self::URL); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
curl_setopt($ch, CURLOPT_NOBODY, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 10); | |
$output = curl_exec($ch); | |
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
return $httpcode; | |
} | |
public function convertJson() | |
{ | |
if (!empty($this->json)) | |
{ | |
$this->params = json_decode($this->json); | |
} | |
} | |
public function outputData() | |
{ | |
if (empty($this->params)) | |
{ | |
echo 'Endpoint unavailable'; | |
return; | |
} | |
$minrec = ' (minimum: %s, recommended: %s)'; | |
$output = "MySQL$minrec\nPHP$minrec\n"; | |
printf( | |
$output, | |
$this->params->mysql->minimum, | |
$this->params->mysql->recommended, | |
$this->params->php->minimum, | |
$this->params->php->recommended | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment