CURL PHP模拟浏览器get和post
模拟浏览器get和post数据需要经常用到的类,
在这里收藏了几个不错的方法
方法一
<?php
define ( 'IS_PROXY', true ); //是否启用代理
/* cookie文件 */
$cookie_file = dirname ( __FILE__ ) . "/cookie_" . md5 ( basename ( __FILE__ ) ) . ".txt"; // 设置Cookie文件保存路径及文件名
/*模拟浏览器*/
$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; function vlogin($url, $data) { // 模拟登录获取Cookie函数
$curl = curl_init (); // 启动一个CURL会话
if (IS_PROXY) {
//以下代码设置代理服务器
//代理服务器地址
curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS ['proxy'] );
}
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 ); // 从证书中检查SSL加密算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS ['user_agent'] ); // 模拟用户使用的浏览器
@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
curl_setopt ( $curl, CURLOPT_POST, 1 ); // 发送一个常规的Post请求
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); // Post提交的数据包
curl_setopt ( $curl, CURLOPT_COOKIEJAR, $GLOBALS ['cookie_file'] ); // 存放Cookie信息的文件名称
curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file'] ); // 读取上面所储存的Cookie信息
curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 设置超时限制防止死循环
curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec ( $curl ); // 执行操作
if (curl_errno ( $curl )) {
echo 'Errno' . curl_error ( $curl );
}
curl_close ( $curl ); // 关闭CURL会话
return $tmpInfo; // 返回数据
} function vget($url) { // 模拟获取内容函数
$curl = curl_init (); // 启动一个CURL会话
if (IS_PROXY) {
//以下代码设置代理服务器
//代理服务器地址
curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS ['proxy'] );
}
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 ); // 从证书中检查SSL加密算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS ['user_agent'] ); // 模拟用户使用的浏览器
@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
curl_setopt ( $curl, CURLOPT_HTTPGET, 1 ); // 发送一个常规的Post请求
curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file'] ); // 读取上面所储存的Cookie信息
curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 设置超时限制防止死循环
curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec ( $curl ); // 执行操作
if (curl_errno ( $curl )) {
echo 'Errno' . curl_error ( $curl );
}
curl_close ( $curl ); // 关闭CURL会话
return $tmpInfo; // 返回数据
} function vpost($url, $data) { // 模拟提交数据函数
$curl = curl_init (); // 启动一个CURL会话
if (IS_PROXY) {
//以下代码设置代理服务器
//代理服务器地址
curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS ['proxy'] );
}
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 ); // 从证书中检查SSL加密算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS ['user_agent'] ); // 模拟用户使用的浏览器
@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
curl_setopt ( $curl, CURLOPT_POST, 1 ); // 发送一个常规的Post请求
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); // Post提交的数据包
curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file'] ); // 读取上面所储存的Cookie信息
curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 设置超时限制防止死循环
curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec ( $curl ); // 执行操作
if (curl_errno ( $curl )) {
echo 'Errno' . curl_error ( $curl );
}
curl_close ( $curl ); // 关键CURL会话
return $tmpInfo; // 返回数据
} function delcookie($cookie_file) { // 删除Cookie函数
unlink ( $cookie_file ); // 执行删除
}
?>
方法二
<?php
/**
*File:curl.class.php
*CURL封装类,本类大部分操作均支持链式操作
*
*example:
*
*$curl=newCurl($url);
*$curl->exec();
*//发送post数据
*$curl->post(array('username'=>'用户名'))->exec();
*/ classCurl{
private$ch;
private$flag_if_have_run=false;
private$has_cloase=true; publicfunction__construct($url='',$forgeIP=false){
$this->init($url,$forgeIP);
} /**
*初始化CURL。如果CURL未被关闭,则先关闭
*
*@paramtype$url
*@return\Common\Library\Curl
*/
publicfunctioninit($url='',$forgeIP=false){
if(!$this->has_cloase){//如果上一次连接尚未结束,则先关闭
$this->close();
} if($forgeIP){//伪造IP,将IP伪造为访问者的IP
if(Validate::isIPAddress($forgeIP)){
$ip=$forgeIP;
}else{
$ip=$_SERVER['SERVER_ADDR'];
}
$this->set_ip($ip);
} $this->ch=curl_init($url);
curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1);
$this->has_cloase=false; return$this;
} publicfunctionsetUrl($url){
curl_setopt($this->ch,CURLOPT_URL,$url); return$this;
} publicfunctionclose(){
if(!$this->has_close){
curl_close($this->ch);
$this->has_cloase=true;
}
} publicfunction__destruct(){
$this->close();
} /**
*设置页面超时时间,支持链式操作
*
*@paramtype$timeout
*@return\Common\Library\Curl
*/
publicfunctionset_time_out($timeout){
curl_setopt($this->ch,CURLOPT_TIMEOUT,intval($timeout));
return$this;
} /**
*伪造来源路径
*
*@paramtype$referer
*@return\Common\Library\Curl
*/
publicfunctionset_referer($referer){
if(!empty($referer)){
curl_setopt($this->ch,CURLOPT_REFERER,$referer);
}
return$this;
} /**
*设置cookie
*本方法仅发送cookie信息到远端,不保存远端返回的cookie信息
*
*@paramtype$cookie_file cookie文件的存储路径
*@return\Common\Library\Curl
*/
publicfunctionload_cookie($cookie_file){
$this->_checkCookie($cookie_file);
curl_setopt($this->ch,CURLOPT_COOKIEFILE,$cookie_file);
return$this;
} /**
*设置cookie
*发送cookie到远端,并保存远端返回的cookie
*
*@paramtype$cookie_file
*@return\Common\Library\Curl
*/
publicfunctioncookie($cookie_file){
$this->_checkCookie($cookie_file);
curl_setopt($this->ch,CURLOPT_COOKIEFILE,$cookie_file);
curl_setopt($this->ch,CURLOPT_COOKIEJAR,$cookie_file);
return$this;
} /**
*设置cookie
*本方法将不发送cookie信息,仅接收返回的cookie并保存
*
*@paramtype$cookie_file
*@return\Common\Library\Curl
*/
publicfunctionsave_cookie($cookie_file=""){
//设置缓存文件,例如a.txt
if(empty($cookie_file)){
$cookie_file=tempnam('./','cookie');
}
$this->_checkCookie($cookie_file);
curl_setopt($this->ch,CURLOPT_COOKIEJAR,$cookie_file);
return$this;
} privatefunction_checkCookie($cookie_file){
if(!\Think\Storage::has($cookie_file)){
\Think\Storage::put($cookie_file,'');
}
} /**
*执行curl请求
*
*@returntype
*/
publicfunctionexec(){
$str=curl_exec($this->ch);
$this->flag_if_have_run=true;
return$str;
} /**
*设置发送POST请求。没有调用过该方法默认使用GET方法提交
*
*@paramtype$postData post的数据,支持数组和“xxx=1&x=2”两种格式
*@return\Common\Library\Curl
*/
publicfunctionpost($postData){
curl_setopt($this->ch,CURLOPT_POST,1);
//echo($postQuery);die;
curl_setopt($this->ch,CURLOPT_POSTFIELDS,$postData);
return$this;
} /**
*获取curl的信息
*
*@returntype
*@throwsException
*/
publicfunctionget_info(){
if($this->flag_if_have_run==true){
returncurl_getinfo($this->ch);
}else{
thrownewException("<h1>需先运行(执行exec),再获取信息</h1>");
}
} /**
*设置代理
*
*@paramtype$proxy
*@return\Common\Library\Curl
*/
publicfunctionset_proxy($proxy){
//设置代理,例如'68.119.83.81:27977'
curl_setopt($this->ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
curl_setopt($this->ch,CURLOPT_PROXY,$proxy);
return$this;
} /**
*设置请求的IP
*
*@paramtype$ip
*@returntype
*/
publicfunctionset_ip($ip=''){
if(!empty($ip)){
curl_setopt($this->ch,CURLOPT_HTTPHEADER,array("X-FORWARDED-FOR:$ip","CLIENT-IP:$ip"));
}
return$ip;
} }
CURL PHP模拟浏览器get和post的更多相关文章
- Curl可以模拟浏览器
curl直接访问被拒绝 [22:10:00 root@C7 ~]#curl -I www.163.com HTTP/1.1 403 Forbidden Date: Wed, 24 Jun 2020 0 ...
- php中curl模拟浏览器来传输数据
cURL可以使用URL的语法模拟浏览器来传输数据, 因为它是模拟浏览器,因此它同样支持多种协议,FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以 ...
- curl模拟浏览器进行phpQuery抓取数据
报Warning: file_get_contents(http://www.dianping.com/shop/8042874) [function.file-get-contents]: fail ...
- CURL --- 命令行浏览器CURL
CURL --- 命令行浏览器CURL CURL --- 命令行浏览器 CURL? 嗯,说来话长了~~~~ 这东西现在已经是苹果机上内置的命令行工具之一了,可见其魅力之一斑 1)二话不说,先从 ...
- PHP cURL实现模拟登录与采集使用方法详解教程
来源:http://www.zjmainstay.cn/php-curl 本文将通过案例,整合浏览器工具与PHP程序,教你如何让数据 唾手可得 . 对于做过数据采集的人来说,cURL一定不会陌生.虽然 ...
- php -- php模拟浏览器访问网址
目前我所了解到的在php后台中,用php模拟浏览器访问网址的方法有两种: 第一种:模拟GET请求:file_get_contents($url) 通过php内置的 file_get_contents ...
- Linux curl 命令模拟 POST/GET 请求
Linux curl 命令模拟 POST/GET 请求 本文链接:https://blog.csdn.net/sunboy_2050/article/details/82156402 curl 命 ...
- java 接口中模拟浏览器 请求webservice 接受返回数据
使用HttpClient 所需jar:commons-codec-1.9.jar,commons-httpclient-3.1.jar try { HttpClient client = new Ht ...
- .net后台模拟浏览器get/post请求
#region 后台模拟浏览器get/post请求 /// <summary> /// 发送请求方式 /// </summary> /// <param name=&qu ...
随机推荐
- js 调试方法两种
JS的错误捕获一般有下面两种方式: 1. 异常捕获常用方法是 try/catch/ throw /finally 2. 全局捕获window.onerror 1. try/catch/throw/fi ...
- Golang bash alias 自动配置GOPATH并运行项目
BASH代码: source ~/.bash_profile; export GOPATH=$GOPATH:`cd ..; pwd`; echo -e "* GOPATH: $GOPATH ...
- C++中的static修饰的变量和函数
原文地址:http://blog.csdn.net/he3913/archive/2008/09/18/2944737.aspxC++里的静态成员函数(不能用const的原因+static在c++中的 ...
- the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf
一:开始Nginx的SSL模块 1.1 Nginx如果未开启SSL模块,配置Https时提示错误 原因也很简单,nginx缺少http_ssl_module模块,编译安装的时候带上--with-htt ...
- _bzoj1007 [HNOI2008]水平可见直线【单调栈】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1007 按斜率排序,去掉斜率相同时,截距较小的直线(即只保留该斜率下截距最大的直线).若当前直 ...
- TIME-April
一转眼四月份又过了三分之一,现在才开始计划自己的四月还真是对自己太过放松了呀!不过前一段时间都在搞学生会的五四评优答辩,索然不是我喜欢的过程,但是结果还比较令人欢喜.翻掉过去的篇章,展开新的一页. 四 ...
- DP UVALive 6506 Padovan Sequence
题目传送门 /* 题意:两行数字,相邻列一上一下,或者隔一列两行都可以,从左到右选择数字使和最大 DP:状态转移方程:dp[i][j] = max (dp[i][j], dp[1-i][j-1] + ...
- jmeter(五)集合点
集合点: 简单来理解一下,虽然我们的“性能测试”理解为“多用户并发测试”,但真正的并发是不存在的,为了更真实的实现并发这感念,我们可以在需要压力的地方设置集合点,每到输入用户名和密码登录时,所有的虚拟 ...
- 2017.5.20欢(bei)乐(ju)赛解题报告
预计分数:100+20+50=first 实际分数:20+0+10=gg 水灾(sliker.cpp/c/pas) 1000MS 64MB 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地 ...
- Android拍照得到全尺寸图片并进行压缩/拍照或者图库选择 压缩后 图片 上传
http://www.jb51.net/article/77223.htm https://www.cnblogs.com/breeze1988/p/4019510.html