Skip to content

Instantly share code, notes, and snippets.

@FabianPastor
Created October 23, 2016 10:31
Show Gist options
  • Save FabianPastor/57dc12c7da53afabac01bd8b14914ed9 to your computer and use it in GitHub Desktop.
Save FabianPastor/57dc12c7da53afabac01bd8b14914ed9 to your computer and use it in GitHub Desktop.
<?php
namespace GitHub\Status;
class API{
//This URLS Can change in any time.
protected $urls=[
"status.github.com",
"octostatus-production.github.com",
"octostatus-9676240.us-east-1.elb.amazonaws.com",
"54.243.208.39",
"107.22.212.99",
"107.20.156.144",
"23.21.94.152",
"status.github.com"
];
protected $url_p=0;
//See methods at status.github.com
protected $methods=[
"api" => "api",
"status" => "api/status",
"messages" => "api/messages",
"last_message" => "api/last-message",
];
public function __construct(){
//TODO: Test api.json
}
public function __get($name){
return $this->get($name);
}
public function get($method){
if(!isset($this->methods[$method]))
return json_decode('{"data":"","nerror":100,"error":"Method not found","json_nerror":false,"json_error":false}');
$method=$this->methods[$method];
$res = $this->get_data($method);
$res->json_nerror = false;
$res->json_error = false;
if($res->nerror==0){
$res->data = json_decode($res->data);
$res->json_nerror = json_last_error();
$res->json_error = json_last_error_msg();
}
return $res;
}
public function reset(){
$url_p=0;
}
protected function get_data($method,$opts=false){
$options = array(
CURLOPT_RETURNTRANSFER => true
,CURLOPT_FOLLOWLOCATION => true
,CURLOPT_HEADER => false
,CURLOPT_TIMEOUT => 1
,CURLOPT_SSL_VERIFYHOST => 0
,CURLOPT_SSL_VERIFYPEER => false
);
if(is_array($opts)){
$options = $opts + $options;
}
do{
$url=$this->urls[$this->url_p];
$options[CURLOPT_URL] = $url."/".$method.".json";
$res = $this->curl($options);
//var_dump($res);
if($res->nerror==0) return $res;
$this->ulr_p++;
}while($this->url_p < count($this->urls));
return $res;
}
protected function curl($opts){
$ch = curl_init();
//print_r($opts);
curl_setopt_array($ch, $opts);
$res = new \stdClass;
$res->data = curl_exec($ch);
$res->nerror = curl_errno($ch);
$res->error = curl_error($ch);
curl_close($ch);
return $res;
}
}
/* How to use:
require_once("githubstatusapi.class.php");
$github = new GitHub\Status\API();
var_dump($github->status->data);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment