Home  >  Article  >  php教程  >  教你如何用php实现LOL数据远程获取

教你如何用php实现LOL数据远程获取

WBOY
WBOYOriginal
2016-06-13 09:34:011062browse

过几天网站就要上线了。

最近完成了一个小功能,就是LOL数据获取,

比如:我给你一个号,你把这个号是否打过排位?战斗力是多少?胜率和所在的总场数数据获取过来

数据都在多玩的网站上可查,所以该做的功能就是远程抓取。

功能没啥亮点,就是简单的实现。

反正就是JS不能跨域,然后用PHP去跨域,用file_get_content好类或者是curl好,都不重要。重要是的能理解业务流程。

上面这个图就是执行业务流程图。清楚流程了,然后代码就好写了

当然说了,这里就,重点是PHP怎么去抓取数据的。

这里要介绍一款非常好的PHP类,Simple_html_dom(自行百度获取文档)

复制代码 代码如下:


public function getData(){
    $server = isset($_POST['gameserver'])?trim($_POST['gameserver']):NULL;
    $name = isset($_POST['gamename'])?trim($_POST['gamename']):NULL;
    import("@.ORG.SimpleHtmlDom");       //数据抓取类
        $url = "http://lolbox.duowan.com/playerDetail.php?serverName=".urlencode($server)."&playerName=".urlencode($name);
        $html = file_get_html($url);
        $dom = $html->find('.fighting',0)->children(1);
        $result['zdl'] =  strip_tags($dom->innertext);
        $doms = $html->find('.J_content',0)->children(1);
        //echo $html->find("#ranked_tier",0)->innertext;
        $temp =  $doms->plaintext;
        $tempArray = explode(" ",trim($temp));
        foreach($tempArray as $key=>$value)
        {
            if(!empty($value))
            {
                $tempArr[] = trim($value);
            }
        }
        unset($tempArray);
        //获取排位类型
        $pwtype = $tempArr[8];
        $pwtotal = $tempArr[12];
        $pwsl = $tempArr[14];
        if($pwtype == "5v5单双排")
        {
            $result['pw'] = $pwtotal;
            $result['pwsl'] = $pwsl;
        }else{
            $result['pw'] = "0";
            $result['pwsl'] = "0";
        }

         $this->ajaxReturn($result) ;
}

上面这些代码,暴露了哥英语过了四级但还是硬伤的BUG。
上面这个类很简单,难点在于怎么去分析多玩查询页面的数据。用firebug看看吧。
写多了,你就知道的了。当然了,你想查询rank隐藏分数,也是可以滴,不过要去马化腾网站去获取数据了,这里就不详细说明了,提供个思路就可以了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn