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

php curl 获取cookie

高老师7年前 (2017-07-02)PHP1518

    为了实现注册机才写的教程,为了批量注册一个网站,注册带有验证码,幸好是文本验证码,但是有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抽象类和接口作用

【一】.抽象类假设如下场景:团队准备开发某网站,表建好了,页面设计好了.A组负责开发底层数据库操作类(DB),B组负责调用DB类.但是此时A组发生了争执,MySQL? Oracle? DB2? sqlite?到底使用什么数据库?B组.... 进入漫长的等待.解决方法:A组和B组 先定1个数据库类的模...

php非对称加密

php非对称加密

先在centos安装openssl,然后开始://生成私钥openssl genrsa -out rsa_private_key.pem 1024//生成公钥openssl rsa -in rsa_private_key.pem&...

PHP中的ArrayAccess用法详解

PHP中的ArrayAccess用法详解

php arrayaccess 官方的说法是让你能以数组的形式访问对象,对于这种php内置接口一直不太明白有什么用,坚持多看文章,终于理解,特来分享,思路不同,更易于理解。(1).创建一个学生类,并且实现arrayaccess 接口。<?php class  studen...

Thinkphp Call Stack,PHP调用栈Call Stack的获取

Thinkphp Call Stack,PHP调用栈Call Stack的获取

ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。<?php //--框架核心--Start //框架内置错误处理 function errDealWith($er...

PHP获取站点根目录,PHP获取应用根目录,cgi和cli都支持

PHP获取站点根目录,PHP获取应用根目录,cgi和cli都支持

重构框架的时候想要考虑支持下cli模式,于是参考了thinkphp的底层。/**  * 获取应用根目录  * @return string  */ public static function getRootP...

php  RabbitMQ消息队列

php RabbitMQ消息队列

(1).config.php 配置文件<?php /**  * RabbitMQ_Config  */ $config = [     'host' => ...