简体   繁体   中英

Show certain part of a different webpage into mine

I want to be able to show the top 10 players on my server from gametracker.com into my webpage. Now I looked up the source code of the gametracker.com page which is showing the top 10 players and the part looks like this

<div class="blocknew blocknew666">
        <div class="blocknewhdr">
            TOP 10 PLAYERS <span class="item_text_12">(Online &amp; Offline)</span>
        </div>
        <table class="table_lst table_lst_stp">
            <tr>
                <td class="col_h c01">
                    Rank
                </td>
                <td class="col_h c02">
                    Name
                </td>
                <td class="col_h c03">
                    Score
                </td>
                <td class="col_h c04">
                    Time Played
                </td>
            </tr>
            .
            .
            .
            .
        </table>
        <div class="item_h10">
        </div>
<a class="fbutton" href="/server_info/*.*.*.*:27015/top_players/">
            View All Players &amp; Stats
        </a>
    </div>

As you can see the content I want is within the class="blocknew blocknew666" I could have easily pulled it out if it was within an id but I don't know how to handle it when the content is within a class. I looked up on the internet a bit and came across this

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

// Find all links
foreach($html->find('a') as $element)
       echo $element->href . '<br>'; 

Is it possible to use this code to do what I want? If yes please write the line of code i would need to use, or give me some suggestion on how to tackle this issue.

I'm only going to post a partial answer because I believe that doing this might be a violation of the terms of use for the GameTracker service, what you are asking for is basically a method to steal proprietary content from another website. You SHOULD most definitely be GETTING PERMISSION from GameTracker before you do this.

To do this I would use strstr. http://php.net/manual/en/function.strstr.php

$html = file_get_html('http://www.gametracker.com/server_info/someip/');
$topten = strstr($html, 'TOP 10 PLAYERS');
echo $topten; //this will print everthing after the content you looked for.

Now I will leave it up to you to figure out how to chop off the un-needed content that comes after the top ten is done AND to get permission from GameTracker to use this.

Based on tremor's suggestion this is the working code for the above problem

    <?php

function rstrstr($haystack,$needle)
    {
        return substr($haystack, 0,strpos($haystack, $needle));
    }

$html = file_get_contents('http://www.gametracker.com/server_info/*.*.*.*:27015/');
$topten = strstr($html, 'TOP 10 PLAYERS');//this will print everthing after the content you looked for.
$topten = strstr($topten, '<table class="table_lst table_lst_stp">');
$topten = rstrstr($topten,'<div class="item_h10">'); //this will trim stuff that is not needed
echo $topten;

?>

The code you provided is part of the simpledom library

http://simplehtmldom.sourceforge.net/

You need to download and include the library for the code to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM