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

php curl 获取cookie

高老师8年前 (2017-07-02)PHP1651

    为了实现注册机才写的教程,为了批量注册一个网站,注册带有验证码,幸好是文本验证码,但是有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跨域问题最佳解决方案

1、允许单个域名访问指定某域名(http://client.runoob.com)跨域访问,则只需在http://server.runoob.com/server.php文件头部添加如下代码:header('Access-Control-Allow-Origin:http://client....

php调用 java webservice接口

php调用 java webservice接口

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

 php xml字符串转数组,phpxml转数组,php 将xml转换成数组

php xml字符串转数组,phpxml转数组,php 将xml转换成数组

001源码:/*  * $xml_str是xml字符串  */ function  xmltoarray($xml_str) { //禁止XML实体扩展攻击 libxml_disable_entity_loader(true); //拒绝包含...

php多进程,php多进程处理任务,php多进程应用场景

php多进程,php多进程处理任务,php多进程应用场景

php多进程应用场景主要是非web端,fpm下是不支持多进程的,非类linux操作系统都不支持,请在cli模式使用.可以使用多进程做任务分发,批量计算,批量文件处理,批量爬虫,网络运维等等。下面看一份简单的入门demo//创建子进程 $pid=pcntl_fork(); //返回-1,创建失败,不...

php异步执行,php后台运行,如何在windows下让php后台运行

php异步执行,php后台运行,如何在windows下让php后台运行

如果想在windows中执行php,并且让php脚本在后台运行,可以用下面的cmd命令start /b php  D:\wwwroot\default\demo1\run.php例如上面的命令意思后台运行run.php,如果想用php编写异步代码: ...

PHP二维数组排序,PHP多维数组排序, array_multisort()

PHP二维数组排序,PHP多维数组排序, array_multisort()

使用php函数array_multisort()即可实现和SQL一样的order by排序. 例如我们需要对会员表按照主键降序排列,年龄升序排列://会员表数据 $list = []; $list[] = ['mid' =>&n...