<?php
	error_reporting(0);
	
	function request_http($url, $type = 0, $post_data = '', $ua = '', $cookie = '', $header = array(), $redirect = true){
		// 初始化cURL
		$curl = curl_init();
		// 设置网址
		curl_setopt($curl,CURLOPT_URL, $url);
		// 设置UA
		if(empty($ua) == false){
			$header[] = 'User-Agent:'.$ua;
		}
		// 设置Cookie
		if(empty($cookie) == false){
			$header[] = 'Cookie:'.$cookie;
		}
		// 设置请求头
		if(empty($ua) == false or empty($cookie) == false or empty($header) == false){
			curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		}
		// 设置POST数据
		if($type == 1){
		    curl_setopt($curl, CURLOPT_POST, true);
		    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
		}
		// 设置重定向
		if($redirect == false){
			curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
		}
		// 过SSL验证证书
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		// 将头部作为数据流输出
		curl_setopt($curl, CURLOPT_HEADER, true);
		// 设置以变量形式存储返回数据
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		// 请求并存储数据
		$return = curl_exec($curl);
		// 分割头部和身体
		if(curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200'){
			$return_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
		    $return_header = substr($return, 0, $return_header_size);
		    $return_data = substr($return, $return_header_size);
		}
		// 关闭cURL
		curl_close($curl);
		// 返回数据
		return [$return_header, $return_data];
	}
	
	function return_result($state, $info){
		$result = array(
			'state'=>$state,
			'info'=>$info
		);
		exit(stripslashes(json_encode($result, JSON_UNESCAPED_UNICODE)));
	}
	
	function get_middle_text($text, $text_left, $text_right){
		$left = strpos($text, $text_left);
		$right = strpos($text, $text_right, $left);
		if($left < 0 or $right < $left){
			return False;
		};
		return substr($text, $left + strlen($text_left), $right - $left - strlen($text_left));
	}
	
	function get_ptqrtoken($qrsig){
		$len = strlen($qrsig);
		$hash = 0;
		for($i = 0; $i < $len; $i++){
		    $hash += (($hash << 5) & 2147483647) + ord($qrsig[$i]) & 2147483647;
		    $hash &= 2147483647;
		}
		return $hash & 2147483647;
	}
	
	function get_result_data($qrsig){
		$state_data = request_http('https://ssl.ptlogin2.qq.com/ptqrlogin?u1=https://qzs.qzone.qq.com/qzone/v5/loginsucc.html?para=izone&from=iqq&ptqrtoken='.get_ptqrtoken($qrsig).'&ptredirect=1&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=0-0-'.time().'&js_ver=10233&js_type=1&login_sig='.$qrsig.'&pt_uistyle=40&aid=549000912&daid=5', null, null, null,'qrsig='.$qrsig)[1];
		if(strpos($state_data, '未失效') == true){
			return '未失效';
		}
		elseif(strpos($state_data, '认证中') == true){
			return '认证中';
		}
		elseif(strpos($state_data, '登录成功') == true){
			$ptuicb_url = get_middle_text($state_data, "'0','0','", "','1',");
			$ptuicb_header = request_http($ptuicb_url, null, null, null, null, null, false)[0]; 
			$cookie = 'uin='.get_middle_text($ptuicb_header, 'uin=', ';').';skey='.get_middle_text($ptuicb_header, 'skey=', ';').';p_uin='.get_middle_text($ptuicb_header, 'p_uin=', ';').';p_skey='.get_middle_text($ptuicb_header, 'p_skey=', ';').';pt4_token='.get_middle_text($ptuicb_header, 'pt4_token=', ';');
			return ['登录成功', $cookie];
		}
		elseif(strpos($state_data, '已失效') == true){
			return '已失效';
		}
	}
	
	function get_login_data(){
		$return = request_http('https://ssl.ptlogin2.qq.com/ptqrshow?appid=549000912&e=2&l=M&s=3&d=72&v=4&t='.time().'&daid=5&pt_3rd_aid=0');
		$qrsig = get_middle_text($return[0], 'qrsig=', ';');
		$qr_code = 'data:image/jpeg;base64,'.base64_encode($return[1]);
		return [$qrsig, $qr_code];
	}
	
	$TYPE = $_REQUEST['type'];
	$QRSIG = $_REQUEST['qrsig'];
	if(empty($TYPE) == true or ($TYPE != 'get' and empty($QRSIG) == true)){
		return_result(100, '参数错误');
	}
	elseif($TYPE == 'get'){
		$login_data = get_login_data();
		$result = array(
			'qrsig'=>$login_data[0],
			'qr_code'=>$login_data[1]
		);
		return_result(200, $result);
	}
	elseif($TYPE == 'result'){
		$result_data = get_result_data($QRSIG);
		if(is_string($result_data) == True){
			$result = $result_data;
		}
		else{
			$result = array(
				'state'=>$result_data[0],
				'cookie'=>$result_data[1]
			);
		}
		return_result(200, $result);
	}
	else{
		return_result(100, '类型错误');
	}
?>
名称 必填 备注
type get代表登录数据,result代表结果
qrsig 存在登录数据中,result时需要填写

本博客所有文章除特别声明外,均采用CC BY-SA 4.0协议,转载请注明出处.

3种方法加密Python文件 下一篇