as-stats-gui/lib/class/peeringdb.php

100 lines
2.4 KiB
PHP
Raw Normal View History

<?php
if ( file_exists('../../config.inc') ) {
require_once '../../config.inc';
} elseif ( file_exists('config.inc') ) {
require_once 'config.inc';
}
class PeeringDB {
protected $url = NULL;
public function __construct() {
global $peeringdb;
2022-05-28 14:55:25 +02:00
$this->url = 'https://www.peeringdb.com/api';
}
protected function sendRequest( $url ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
public function GetInfo( $asn = NULL ) {
if ( $asn ) {
$json = $this->sendRequest($this->url."/net?asn=".$asn);
$json = json_decode($json);
if ( isset($json->meta->error) ) { return 0; }
else { return $json->data[0]; }
}
}
public function GetIX( $asn = NULL ) {
if ( $asn ) {
$json = $this->sendRequest($this->url."/netixlan?asn=".$asn);
$json = json_decode($json);
if ( empty($json->data) ) { return 0; }
else { return $json->data; }
}
}
public function GetAllNet( $regex = NULL) {
if ( $regex ) { $regex = '?asn__contains='.$regex; }
$json = json_decode($this->sendRequest($this->url."/net".$regex));
return $json->data;
}
public function GetIXInfo( $id = NULL ) {
if ( $id ) {
$json = $this->sendRequest($this->url."/ix?id=".$id);
$json = json_decode($json);
if ( empty($json->data) ) { return 0; }
else { return $json->data; }
}
}
public function GetIXMembers( $id = NULL ) {
if ( $id ) {
$json = $this->sendRequest($this->url."/net?ix_id=".$id);
$json = json_decode($json);
if ( empty($json->data) ) { return 0; }
else { return $json->data; }
}
}
public function GetIXMembersLan( $id = NULL ) {
if ( $id ) {
$json = $this->sendRequest($this->url."/netixlan?ix_id=".$id);
$json = json_decode($json);
if ( empty($json->data) ) { return 0; }
else { return $json->data; }
}
}
public function GetIXASN($id = NULL) {
$return = array();
if ( $id ) {
foreach ($this->GetIXMembers($id) as $key => $value) {
$return[] = $value->asn;
};
}
return $return;
}
public function GetIXName( $regex = NULL) {
2018-08-24 00:14:26 +02:00
if (is_string($regex)) { $regex = '?name__contains='.urlencode($regex); }
else $regex = '';
$json = json_decode($this->sendRequest($this->url."/ix".$regex));
return $json->data;
}
}
?>