PHP_cURL初始化和执行方法
2023-02-18 16:43:41 时间
这个是采集基础,最好熟悉一下
$ch = curl_init();
# 设定url和把结果返回,是否返回头部
curl_setopt($ch, CURLOPT_URL, 'http://www.baidu.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_HEADER, 1);
# cookie文件设定
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookie_file);
# 额外头部
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0'));
# 设定post
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $poststring);
# 连接、执行过期时间
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 30);
# 是否跟随301 302
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_MAXREDIRS, 10);
# refer
curl_setopt($this->ch, CURLOPT_REFERER, $refer);
# http版本和端口重用设置
curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->ch, CURLOPT_FORBID_REUSE, 1);
# 支持https
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);
# 如果需要进行毫秒超时,需要增加:
curl_setopt($this->ch, CURLOPT_NOSIGNAL, 1);
# 执行
$response = curl_exec($ch);
if(curl_errno($ch)){
curl_error($ch);
exit();
}
curl_close($ch);
相关文章
- C++基础篇之什么是数据类型
- Golang ast 的使用
- c++中的std::stod, stCPP程序说明std::stod():stof, std::stold
- php解压文件
- 堪称一站式管理平台,同时支持Linux、MySQL、Redis、MongoDB可视化管理!
- Golang语言情怀--第94期 区块链技术-centos安装部署IPFS
- Golang语言情怀--第96期 区块链技术-以太坊公链合约部署-第2节:本地合约部署
- Golang语言情怀--第97期 区块链技术-以太坊公链合约部署-第3节:合约转Go语言代码
- Golang语言情怀--第98期 区块链技术-以太坊公链合约部署-第4节:MetaMask钱包连接到本地环境
- Golang语言情怀--第99期 区块链技术-以太坊公链合约部署-第5节:部署合约到以太坊公链
- Golang语言情怀--第100期 区块链技术-以太坊公链NFT图片去中心化存储-第1节:IPFS存储方案
- Golang语言情怀--第101期 区块链技术-以太坊公链NFT图片去中心化存储-第2节:go语言实现IPFS存储demo
- Golang语言情怀--第102期 区块链技术-通过区块链技术告诉你是“斯人”还是“是人”
- Golang语言情怀--第103期 区块链技术-关于区块链中的智能合约:您想知道的一切
- Power BI模拟谷歌2022搜索排行榜
- gTasks Pro for Mac(Google任务管理器) 中文
- mongodb的安装
- 关于GO语言,这篇文章讲的很明白
- 你在开发过程中使用Git Rebase还是Git Merge?
- 痞子衡嵌入式:恩智浦i.MX RTxxx系列MCU特性那些事(2)- RT685SFVK性能实测(Dhrystone)