PHP随机获取测试昵称

技能 · 2022-05-17 · 424 人浏览
PHP随机获取测试昵称

/**
 * https://api.sunweihu.com/api/qqxt/api.php接口可接受一个参数【qq】
 * 传入随机参数可获取对应QQ用户的昵称,拿这些昵称就可以当作测试数据
 **/
public function getUserName() {
    $nickname = null;
    while (!$nickname) {
        $http_result = httpGet('https://api.sunweihu.com/api/qqxt/api.php?qq='.mt_rand(1000000,2000000000));
        $data = json_decode(str_replace(array("\r\n", "\r", "\n"), "", http_result ));
        if ($data->code == 1) {
            $nickname = $data->name;
        }
    }
    return $nickname;
}

// 附:请求方法
function httpGet($url){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}
php 测试
Theme Jasmine by Kent Liao