Skip to content

Instantly share code, notes, and snippets.

@Dysta
Last active February 11, 2021 22:49
Show Gist options
  • Save Dysta/c54fd9576fe8c98801881d9ef2596042 to your computer and use it in GitHub Desktop.
Save Dysta/c54fd9576fe8c98801881d9ef2596042 to your computer and use it in GitHub Desktop.
code source de mon API pour parser RPG
<?php
/**
* API pour RPG paradize
* @author Dysta
*/
class RPG_Parser {
/**
* HTML code
* @var string
*/
private $webContent;
/**
* RPG id
* @var int
*/
private $id;
/**
* RPG position
* @var int
*/
private $position;
/**
* RPG name
* @var string
*/
private $name;
/**
* RPG outs
* @var int
*/
private $outs;
/**
* RPG votes
* @var int
*/
private $votes;
/**
* RPG graph data
* @var array
*/
private $graph;
public function __construct($id) {
$this->id = $id;
}
private function getWebContent() {
$opts = array(
CURLOPT_URL => 'http://rpg-paradize.com/site--' . $this->id,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'
);
$ch = curl_init();
curl_setopt_array($ch, $opts);
$this->webContent = curl_exec($ch);
curl_close($ch);
}
public function getName(){
if (empty($this->webContent))
$this->getWebContent();
if (empty($this->name)) {
preg_match('/<b>(.*?)<\/b>/', $this->webContent, $name);
$this->name = $name[1];
}
return $this->name;
}
public function getVote(){
if (empty($this->webContent))
$this->getWebContent();
if (empty($this->votes)) {
preg_match('/Vote : (.*?)<\/a>/', $this->webContent, $vote);
$this->votes = $vote[1];
}
return $this->votes;
}
public function getPosition(){
if (empty($this->webContent))
$this->getWebContent();
if (empty($this->position)) {
preg_match('/Position (.*?)<\/b>/', $this->webContent, $position);
$this->position = $position[1];
}
return $this->position;
}
public function getOut(){
if (empty($this->webContent))
$this->getWebContent();
if (empty($this->outs)) {
preg_match('/Clic Sortant : (.*?)<\/div>/', $this->webContent, $out);
$this->outs = $out[1];
}
return $this->outs;
}
public function getGraph(){
if (empty($this->webContent))
$this->getWebContent();
if (empty($this->graph)) {
preg_match('/labels : \[(.*?)\]/',$this->webContent, $label);
preg_match('/data : \[(.*?)\]/', $this->webContent, $data);
$array_label = explode(",", $label[1]);
$array_data = explode(",", $data[1]);
foreach($array_label as $k => $v)
$array_label[$k] = substr($v, 1, -1);
$this->graph = array();
foreach ($array_label as $k => $v) {
$this->graph[$k]['date'] = $array_label[$k];
$this->graph[$k]['vote'] = $array_data[$k];
}
}
return $this->graph;
}
public function toJSON() {
$data = array(
'name' => $this->getName(),
'vote' => $this->getVote(),
'position' => $this->getPosition(),
'out' => $this->getOut(),
'graphe' => $this->getGraph()
);
return json_encode($data);
}
}
// example usage
if (!isset($_GET['id'])):
echo "Usage : url.co?id={id_rpg}";
die();
endif;
$RPG = new RPG_Parser($_GET['id']);
$json_data = $RPG->toJSON();
header("Content-Type: application/json");
echo $json_data;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment