Skip to content

Instantly share code, notes, and snippets.

@ArnaudLigny
Created January 25, 2012 17:44
Show Gist options
  • Save ArnaudLigny/1677543 to your computer and use it in GitHub Desktop.
Save ArnaudLigny/1677543 to your computer and use it in GitHub Desktop.
PHP script (that uses ZF) returns your latest Battlelog servers list as array
<?php
/**
* PHP script (that uses ZF) returns your latest Battlelog servers list as array
*
* @author Arnaud Ligny aka Narno <arnaud@ligny.org>
*/
define('DS', DIRECTORY_SEPARATOR);
define('PS', PATH_SEPARATOR);
define('BP', dirname(dirname(dirname(dirname(__FILE__))))); // ../../../ change it if necessary
// Zend Framework
$paths[] = BP . DS . 'zf/library'; // change it if necessary
$original_include_path = get_include_path();
$appPath = implode(PS, $paths);
set_include_path($appPath . PS . $original_include_path);
require 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
// log into Battlelog
$c = new Zend_Http_Client();
$r = $c->setCookieJar()
->setUri('https://battlelog.battlefield.com/bf3/gate/login/')
->setParameterPost('redirect', '')
->setParameterPost('email', 'xxxx@xxxx.xxx')
->setParameterPost('password', 'xxxx')
->setParameterPost('remember', '1')
->setParameterPost('submit', 'Sign In')
->request('POST');
// go to servers list
$r = $c->setUri('http://battlelog.battlefield.com/bf3/servers/')
->request('GET');
// get page body
$body = $r->getBody();
$servers = array();
// search for JS sections
if (preg_match_all('|<script type="text\/javascript">(.*)<\/script>|Us', $body, $matches)) {
$jsData = $matches[1][2]; // we know the number of the JS section (number 3 = index 2)
// search each server row
if (preg_match_all('|serverguide\.serverrow\.surface_3_2\.render\((.*)\);|Us', $jsData, $matches)) {
foreach ($matches[1] as $key => $serverRow) {
// extract json data
if (preg_match('|(\{.*\}), block|Us', $serverRow, $jsonMatches) ) {
$servers[] = Zend_Json::decode($jsonMatches[1]);
}
}
}
}
// DEBUG
echo '<pre>', print_r($servers), '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment