当前位置:网站首页>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);
}
边栏推荐
- 【剑指Offer】50. 第一个只出现一次的字符
- Keil project, RTT cannot print after too many programs are written
- 设计电商秒杀系统
- Leetcode 324 Swing sort II [tri double pointeur] le chemin du leetcode pour l'héroding
- [mathematical modeling] fmincon() function of MATLAB nonlinear programming
- Langage C - analyse des mots
- MSCI 2022 market classification assessment
- TDD case practice
- 数学知识:求组合数 I—求组合数
- 自动化测试的生命周期是什么?
猜你喜欢

两栏布局左边图片显示部分由右边内容高度决定

Chapter II Classic synchronous exercises

机器学习4-降维技术

Matlab learning notes (6) upsample function and downsample function of MATLAB

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

MATLAB 学习笔记(6)MATLAB 的 upsample 函数和 downsample 函数
![[Chapter 2 of word tutorial series] how to set the table on each page to have a header in word](/img/1a/8416d2c48bf1ddcc45e0c5d9acf242.png)
[Chapter 2 of word tutorial series] how to set the table on each page to have a header in word

Online sql to htmltable tool

第四章 存储器管理练习

笔记
随机推荐
Design e-commerce seckill system
一文搞懂shell脚本
Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way
frameworks/base/core/res/res/values/symbols.xml:3915: error: no definition for declared symbol解决办法
frameworks/base/core/res/res/values/symbols. Xml:3915: error: no definition for declared symbol solution
入行数字IC验证后会做些什么?
IDC: Alibaba cloud ranks first in the market share of China's data governance platform in 2021
C# 面试题目_20220627记录一下
C語言-單詞分析解析
没找到实习,他总结了这些
TDD案例实战
keil工程,程序写多后,RTT不能打印
论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
【Flutter 问题系列第 71 篇】Flutter 中 Uint8List 和 Image 之间的相互转换
2022-06-28:以下golang代码输出什么?A:true;B:false;C:panic;D:编译失败。 package main import “fm
机器学习6-决策树
月薪6万,互联网“降本增效”后,这类人开始被疯抢
One card can sell tens of millions, and the business expansion is faster than that of players: you don't understand the Jianghu of star cards
Do you know all the wonderful functions of the vlookup function?
When dialogfragment's onstop is completely invisible, call disass to exit the interface and report an error. Solution