> 백엔드 개발 > PHP 튜토리얼 > php curl 模拟登录百度首页

php curl 模拟登录百度首页

WBOY
풀어 주다: 2016-06-23 13:43:43
원래의
1478명이 탐색했습니다.

代码如下:

<?php$url = "https://passport.baidu.com/?login&tpl=mn";//$url = "http://www.baidu.com/";$cookdir = "d:/www/html/mndl/cookie.txt";$ch = curl_init();curl_setopt ($ch, CURLOPT_URL, $url);curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //这句是必须有的  设定为不验证证书 下面是不验证host。curl_setopt ($ch, CURLOPT_HEADER, 0);   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookdir); //保存cookies 发送用户浏览器信息 发送访问来源curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");curl_setopt ($ch, CURLOPT_REFERER, "http://www.baidu.com/");$contents = curl_exec($ch);curl_close($ch);/* 原始表单样式				<input type="hidden" name="tpl_ok" value="">				<input type="hidden" name="next_target" value="">				<input type="hidden" name="tpl" value="mn">				<input type="hidden" name="skip_ok" value="">				<input type="hidden" name="aid" value="">				<input type="hidden" name="need_pay" value="">				<input type="hidden" name="need_coin" value="">				<input type="hidden" name="pay_method" value="">				<input type="hidden" id="u" name="u" value="http://www.baidu.com/">				<input type="hidden" name="return_method" value="get">				<input type="hidden" name="more_param" value="">				<input type="hidden" name="return_type" value="">				<input type="hidden" name="psp_tt" value="0">				<input type="hidden" id="password" name="password" value="">				<input type="hidden" name="safeflg" id="safeflg" value="">				<input type="hidden" name="isphone" value="tpl">*/$post_fields = array();$post_fields = preg_data($contents,'aid',$post_fields);$post_fields = preg_data($contents,'isphone',$post_fields);$post_fields['mem_pass'] = 'on'; //post 数据数组  记住我的登录状态$post_fields = preg_data($contents,'more_param',$post_fields);$post_fields = preg_data($contents,'need_coin',$post_fields);$post_fields = preg_data($contents,'need_pay',$post_fields);$post_fields = preg_data($contents,'next_target',$post_fields);$post_fields['password'] = 'password'; //post 数据数组  记住我的登录状态$post_fields = preg_data($contents,'pay_method',$post_fields);$post_fields = preg_data($contents,'psp_tt',$post_fields);$post_fields = preg_data($contents,'return_method',$post_fields);$post_fields = preg_data($contents,'return_type',$post_fields);$post_fields = preg_data2($contents,'safeflg',$post_fields);$post_fields = preg_data($contents,'skip_ok',$post_fields);$post_fields = preg_data($contents,'tpl',$post_fields);$post_fields = preg_data($contents,'tpl_ok',$post_fields);$post_fields['u'] = 'http://www.baidu.com/';$post_fields['username'] = 'username'; //post 数据数组  记住我的登录状态$post_fields['verifycode'] = '';$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_POST, 1);curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_fields);curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookdir);curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");curl_setopt ($ch, CURLOPT_REFERER, "https://passport.baidu.com/?login&tpl=mn");curl_exec($ch);curl_close($ch);$url = "http://www.baidu.com/";$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, $cookdir);curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");curl_setopt ($ch, CURLOPT_REFERER, "http://www.baidu.com/n");$contents = curl_exec($ch);print_r($contents);die;curl_close($ch);exit;function preg_data($source,$chr,$array){		preg_match('/<input\s*type="hidden"\s*name="'.$chr.'"\s*value="(.*?)">/i', $source, $matches);		if(!empty($matches)) {		    $a = $matches[1];		} else {		    die('Not found the '.$chr.'.');		}		$array[$chr]=$a;		return $array;}function preg_data2($source,$chr,$array){		preg_match('/<input\s*type="hidden"\s*name="'.$chr.'"\s*id="'.$chr.'"\s*value="(.*?)">/i', $source, $matches);		if(!empty($matches)) {		    $a = $matches[1];		} else {		    die('Not found the '.$chr.'.');		}		$array[$chr]=$a;		return $array;}?>
로그인 후 복사


现在是无法访问到登录后的页面 高手指教


回复讨论(解决方案)

可能是网址转向了

CURLOPT_FOLLOWLOCATION设置这个参数为TRUE试试看

CURLOPT_FOLLOWLOCATION设置这个参数为TRUE试试看



这个貌似不行啊

看不懂咋回事!!!

之前已经写了python和c#的版本的了。
等我抽空,再弄个php版本的,模拟登录百度,给你看看。

楼主问题解决了么

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿