PHP实现模拟登录

来源:互联网 发布:3d算法技巧 编辑:程序博客网 时间:2024/06/11 10:45

方法一:在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_codeexec($cmd, $res);$result = $res[0];?>


方法二:利用PHP cURL方法

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


1 0
原创粉丝点击