2013-10-03 122 views
0

我希望能够将我的服务器上前10名玩家从gametracker.com展示到我的网页中。 现在,我抬头一看这正显示出前10名的玩家gametracker.com页面的源代码和部分看起来像这样向我的网页显示不同网页的某些部分

<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> 

正如你可以看到我想要的内容是class="blocknew blocknew666"内我可以很容易地如果它在一个ID内,但是我不知道如何处理它,当内容是在一个类中。我抬起头,在互联网上一点,过这个

// 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>'; 

来是否有可能使用此代码做我想做什么?如果是的话,请写下我需要使用的代码行,或者给我一些关于如何解决这个问题的建议。

回答

0

基于震颤的的建议,这是对上述问题的工作代码

<?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; 

?> 
1

我只会发布部分答案,因为我认为这样做可能违反了GameTracker服务的使用条款,您要求的基本上是从另一个网站窃取专有内容的方法。在这样做之前,你绝对应该从GameTracker获得许可。

要做到这一点,我会使用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. 

现在我把它留给你找出如何砍掉前十名完成,并从GameTracker获得许可使用后到来的联合国需要的内容。

+0

我明白,但事情是从我的服务器的顶级球员,所以我觉得我有理由获得前10名玩家在我的游戏服务器中玩的名单 –

+0

@RickRoy如果他们在您的游戏服务器上玩,为什么你需要一个外部来源来跟踪他们? –

+0

我不知道如何处理来自AMX mod的提要以获取顶级玩家名单,并且从gametracker.com获取数据比从AMX提取数据要容易得多 –

0

您所提供的代码是simpledom库的一部分

http://simplehtmldom.sourceforge.net/

您需要下载并包括代码工作的库。

+0

是的我看到了所以修改了使用'file_get_contents'的代码,因为我只打算获取内容,并且不在乎它是否在xml中,因此适用于这种情况 –