通过拼接随机数,以及已知的接口,使用循环抓取数据并保存。
$time_start = time();  // 开始时间
$tongji_file = "./c.php";
$tongji = array();
if(file_exists($tongji_file)){
    $tongji = require($tongji_file);
}
$num = 100;
$json_arr = array();
$data_file = "./data.php";
if(file_exists($data_file)){
    $json_arr = require($data_file);
}
for($j=1; $j<=$num; $j++){
// 生成8-11位随机数
    $rand_str = "";
    $wei = rand(10,11);
    for($i=0;$i<$wei;$i++){
        if($i==0){
            $rand_str .= rand(1,9);
        }else{
            $rand_str .= rand(0,9);
        }
    }
    $rand_url = "https://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins=".$rand_str;
//初始化curl对象
    $ch = curl_init();
//设置URL
    curl_setopt($ch, CURLOPT_URL, $rand_url);
// 字符串形式
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 允许301,302
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// 取消https验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,0);  // 无限等待时长
    $result =     curl_exec($ch);
// 字符转码
    $data=iconv("GB2312","UTF-8",$result);
// 去掉port
    $pattern = '/portraitCallBack\((.*)\)/is';
    preg_match($pattern,$data,$result);
    $result=$result[1];
// 字符串转arr
    $arr = json_decode($result,true);
    $qq_img = "";  // QQ头像
    $qq_nick = ""; // QQ昵称
    if(!is_array($arr)){
        $j--;
        continue;
    }
    $qq = "";
    foreach ($arr as $k=> $v){
        $qq = $k;  // QQ号码
        // 判断用户是否存在
        $qq_img = $arr[$k][0];
        $qq_nick = $arr[$k][6];
        break;
    }
    if($qq_nick){
        // 尝试插入一条数据
//        $sql = $dsql->setQuery("insert into `#@__member`(`username`) values()");
        echo "<h2>$j</h2>";
        $img = "https://q1.qlogo.cn/g?b=qq&nk=".$qq."&s=100&t=1547904810";
        echo "<p>头像地址:<img src='$img'/></p>";
        echo "<p>昵称:$qq_nick</p>";
        $json_arr[] = [
            'qq' => $qq,
            'name' => $qq_nick,
            'img' => $qq_img
        ];
    }else{
        // 重新抓取
        $j--;
        continue;
    }
}
$time_end = time();
$current = ($time_end-$time_start);
echo "<p>本次用时:".$current."秒</p>";
$tongji[] = $current;  // 新增一项记录
$totalSum = 0;
foreach ($tongji as $k=>$v){
    $totalSum += $v;
}
$avg = number_format($totalSum/count($tongji,2));
echo "<p>总共抓取:".count($tongji)."次,平均耗时:".$avg."秒</p>";
$text='<?php return '.var_export($tongji,true).';';
file_put_contents($tongji_file,$text);
// 保存数据
$text='<?php return '.var_export($json_arr,true).';';
file_put_contents($data_file,$text);本篇完,还有疑问?留下评论吧