• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[PHP] Script to fetch TibiaCoins transactions.

atef201080

Member
Joined
Aug 16, 2009
Messages
26
Solutions
1
Reaction score
20
Hi.
This PHP script will fetch Tibia Coins transactions and add them to a table, can be used as donation system.
can be used on a cronjob to fetch the transactions periodically.

PHP:
<?php

// crontab -e
// * * * * * php /var/www/html/check_coin_transactions.php

$dbserver = "localhost";
$username = "root";
$password = "";
$dbname = "tibia";

$acc = "[email protected]";
$pass = "www";
$character = "Character Name";
$transfers_after_id = 80; // don't include transfers with id less or equal

$conn = new mysqli($dbserver, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$url = "https://www.tibia.com/account/?subtopic=accountmanagement";

$options = array(
    CURLOPT_USERAGENT      => "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0",
    CURLOPT_COOKIESESSION  => true,
    CURLOPT_COOKIEFILE     => getcwd() . "/cookies.txt",
    CURLOPT_COOKIEJAR      => getcwd() . "/cookies.txt",
    CURLOPT_CUSTOMREQUEST  => "POST",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER         => false,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_ENCODING       => "",
    CURLOPT_AUTOREFERER    => true,
    CURLOPT_CONNECTTIMEOUT => 20,
    CURLOPT_TIMEOUT        => 20,
    CURLOPT_MAXREDIRS      => 10,
    CURLOPT_POSTFIELDS     => "loginemail=$acc&loginpassword=$pass&page=tibiacoinshistory&Login.x=" . rand(30, 101). "&Login.y=" . rand(4, 40),
    CURLOPT_HTTPHEADER     => array(
        'Content-Type: application/x-www-form-urlencoded',
        'Accept: */*',
        'Pragma: no-cache',
        'origin: https://www.tibia.com',
        'referer: https://www.tibia.com/account/?subtopic=accountmanagement',
    )
);

$ch      = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);

$transfers = array();

preg_match_all('/<tr[^>]*><td[^>]*>(.*?)<\/td><td[^>]*>(.*?)<\/td><td[^>]*>(.*?)<\/td[^>]*><td[^>]*>(.*?)<\/td><td[^>]*>(.*?)<\/td><td[^>]*>(.*?)<\/td><\/tr>/si', $content, $matches);

for ($i = 0; $i < count($matches[0]); $i++) {
    if (strpos($matches[3][$i], "gifted") === false || strpos($matches[3][$i], ">") !== false)
        continue;
    $id = preg_match('/([0-9+-]+)/', str_replace(",", "", $matches[1][$i]), $id_res);
    $id = (int) ($id_res[1]);
    $amount = preg_match('/>([0-9+-]+)/', str_replace(",", "", $matches[5][$i]), $amount_res);
    $amount = (int) ($amount_res[1]);
    $fromto = preg_match('/(.+) gifted to (.+)/', $matches[3][$i], $fromto_res);
    $from = strtolower(trim($fromto_res[1]));
    $to = strtolower(trim($fromto_res[2]));
    $transfers[] = array("id" => $id, "from" => $from, "to" => $to, "amount" => $amount);
}


foreach($transfers as $transfer) {
    if($transfer["to"] != strtolower(trim($character)))
        continue;
    if($transfer["amount"] < 1)
        continue;
    if($transfer["id"] <= $transfers_after_id)
        continue;

    $result = $conn->query("select * from coin_transfers where `id` = '".$transfer['id']."'");
    if(!($result))
        continue;
    if($result->num_rows != 0)
        continue;
    
    $conn->query("INSERT INTO `coin_transfers` (`id`, `from`, `date`, `to`, `amount`, `points`, `account`) VALUES ('".$transfer['id']."', '".$conn->real_escape_string($transfer['from'])."', NOW(), '".$conn->real_escape_string($transfer['to'])."', '".$transfer['amount']."', 0, 0);");

    echo($transfer['id'] . " - " . $transfer['from'] . " - " . $transfer['to'] ." - ". $transfer['amount'] . "<br>\n");
}

$conn->close();
 
Goob job!

Maybe you could also provide the coin_transfers MySQL table schema?
 
there you go
SQL:
CREATE TABLE `coin_transfers` (
  `id` int(11) NOT NULL,
  `from` varchar(200) DEFAULT NULL,
  `date` bigint(20) NOT NULL,
  `to` varchar(200) DEFAULT NULL,
  `amount` int(11) NOT NULL,
  `points` int(11) NOT NULL,
  `account` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
Back
Top