当前位置:首页 > PHP > 正文内容

php curl 获取cookie

高老师9年前 (2017-07-02)PHP1678

    为了实现注册机才写的教程,为了批量注册一个网站,注册带有验证码,幸好是文本验证码,但是有session验证,于是POST必须携带cookie。代码如下。

<?php
class  AutoCurl{
 
 
private $curl;
private $strCookie;
public  $url;
public  $data;
 
 
function __construct(){
$this->curl=curl_init();
    $this->strCookie='tmp.cookie'; 
if(!file_exists('tmp.cookie')){
$ckfile = fopen("tmp.cookie", "w") or die("Unable to open file!");
fclose($ckfile);
}
 
 
 
}
 
function __destruct(){ 
curl_close($this->curl);
} 
 
 
function  CurlGet(){
 
curl_setopt($this->curl, CURLOPT_URL, $this->url);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt($this->curl, CURLOPT_BINARYTRANSFER, true) ;
//important
curl_setopt($this->curl,CURLOPT_CONNECTTIMEOUT, 5);
//save_cookie
curl_setopt($this->curl,CURLOPT_COOKIEJAR,$this->strCookie); 
    $output = curl_exec($this->curl) ;
return $output;  
}
 
 
function  CurlPost(){
 
curl_setopt($this->curl,CURLOPT_POST,1);
//send_cookie
curl_setopt($this->curl,CURLOPT_COOKIEFILE, $this->strCookie);
curl_setopt($this->curl,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl,CURLOPT_URL,$this->url);
curl_setopt($this->curl,CURLOPT_POSTFIELDS,$this->data);
$output = curl_exec($this->curl) ;
return $output;  
} 
 
 
}
 
 
 
 
//实例化一个会话
 
 
$newcurl= new AutoCurl();
 
 
//第一步GET获取Cookie和验证码
//设置登录填充的Url
$newcurl->url='http://www.jinghong.in/index.php?m=user&mod=reg';
//发送Get
$result=$newcurl->CurlGet();
//获取验证码
if(preg_match('/\<b\>(.*)\<\/b\>/U',$result, $matches)){ 
$code=$matches[1];
}
 
 
 
 
 
 
//第二步POST提交注册 
//设置登录提交的Url
$newcurl->url='http://www.jinghong.in/index.php?m=user&mod=reg&act=ok';
//设置发送POST数据
$newcurl->data=array("name" =>'a'.rand(pow(10,(6-1)), pow(10,6)-1),"sj" => "1".rand(pow(10,(11-1)), pow(10,11)-1),"email" =>rand(pow(10,(5-1)), pow(10,5)-1)."@qq.com","pass" => rand(pow(10,(7-1)), pow(10,7)-1)."qqcom","bzyzm" => $code);
 
//发送Post
$result=$newcurl->CurlPost();
 
 
 
var_dump($result);
 
 
 
?>

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/20.html

分享给朋友:

“php curl 获取cookie” 的相关文章

cookie中文乱码,GBK下

cookie中文乱码,GBK下

上家公司开发医院挂号系统,系统采用GBK编码。ajax发送的中文用户名让PHP保存为cookie出现乱码的解决方案。1.Javascript变量var user=document.getElementById('user').innerText; user=escape(u...

PHP高并发下数据库值更新的问题

PHP高并发下数据库值更新的问题

(1).创建数据库test ,创建表shop(字段id,total),商品id是1,商品总数10    (2).PHP模拟购买,商品数量大于0才能购买<?php //连接数据库 $con=mysqli_connect("192.168.2.18...

 php监听事件,php触发事件

php监听事件,php触发事件

逛公众号文章看到文章"php实现事件监听与触发的方法,你用过吗?",我就好奇了,php又不是asp.net的webform,哪里来的服务端事件监听。于是学习了一波。先看下监听类:class Event {     /** &nbs...

php arrayaccess的应用场景:配置管理器

php arrayaccess的应用场景:配置管理器

上篇文章已经讲解arrayacces的原理,现在来讲解下arrayaccess的实际应用。一个大型的互联网项目中必然会存在各种配置信息,例如多种数据库信息:mysql,tidb,mongodb,redis,某个业务模块单独的配置信息如比例,额度等等,那么该如何治理配置信息?PHP项目中大部分的框架都...

在MyISAM引擎中使用事务会怎样

在MyISAM引擎中使用事务会怎样

众所周知MyISAM引擎不支持事务,但是我只是知道不支持事务,并未测试具体的表现是什么,测试代码如下:try {     //开启事务     Db::startTrans();    &...

php爬虫执行js,php执行js

php爬虫执行js,php执行js

当我们使用php爬虫采集网站时经常会遇到内容使用ajax异步加载。一般采取的方案是PHP模拟再请求api接口获取数据,但是有时候前端js加密非常麻烦,我们需要将js的加密方法转换为php方法方便curl请求。当然通过了解我们可以通过3种方案解决。第一种:使用phpv8js扩展执行js代码。(pecl...