教你用PHP爬取王者荣耀英雄皮肤高清壁纸(附源码)

289 阅读1分钟

午饭后看到一篇文章关于“用Python爬取王者荣耀官网的英雄皮肤图片”,于是顺手用PHP也写了一个,在这里给大家分享一下,贴出源码:

//下载王者荣耀皮肤图片
public function heroSkinDownload(){
    $heroApi = 'https://pvp.qq.com/web201605/js/herolist.json';
    $heroArr = json_decode(Curl::get($heroApi),true);
    foreach ($heroArr as $v){
        $saveDir = '/heroskin/' . $v['cname'] . '/';
        $i = 1;
        while (true){
            $url = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/'.$v['ename'].'/'.$v['ename'].'-bigskin-'.$i.'.jpg';
            $headers = get_headers($url);
            if (!strstr($headers[0], '200')) {
                break;
            }
            if (!file_exists($saveDir)) {
                mkdir($saveDir);
            }
            $this->downloadFile($url, $saveDir . str_replace('/', '', parse_url($url)['path']));
            $i++;
        }
        echo "下载==".$v['cname']."==success\n";
    }
}
 
//下载远程文件
public function downloadFile($file_url, $save_to) {
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_POST, 0);
     curl_setopt($ch,CURLOPT_URL,$file_url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     $file_content = curl_exec($ch);
     curl_close($ch);
     $downloaded_file = fopen($save_to, 'w');
     fwrite($downloaded_file, $file_content);
     fclose($downloaded_file);
}

 运行如下:

下载结果: