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

php curl 获取cookie

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

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

开启错误提示代码:ini_set("display_errors", "On"); error_reporting(E_ALL | E_STRICT);关闭错误提示代码:error_reporting(E_ALL ^&n...

php elasticsearch基础使用

php elasticsearch基础使用

elasticsearch的操作都是基于http协议的,已经有现成的php类库,composer安装即可。{     "require": {        &...

php迭代器返回值,php yield getReturn

php迭代器返回值,php yield getReturn

php生成器的方法getReturn获取生成器迭代完成后的返回值,当生成器迭代完成会将生成器的返回值返回,因此如果迭代器未进行迭代是获取不到值的,如果你没有return值则返回null,参考代码:<?php function G1() {    &nbs...

PHP RSA密文过长加密解密,PHP RSA证书大小自动适配,PHP RSA分段加密

PHP RSA密文过长加密解密,PHP RSA证书大小自动适配,PHP RSA分段加密

项目中需要加密超长json内容才发现rsa加密长度有限制,于是换一种思路:我们将原本需要加密的内容拆分为多个字符串,一段一段的加密,解密端也是一段一段的解密即可完成。(1).确认每次加密多少长度首先我们要知道rsa加密长度是多少,1024位的rsa能加密的长度也是1024位。那么我们一次加密多长的字...

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

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

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

thinkphp虚拟主机部署,Laravel虚拟主机部署

thinkphp虚拟主机部署,Laravel虚拟主机部署

适用于虚拟主机无法设置public目录为网站目录的虚拟主机(1).根目录创建index.php<?php //设置网站目录 $root_path = __DIR__ . DIRECTORY_SEPARATOR . 'publ...