当前位置:网站首页>PHP利用CURL实现登录网站后下载Excel文件
PHP利用CURL实现登录网站后下载Excel文件
2022-06-28 23:21:00 【supramolecular】
PHP利用curl实现登录,保存cookie到文件
<?php
header("Content-Type:text/html;charset=utf-8");
$curl = curl_init();
//$cookie_jar = tempnam('./tmp','cookie'); //tempnam--建立一个具有唯一文件名的文件
curl_setopt($curl, CURLOPT_URL,'https://www.baidu.com');//这里写上处理登录的界面
curl_setopt($curl, CURLOPT_POST, 1); //post提交数据
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER , 0); //post提交数据
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST , 0); //post提交数据
$request = 'name=****&password=*****';
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);//传递数据
curl_setopt($curl, CURLOPT_COOKIEJAR, "a.txt");//把返回来的cookie信息保存在$cookie_jar文件中
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设定返回的数据是否自动显示
curl_setopt($curl, CURLOPT_HEADER, false);//设定是否显示头信息
curl_setopt($curl, CURLOPT_NOBODY, false);//设定是否输出页面内容
curl_exec($curl);//返回结果
if(curl_exec($curl) === false)
{
echo 'Curl error: ' . curl_error($curl);
}
else
{
echo '操作完成没有任何错误';
}
curl_close($curl); //关闭*/PHP利用cookie文件实现需要登录的情况下get下载EXcel
<?php
header("Content-Type:text/html;charset=utf-8");
$cookie_jar = dirname(__FILE__) . "/a.txt"; //保存的cookie信息文件路径
$url = "https://www.baidu.com/filepath";
$arr = curl_get($url, $cookie_jar);
/**
*curl post请求
*/
function curl_post($url, $data, $cookie_jar)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$return = curl_exec($ch);
curl_close($ch);
}
/**
*curl get请求
*/
function curl_get($url, $cookie_jar)
{
$curl2 = curl_init();
curl_setopt($curl2, CURLOPT_URL, $url);//登陆后要从哪个页面获取信息
curl_setopt($curl2, CURLOPT_HEADER, false);
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl2, CURLOPT_SSL_VERIFYPEER , 0); //post提交数据
curl_setopt($curl2, CURLOPT_SSL_VERIFYHOST , 0); //post提交数据
curl_setopt($curl2, CURLOPT_COOKIEFILE, $cookie_jar);
$content = curl_exec($curl2);
file_put_contents("a.xlsx", $content);
if(curl_exec($curl2) === false)
{
echo 'Curl error: ' . curl_error($curl2);
}
else
{
echo '操作完成没有任何错误';
}
curl_close($curl2);
}
边栏推荐
- LeCun预言AGI:大模型和强化学习都是斜道!我的世界模型才是新路
- 【OpenCV】—线性滤波:方框滤波、均值滤波、高斯滤波
- 云计算的迷路者
- 见丰知夏|国漫鼻祖丰子恺,数字藏品独家发售
- Count the number of arrays with pointers
- Flutter obtains the coordinate size of any element in the interface through globalkey
- 论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
- C language - word analysis
- Didn't find an internship. He summed it up
- 数学知识:求组合数 I—求组合数
猜你喜欢

Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way

The picture display on the left of the two column layout is determined by the content height on the right

CS5463代码模块解析(包含下载链接)

Go language - reflect

Keil project, RTT cannot print after too many programs are written

深入虚拟内存(Virtual Memory,VM)

Wechat red envelope cover making tutorial and use guide with link jump

Powerful open source API interface visual management platform Yapi

IDC: Alibaba cloud ranks first in the market share of China's data governance platform in 2021

C# 面试题目_20220627记录一下
随机推荐
CS5463代码模块解析(包含下载链接)
Yyds dry inventory solution sword finger offer: maximum sum of continuous subarrays (II)
MSCI 2022 market classification assessment
Finally, someone explained the cloud native architecture
融云通信解决方案 破解企业沟通痛点
设计电商秒杀系统
Langage C - analyse des mots
云计算的迷路者
Prometeus 2.36.0 new features
MATLAB 学习笔记(6)MATLAB 的 upsample 函数和 downsample 函数
油猴脚本学习
Use conditional breakpoints in vscode (based on GDB)
小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现
复杂嵌套的对象池(4)——管理多类实例和多阶段实例的对象池
【Word 教程系列第 1 篇】如何去除 Word 表格中的箭头
Non scientific class! The road of self-study!
Would like to ask, how to open a stock account? Is it safe to open an account online?
Keil project, RTT cannot print after too many programs are written
C language - word analysis
论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》