Last active
April 28, 2023 08:39
-
-
Save mattiasghodsian/ba827623551ec4e2ff5322b25ce37d18 to your computer and use it in GitHub Desktop.
Get ladder data as array from http://pokemon-revolution-online.net
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 | |
/** | |
* Title: PRO Ladder | |
* Author: Mattias Ghodsian | |
* Description: Get ladder data as array from http://pokemon-revolution-online.net | |
* Donate a cup of coffee: https://www.buymeacoffee.com/mattiasghodsian | |
**/ | |
class PROLadder | |
{ | |
private $url = "http://pokemon-revolution-online.net/ladder.php"; | |
function __construct() | |
{ | |
if (!function_exists('curl_init')){ | |
die('CURL is not installed!'); | |
} | |
} | |
/* | |
* getLadders | |
* | |
* return array | |
*/ | |
public function getLadders() | |
{ | |
$data = []; | |
$content = $this->curl($this->url); | |
$table_start = explode( '<table cellpadding="10" cellspacing="0" class="auto-style2">' , $content ); | |
$tableInt = (count($table_start) - 1); | |
$i = 1; | |
while ($i <= $tableInt) { | |
$wdata = []; | |
$table_end = explode("</table>" , $table_start[$i] ); | |
$body = explode( '<tr>' ,$table_end[0]); | |
$key = ucwords(strtolower(trim(strip_tags(preg_replace("/\([^)]+\)/","",$body[1]))))); | |
$key = str_replace([' ', '/', '-'], '', $key); | |
$title = trim(strip_tags($body[1])); | |
$wdata['table'] = $title; | |
// unset unwanted data | |
unset($body[0], $body[1], $body[2], $body[3]); | |
// loop data | |
foreach ($body as $ladder) { | |
$rework = ltrim(str_replace('<td class="auto-style17">', ',', $ladder), ','); | |
$rework = explode(',', $rework); | |
unset($rework[0]); // unset unwanted data | |
$wdata['data'][] = $rework; | |
} | |
$data[$key] = $wdata; | |
$i++; | |
} | |
// return data | |
return $data; | |
} | |
/* | |
* getLadder | |
* | |
* @param integer $table | |
* return array | |
*/ | |
public function getLadder($table = 1) | |
{ | |
if ($table == 0) { return false; } | |
$data = []; | |
$content = $this->curl($this->url); | |
$table_start = explode( '<table cellpadding="10" cellspacing="0" class="auto-style2">' , $content ); | |
$table_end = explode("</table>" , $table_start[$table] ); | |
$body = explode( '<tr>' ,$table_end[0]); | |
$title = trim(strip_tags($body[1])); | |
$data['table'] = $title; | |
// unset unwanted data | |
unset($body[0], $body[1], $body[2], $body[3]); | |
// loop data | |
foreach ($body as $ladder) { | |
$rework = ltrim(str_replace('<td class="auto-style17">', ',', $ladder), ','); | |
$rework = explode(',', $rework); | |
unset($rework[0]); // unset unwanted data | |
$data['data'][] = $rework; | |
} | |
// return data | |
return $data; | |
} | |
/* | |
* CURL | |
* | |
* @param string $url | |
* @param string $method | |
* @param array $data | |
* return html | |
*/ | |
private function curl($url, $method = "GET", $data = "") | |
{ | |
$ch = curl_init($url); | |
curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ); | |
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $method ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
if ( !empty($data) ) { | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($data) ); | |
} | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment