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

php curl 获取cookie

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

    为了实现注册机才写的教程,为了批量注册一个网站,注册带有验证码,幸好是文本验证码,但是有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” 的相关文章

php 获取当前执行文件,php 获取当前执行文件,php 获取当前文件

php 获取当前执行文件,php 获取当前执行文件,php 获取当前文件

php官方的超全局变量$_SERVER['PHP_SELF']也能直接获取,只不过如果url参数太多也会获取。下面提供一个方法获取/*  * 获取当前PHP文件名称  */ if (!function_exists('phpself&#...

php异步信号处理

php异步信号处理

php7.1引入了PHP异步信号处理函数pcntl_async_signals() 来处理阻塞问题。在php7之前信号处理方式有2种,第一种是基于ticks来每执行一行代码来触发执行信号监听,第二种是直接while(true){  //监听信号 }第一种方式如果某行的代码阻塞时间较长会影响...

php通过event扩展创建定时器,php毫秒级定时器

php通过event扩展创建定时器,php毫秒级定时器

PHP简单定时器可以通过pcntl_signal创建闹钟信号来实现。但是缺点很明显,性能一般,要自己实现守护进程,不支持毫秒级定时器,单进程不支持多个闹钟信号,不能跨平台运行event扩展支持的事件多,性能高。<?php //创建event配置.[空配置] $eventConfig ...

php生成器的send方法详解,php yield send

php生成器的send方法详解,php yield send

【一】.基本用法首先看看官方对send方法的解释:Sets the return value of the yield expression and resumes the generato...

php关闭浏览器继续运行

php关闭浏览器继续运行

//设置客户端断开依然运行 ignore_user_abort(true); //设置脚本不超时 set_time_limit(0); //死循环每隔1秒访问一次网址 while (true) {     sleep(1);  &nb...

php定时任务,php定时器,php定时器,php定时任务管理,php定时任务框架,php实现定时任务,php定时任务系统

php定时任务,php定时器,php定时器,php定时任务管理,php定时任务框架,php实现定时任务,php定时任务系统

        本教程使用的定时任务基于EasyTak实现,EasyTask官方参考文档:https://gitee.com/392223903/EasyTask首先我们必须明白PHP的定时器只能基于CLI命令行形式运行...