DeletedUser
Guest
Not seen this posted so here is the first working draft.
This is written on the fly so no comments and nothing nice about the script.
crude and working on my shared hosting with cURL. Other MySQL imports didnt work on my cheap hosting so stuck with simple INSERT's.
The first section is purely for caching the API data from grepolis's servers to increase speed and reduce overheads I use this a lot else where. Some hosts and API providers would temp. block you if you kept hammering for the data.
PHP script to insert world data into a MySQL database.
include("dbinfo.inc.php");
This is written on the fly so no comments and nothing nice about the script.
crude and working on my shared hosting with cURL. Other MySQL imports didnt work on my cheap hosting so stuck with simple INSERT's.
The first section is purely for caching the API data from grepolis's servers to increase speed and reduce overheads I use this a lot else where. Some hosts and API providers would temp. block you if you kept hammering for the data.
PHP script to insert world data into a MySQL database.
PHP:
<?php
$cachedir="/host_directory/";
if (readCache($cachedir . 'players.gz', 86400) != 2) { /* 1 day */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://en3.grepolis.com/data/players.txt.gz');
$fp = fopen('players.gz', 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
curl_close ($ch);
fclose($fp);
uncompress("players.gz","players.txt");
$count=0;
include("dbinfo.inc.php");
mysql_connect($db_host,$db_user,$db_pwd);
@mysql_select_db($db_base) or die( "Unable to select database");
$query = "TRUNCATE TABLE ".$tableT1;
$query = @mysql_query($query);
$handle = fopen ('./players.txt', 'r');
while (($data = fgetcsv($handle, 1000, ',', '"')) !== FALSE)
{
if ($count == 1000) {
$query = @mysql_query($query);
$count = 0;
}
$count++;
if ($count == 1) {
$query = "INSERT INTO ".$tableT1." VALUES ('". implode("','", $data) ."')";
} else {
$query .= ",('". implode("','", $data) ."')";
}
}
$query = @mysql_query($query);
$query = "SELECT * FROM `".$tableT1."`";
$thing = mysql_query($query);
echo "mysql row count = ".mysql_numrows($thing)."<br>";
mysql_close();
}
echo "players.txt line count = ".countLines($cachedir.'players.txt')."<br>";
function countLines($filepath) {
$handle = fopen( $filepath, "r" );
$count = 0;
while( fgets($handle) )
{
$count++;
}
fclose($handle);
return $count;
}
function readCache($filename, $expiry) {
if (file_exists($filename)) {
if ((time() - $expiry) > filemtime($filename)) { return 1; }
return 2;
}
return 0;
}
function uncompress($srcName, $dstName) {
$string = implode("", gzfile($srcName));
$fp = fopen($dstName, "w");
fwrite($fp, $string, strlen($string));
fclose($fp);
}
?>
include("dbinfo.inc.php");
PHP:
<?php
$db_host = 'localhost';
$db_user = 'user';
$db_pwd = 'password';
$db_base = 'database';
$tableT1 = 'table';
?>
Last edited by a moderator: