php curl 模拟登录百度首页解决方案
php curl 模拟登录百度首页
代码如下:
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><?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>/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>/i', $source, $matches); if(!empty($matches)) { $a = $matches[1]; } else { die('Not found the '.$chr.'.'); } $array[$chr]=$a; return $array; } ?> <div class="clear"> </div>

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

curl和Pythonrequests都是发送HTTP请求的强大工具。虽然curl是一种命令行工具,可让您直接从终端发送请求,但Python的请求库提供了一种更具编程性的方式来从Python代码中发送请求。将curl转换为Pythonrequestscurl命令的基本语法如下所示:curl[OPTIONS]URL将curl命令转换为Python请求时,我们需要将选项和URL转换为Python代码。这是一个示例curlPOST命令:curl-XPOSThttps://example.com/api

在Linux下更新curl版本,您可以按照以下步骤进行操作:检查当前curl版本:首先,您需要确定当前系统中安装的curl版本。打开终端,并执行以下命令:curl--version该命令将显示当前curl的版本信息。确认可用的curl版本:在更新curl之前,您需要确定可用的最新版本。您可以访问curl的官方网站(curl.haxx.se)或相关的软件源,查找最新版本的curl。下载curl源代码:使用curl或浏览器,下载您选择的curl版本的源代码文件(通常为.tar.gz或.tar.bz2

从头到尾:如何使用php扩展cURL进行HTTP请求引言:在Web开发中,经常需要与第三方API或其他远程服务器进行通信。而使用cURL进行HTTP请求是一种常见而强大的方式。本文将介绍如何使用php扩展cURL来执行HTTP请求,并提供一些实用的代码示例。一、准备工作首先,确保php已安装cURL扩展。可以在命令行执行php-m|grepcurl查

PHP8.1发布:引入curl多个请求并发处理近日,PHP官方发布了最新版本的PHP8.1,其中引入了一个重要的特性:curl多个请求并发处理。这个新特性为开发者提供了一个更加高效和灵活的方式来处理多个HTTP请求,极大地提升了性能和用户体验。在以往的版本中,处理多个请求往往需要通过创建多个curl资源,并使用循环来分别发送和接收数据。这种方式虽然能够实现目

PHPCurl中如何处理网页的301重定向?在使用PHPCurl发送网络请求时,时常会遇到网页返回的301状态码,表示页面被永久重定向。为了正确处理这种情况,我们需要在Curl请求中添加一些特定的选项和处理逻辑。下面将详细介绍在PHPCurl中如何处理网页的301重定向,并提供具体的代码示例。301重定向处理原理301重定向是指服务器返回了一个30

在linux中,curl是一个非常实用的、用来与服务器之间传输数据的工具,是一个利用URL规则在命令行下工作的文件传输工具;它支持文件的上传和下载,是综合传输工具。curl提供了一大堆非常有用的功能,包括代理访问、用户认证、ftp上传下载、HTTP POST、SSL连接、cookie支持、断点续传等等。

对于PHP开发者来说,使用POST带参数跳转页面是一项基本技能。POST是HTTP中一种发送数据的方法,它可以通过HTTP请求向服务器提交数据,跳转页面则是在服务器端进行页面的处理和跳转。在实际开发中,我们经常需要使用POST带参数来跳转页面,以达到一定的功能目的。

python模拟浏览器发送post请求importrequests格式request.postrequest.post(url,data,json,kwargs)#post请求格式request.get(url,params,kwargs)#对比get请求发送post请求传参分为表单(x-www-form-urlencoded)json(application/json)data参数支持字典格式和字符串格式,字典格式用json.dumps()方法把data转换为合法的json格式字符串次方法需要
