新手进行php的模拟登录 按照网页的代码进行输入无法显示结果
这是登录的代码,是根据网页提示进行php模拟登录的代码进行修改的,模拟登录开源中国。
function login_post($url, $cookie, $post) {
$curl = curl_init();//初始化curl模块
curl_setopt($curl, CURLOPT_URL, $url);//登录提交的地址
curl_setopt($curl, CURLOPT_HEADER, 0);//是否显示头信息
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);//是否自动显示返回的信息
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); //设置Cookie信息保存在指定的文件中
curl_setopt($curl, CURLOPT_POST, 1);//post方式提交
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));//要提交的信息
curl_exec($curl);//执行cURL
curl_close($curl);//关闭cURL资源,并且释放系统资源
}
//登录成功后获取数据
function get_content($url, $cookie) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //读取cookie
$rs = curl_exec($ch); //执行cURL抓取页面内容
curl_close($ch);
return $rs;
}
$post = array (
'email' => [email protected]',
'pwd' => 'smud19941202',
'goto_page' => '/my',
'error_page' => '/login',
'save_login' => '1',
'submit' => '现在登录'
);
//登录地址
$url = "http://m.oschina.net/login";
//设置cookie保存路径
$cookie = dirname(__FILE__) . '/cookie_oschina.txt';
//登录后要获取信息的地址
$url2 = "http://m.oschina.net/my";
//模拟登录
login_post($url, $cookie, $post);
//获取登录页的信息
$content = get_content($url2, $cookie);
//删除cookie文件
@ unlink($cookie);
//匹配页面信息
$preg = "/
<br /><?php<br />$cookie_file = dirname(__FILE__).'/cookie.txt';<br /><br />$rs=login('邮件地址','密码',$cookie_file);<br />if($rs){<br /> $rs=json_decode($rs,true);<br /> exit($rs['msg']);<br />}else{<br /> echo '<span style="max-width:90%">登陆成功</span>';<br />}<br />echo "<br>";<br />echo "<hr>";<br /><br />echo getIndex($cookie_file);//输出OSC首页<br /><br />function login($user,$pwd,$cookie_file){<br /> $post='email='.urlencode($user).'&pwd='.sha1($pwd).'&verifyCode=&save_login=1';<br /> // 初始化<br /> $curl = curl_init('http://www.oschina.net/action/user/hash_login');<br /> $header = array();<br /> $header[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36';<br /> curl_setopt($curl, CURLOPT_HTTPHEADER, $header);<br /> // 不输出header头信息<br /> curl_setopt($curl, CURLOPT_HEADER, 0);<br /> // 保存到字符串而不是输出<br /> curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);<br /> curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file); //存储cookies<br /> // post数据<br /> curl_setopt($curl, CURLOPT_POST, 1);<br /> // 请求数据<br /> curl_setopt($curl, CURLOPT_POSTFIELDS, $post);<br /> // 是否抓取跳转后的页面<br /> curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);<br /> $response = curl_exec($curl);<br /> curl_close($curl);<br /> return $response;<br />}<br /><br />function getIndex($cookie_file){<br /> // 初始化<br /> $curl = curl_init('http://www.oschina.net');<br /> $header = array();<br /> $header[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36';<br /> //$header[] = 'Cookie: tc=AQAAAOaUCR/QXw4AwxXKeMgqcfroqWeb; oscid=PwzzXphS9GBIcX5pQFukpai0W0vxPrG4zeASFKTqkhgqGAHYCQekA%2FptCnh127%2BP5immlCrhkQqiEriCja%2F4ov1i2XHq2D%2BP6Dn8L9pfVydrkxvpObEDaOsIy9RU4DtH';<br /> curl_setopt($curl, CURLOPT_HTTPHEADER, $header);<br /> // 不输出header头信息<br /> curl_setopt($curl, CURLOPT_HEADER, 0);<br /> curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);<br /> // 保存到字符串而不是输出<br /> curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);<br /> // 是否抓取跳转后的页面<br /> curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);<br /> $response = curl_exec($curl);<br /> curl_close($curl);<br /> return $response;<br />}<br />