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

php curl 获取cookie

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

    为了实现注册机才写的教程,为了批量注册一个网站,注册带有验证码,幸好是文本验证码,但是有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.5b1.cn/post/20.html

分享给朋友:

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

php调用 java webservice接口

php调用 java webservice接口

php调用Webservice基本语法如下:$url ='xxxxxxx.cn'  //链接服务器端 $client = new SoapClient($url);通过以上语法已经连接到webservice,也可将wsdl在本地使用,...

php 地址转换经纬度

php 地址转换经纬度

//$ak开发密钥,$cityname城市名称(支持省县乡详细地址) public  function   getposition($ak,$cityname){ $callback=array('lng'=>0,'l...

 php mysql 行锁,php mysql 行级锁,php mysql 行锁定

php mysql 行锁,php mysql 行级锁,php mysql 行锁定

应用场景:PHP模拟购买,商品数量大于0才能购买常见代码:<?php //连接数据库 $con=mysqli_connect("localhost","ihuohuo","927464cy","ihuohuo");...

PHP中的ArrayAccess用法详解

PHP中的ArrayAccess用法详解

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

composer自动加载类库(非psr4规范的文件)

composer自动加载类库(非psr4规范的文件)

在项目下的composer配置文件修改(PaySdk是我这里一个支付sdk的目录,包含各种各样的支付sdk,这样写的意思让composer自动把PaySdk下的所有文件自动加载):"autoload": {      &...

php jsonp转json,php jsonp转数组,php jsonp转对象

php jsonp转json,php jsonp转数组,php jsonp转对象

将jsonp转为PHP数组和对象。/**  * jsonp转数组|Jsonp转json  * @param string $jsonp jsonp字符串  * @param bool $as...