Home  >  Article  >  Backend Development  >  php curl 模拟登录百度首页,该如何处理

php curl 模拟登录百度首页,该如何处理

WBOY
WBOYOriginal
2016-06-13 12:11:241026browse

php curl 模拟登录百度首页

本帖最后由 STONEWP 于 2012-03-29 13:43:04 编辑 代码如下:
<?php<br />$url = "https://passport.baidu.com/?login&tpl=mn";<br />//$url = "http://www.baidu.com/";<br />$cookdir = "d:/www/html/mndl/cookie.txt";<br />$ch = curl_init();<br />curl_setopt ($ch, CURLOPT_URL, $url);<br />curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //这句是必须有的  设定为不验证证书 下面是不验证host。<br />curl_setopt ($ch, CURLOPT_HEADER, 0);   <br />curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);<br />curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookdir); //保存cookies 发送用户浏览器信息 发送访问来源<br />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)");<br />curl_setopt ($ch, CURLOPT_REFERER, "http://www.baidu.com/");<br />$contents = curl_exec($ch);<br />curl_close($ch);<br /><br />/* 原始表单样式<br />				<input type="hidden" name="tpl_ok" value=""><br />				<input type="hidden" name="next_target" value=""><br />				<input type="hidden" name="tpl" value="mn"><br />				<input type="hidden" name="skip_ok" value=""><br />				<input type="hidden" name="aid" value=""><br />				<input type="hidden" name="need_pay" value=""><br />				<input type="hidden" name="need_coin" value=""><br />				<input type="hidden" name="pay_method" value=""><br />				<input type="hidden" id="u" name="u" value="http://www.baidu.com/"><br />				<input type="hidden" name="return_method" value="get"><br />				<input type="hidden" name="more_param" value=""><br />				<input type="hidden" name="return_type" value=""><br />				<input type="hidden" name="psp_tt" value="0"><br />				<input type="hidden" id="password" name="password" value=""><br />				<input type="hidden" name="safeflg" id="safeflg" value=""><br />				<input type="hidden" name="isphone" value="tpl"><br />*/<br />$post_fields = array();<br />$post_fields = preg_data($contents,'aid',$post_fields);<br />$post_fields = preg_data($contents,'isphone',$post_fields);<br />$post_fields['mem_pass'] = 'on'; //post 数据数组  记住我的登录状态<br />$post_fields = preg_data($contents,'more_param',$post_fields);<br />$post_fields = preg_data($contents,'need_coin',$post_fields);<br />$post_fields = preg_data($contents,'need_pay',$post_fields);<br />$post_fields = preg_data($contents,'next_target',$post_fields);<br />$post_fields['password'] = 'password'; //post 数据数组  记住我的登录状态<br />$post_fields = preg_data($contents,'pay_method',$post_fields);<br />$post_fields = preg_data($contents,'psp_tt',$post_fields);<br />$post_fields = preg_data($contents,'return_method',$post_fields);<br />$post_fields = preg_data($contents,'return_type',$post_fields);<br />$post_fields = preg_data2($contents,'safeflg',$post_fields);<br />$post_fields = preg_data($contents,'skip_ok',$post_fields);<br />$post_fields = preg_data($contents,'tpl',$post_fields);<br />$post_fields = preg_data($contents,'tpl_ok',$post_fields);<br />$post_fields['u'] = 'http://www.baidu.com/';<br />$post_fields['username'] = 'username'; //post 数据数组  记住我的登录状态<br />$post_fields['verifycode'] = '';<br /><br />$ch = curl_init();<br />curl_setopt ($ch, CURLOPT_URL, $url);<br />curl_setopt ($ch, CURLOPT_HEADER, 0);<br />curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);<br />curl_setopt ($ch, CURLOPT_POST, 1);<br />curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_fields);<br />curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookdir);<br />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)");<br />curl_setopt ($ch, CURLOPT_REFERER, "https://passport.baidu.com/?login&tpl=mn");<br />curl_exec($ch);<br />curl_close($ch);<br /><br />$url = "http://www.baidu.com/";<br />$ch = curl_init();<br />curl_setopt($ch, CURLOPT_URL, $url);<br />curl_setopt($ch, CURLOPT_HEADER, 0);<br />curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br />curl_setopt($ch, CURLOPT_COOKIEFILE, $cookdir);<br />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)");<br />curl_setopt ($ch, CURLOPT_REFERER, "http://www.baidu.com/n");<br /><br />$contents = curl_exec($ch);print_r($contents);die;<br />curl_close($ch);<br /><br />exit;<br />function preg_data($source,$chr,$array){<br />		preg_match('/<input\s*type="hidden"\s*name="'.$chr.'"\s*value="(.*?)">/i', $source, $matches);<br />		if(!empty($matches)) {<br />		    $a = $matches[1];<br />		} else {<br />		    die('Not found the '.$chr.'.');<br />		}<br />		$array[$chr]=$a;<br />		return $array;<br />}function preg_data2($source,$chr,$array){<br />		preg_match('/<input\s*type="hidden"\s*name="'.$chr.'"\s*id="'.$chr.'"\s*value="(.*?)">/i', $source, $matches);<br />		if(!empty($matches)) {<br />		    $a = $matches[1];<br />		} else {<br />		    die('Not found the '.$chr.'.');<br />		}<br />		$array[$chr]=$a;<br />		return $array;<br />}<br />?><br />


现在是无法访问到登录后的页面 高手指教
------解决思路----------------------
CURLOPT_FOLLOWLOCATION设置这个参数为TRUE试试看
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn