PHP实现模拟登录

方法一:在PHP中使用exec()函数执行linux shell语句

<?php
	$filename = 'cookie.txt';
	$username = 'clive';
	$password = 'psw123'; 
	
	$loginInfoArr = array(
		'username' => $username,
		'password' => $password,
	);
	
	$loginInfoString = http_build_query($loginInfoArr);
	$loginInfoString = urldecode($loginInfoString);
	
	$url = "https://test.clive.com/v1/oauth2/authorize";
	$cmd = "curl -d \"".$loginInfoString."\" -c $filename $url";//访问登录接口将cookie保存到文件
	exec($cmd, $res);	 		
	$result = json_decode($res[0], true);
	
	if (!empty($result['auth_code'])) {
		$auth_code = $result['auth_code'];
	} else {
		echo $result['error']);
	}

	$content = file_get_contents($filename);//取出cookie信息
	unlink($filename);
	
	$content = strstr($content, 'user');
	preg_match("/(?<=\")[\|\d\:\=a-zA-Z]{1,}/", $content, $matches);
	$user = $matches[0];//正则匹配出cookie:user
	
	$postArr = array(
		'email' => 'clive@163.com',
		'id_no' => '325254223568445',
	);
	
	$postQuery = http_build_query($postArr);
	$postQuery = urldecode($postQuery);
	
	$url = "https://test.clive.com/api/v1/checkinfo";
	$cmd = "curl -L -b \"user=".$user.';auth_code='.$auth_code."\" -d \"".$postQuery." $url";//请求需要登录后才能访问的url,带上cookie:user和auth_code
	exec($cmd, $res);
	$result = $res[0];		
?>


方法二:利用PHP cURL方法

CURLOPT_HTTPHEADER => array(
				"cache-control: no-cache",
				"cookie:user=".$user//cURL HTTPHEADER将cookie带上
			),


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值