用到官方的两个接口,一个是获取图片地址以及qrid,一个是用qrid验证二维码状态
获取新浪登入二维码
public function getqrpic()
{
$url = "https://login.sina.com.cn/sso/qrcode/image?entry=weibo&size=180&callback=STK_" . $this->getTime();
$header = [
'Accept:' => '*/*',
'Accept-Encoding:' => 'gzip, deflate, br',
'Accept-Language:' => 'en-US,en;q=0.9',
'Connection:' => 'keep-alive',
'Cookie:' => 'mm_lang=en; mm_lang=en',
'Host:' => 'login.wx.qq.com',
'Referer:' => 'https://wx.qq.com/',
'User-Agent:' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
];
$res = Curl::url($url)->setHeader($header)->get();
$regex = "#window(.*?)\(#";
$resArr = json_decode(rtrim(rtrim(preg_replace($regex, "", $res), ';'), ')'), true);
// echo ltrim($resArr['data']['image'], '//');
if ($resArr['retcode'] == 20000000) {
Json::msg(0, 'success', ['qrid' => $resArr['data']['qrid'], 'image' => 'https://' . ltrim($resArr['data']['image'], '//')]);
} else {
Json::msg(1, '二维码获取失败');
}
}
利用返回的qrid验证二维码状态
public function cron()
{
if (input('?get.qrid')) {
$qrid = input('get.qrid');
} else {
Json::msg(400, 'qrid不能为空');
}
$url = "https://login.sina.com.cn/sso/qrcode/check?entry=weibo&qrid=" . $qrid . "&callback=STK_" . $this->getTime();
// echo $url;exit;
$header = [
'Accept:' => '*/*',
'Accept-Encoding:' => 'gzip, deflate, br',
'Accept-Language:' => 'en-US,en;q=0.9',
'Connection:' => 'keep-alive',
'Cookie:' => 'mm_lang=en; mm_lang=en',
'Host:' => 'login.wx.qq.com',
'Referer:' => 'https://wx.qq.com/',
'User-Agent:' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
];
$res = Curl::url($url)->setHeader($header)->get();
$regex = "#window(.*?)\(#";
$resArr = json_decode(rtrim(rtrim(preg_replace($regex, "", $res), ';'), ')'), true);
if($resArr['retcode'] == 50114003){
Json::msg(205, '二维码已失效');
} elseif($resArr['retcode'] == 50114001) {
Json::msg(202, '二维码未失效');
} elseif($resArr['retcode'] == 50114002) {
Json::msg(204, '正在验证二维码');
} elseif ($resArr['retcode'] == 50114004) {
Json::msg(204, '该二维码已登录,请重新扫描');
} elseif ($resArr['retcode'] == 20000000) {
//Json::msg(200, 'sucess', $resArr);
preg_match("/-(.*?)-/", $resArr['data']['alt'], $ts_arr);
$wb = base64_decode($ts_arr[1]);
// 这里写成功后的操作
// 扫码成功后会返回一个固定的base64,利用这个值登入吧
$this->reg($wb);
} else{
Json::msg(306, $res);
}
}
附getTime方法,此方法获取13位时间戳
private function getTime() {
$arr = explode('.', microtime(true));
return (float)($arr[0] . substr($arr[1], 0 , 3));
}
前端代码博主就不贴了,定时器罢了
评论 (0)