TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-22-2008, 07:45 AM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default A form of a image class..

Hey!

I'm having a problem..
I wanna code a class, that would let me do something like this:

php Code:
$image = new TibiaChar('Mortis Dominus');
if($image) {
      echo $image->getImageCode();
}
else {
      echo 'Character does not exist!';
}

And that would search Tibia - Free Multiplayer Online Role Playing Game - News character page for Mortis Dominus, and if it exists, it would do some fetching of the data on that character page, and create an image for it.
Character page is: Tibia - Free Multiplayer Online Role Playing Game - Community

Then it would echo out a image code, such as "www.domain.com/images/mortis_dominus.png" so I can use it as a dynamic image.

How would I achieve this?
I know I have to use file_get_contents, and also use the PHP GD library..

I have an example here:
Tibia - Character Image
Tanax is offline  
Reply With Quote
Old 01-22-2008, 09:46 AM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

do we have a form of api for this? ie how do you get the data for the character?
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 01-22-2008, 11:16 AM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

with file_get_contents and using preg_match on the resulted page IF the character existed.

Tibia - Free Multiplayer Online Role Playing Game - Community

As you see on the link, if you click it, you come to the page with the data of my char.. if you write a char that doesn't exist instead of "Mortis%20Dominus", you won't get that page..
Tanax is offline  
Reply With Quote
Old 01-22-2008, 01:24 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Wrote you a class for this because I'm feeling kind today ! Don't forget to mention TalkPHP wherever!

php Code:
<?php

    class TalkPHP_TibiaChar
    {
        private $m_aData;
        private $m_bFound;
       
        const TIBIA_ADDRESS = 'http://www.tibia.com/community/?subtopic=characters&name=%s';
       
        public function find($szCharacter)
        {
            $szCharacter = urlencode($szCharacter);
            $szContent = file_get_contents(sprintf(self::TIBIA_ADDRESS, $szCharacter));
           
            if(empty($szContent))
            {
                throw new Exception('Tibia content is not available');
            }
           
            preg_match_all('~<td[\swidth=20%]*>([^<]+):</td><td>([^<]+)</td>~i', $szContent, $aMatches);
           
            for($iIndex = 0; $iIndex < count($aMatches[1]); $iIndex++)
            {
                $szKey = preg_replace('~[^a-z]+~i', '', ucwords($aMatches[1][$iIndex]));
                $szValue = ucwords(html_entity_decode($aMatches[2][$iIndex]));
               
                $this->m_aData[$szKey] = $szValue;
            }
        }
       
        public function __call($szCall, $aArgs)
        {
            if(preg_match('~get(?P<key>.+)~i', $szCall, $aMatches))
            {
                if(!array_key_exists($aMatches['key'], $this->m_aData))
                {
                    return false;
                }
               
                return $this->m_aData[$aMatches['key']];
            }
        }
       
        public function isCharacter()
        {
            if(count($this->m_aData) > 0)
            {
                return true;
            }
           
            return false;
        }
    }
   
    $pTibia = new TalkPHP_TibiaChar();
    $pTibia->find('Mortis Dominus');
   
    if($pTibia->isCharacter())
    {
        echo 'Name: ' . $pTibia->getName();
        echo '<br />';
        echo 'Sex: ' . $pTibia->getSex();
        echo '<br />';
        echo 'Profession: ' . $pTibia->getProfession();
        echo '<br />';
        echo 'Level: ' . $pTibia->getLevel();
        echo '<br />';
        echo 'World: ' . $pTibia->getWorld();
        echo '<br />';
        echo 'Residence: ' . $pTibia->getResidence();
        echo '<br />';
        echo 'Last Login: ' . $pTibia->getLastLogin();
        echo '<br />';
        echo 'Account Status: ' . $pTibia->getAccountStatus();
    }
    else
    {
        die('Character does not exist.');
    }

?>
Attached Files
File Type: php TalkPHP_Tibia.php (1.9 KB, 168 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
Tanax (01-22-2008)
Old 01-22-2008, 02:58 PM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

PHP Code:
for($iIndex 0$iIndex count($aMatches[1]); $iIndex++) 
NOOOOOOOOOOOOOOOOOoooooooooooooooooOOOOOOOOOOOOOoo ooo

PHP Code:
$iNumMatches count($aMatches[1]);
for(
$iIndex 0$iIndex $iNumMatches$iIndex++) 
being picky abit there, im just allergic to count() being used there.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Tanax (01-22-2008)
Old 01-22-2008, 03:12 PM   #6 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

lol @ sketchmedia
You can even do it in one line:
PHP Code:
for($iIndex 0$iNumMatches count($aMatches[1]); $iIndex $iNumMatches$iIndex++) 


For another speed boost you should use curl instead of file_get_contents
sjaq is offline  
Reply With Quote
Old 01-22-2008, 03:13 PM   #7 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
PHP Code:
for($iIndex 0$iIndex count($aMatches[1]); $iIndex++) 
NOOOOOOOOOOOOOOOOOoooooooooooooooooOOOOOOOOOOOOOoo ooo

PHP Code:
$iNumMatches count($aMatches[1]);
for(
$iIndex 0$iIndex $iNumMatches$iIndex++) 
being picky abit there, im just allergic to count() being used there.
You should be because putting it in the for loop executed count each time.
__________________

Village Idiot is offline  
Reply With Quote
Old 01-22-2008, 03:26 PM   #8 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

which was precisely why i objected to it in the first place
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 01-22-2008, 06:12 PM   #9 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Thanks!!!

I got a question though about your code there..
I suck at those.. preg_replace and that..

And I know I should read about it in some tutorial.. but I just don't.. understand it :(

So could someone please explain this to me:
PHP Code:
preg_match_all('~<td[\swidth=20%]*>([^<]+):</td><td>([^<]+)</td>~i'$szContent$aMatches); 
Also, how would I make an image out of it?
If you go to this site:
Tibia - Character Image

And enter "Mortis Dominus" as character.. you'll get an image.
Also, that image is updated every 10 minutes, how would I achieve that?

Thanks alot!
Tanax is offline  
Reply With Quote
Old 01-22-2008, 06:39 PM   #10 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Oh, and also, how would I get it to include the comments of a player?

Tibia - Free Multiplayer Online Role Playing Game - Community

That character has a comment, so how would I include it in the preg_match_all statement??
Tanax is offline  
Reply With Quote
Old 01-23-2008, 07:21 PM   #11 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Anyone? xD
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:21 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design