当前位置:网站首页>php封装curl发送get、post请求方法,并使用
php封装curl发送get、post请求方法,并使用
2022-06-25 04:00:00 【if时光重来】
参考:https://blog.csdn.net/fuchto/article/details/119913065
发送get请求
/**
* @param $url
* @param array $header
* @return bool|string
*/
function curl_get($url,$header = array()){
$curl = curl_init();
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 0);
// 超时设置,以秒为单位
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
// 超时设置,以毫秒为单位
// curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
// 设置请求头
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//执行命令
$data = curl_exec($curl);
// 显示错误信息
if (curl_error($curl)) {
print "Error: " . curl_error($curl);
} else {
// 打印返回的内容
curl_close($curl);
return $data;
}
}
// 使用
$headers = array(
"Content-Type: application/json",
"Content-Length: " . strlen($jsonStr) . "",
"Accept: application/json",
"client-id:5K8264ILTKCH16CQ2502SI8ZNMTM67VS", //自定义参数
"client-secret:C380BEC2BFD727A4B6845133519F3AD6" //自定义参数
);
curl_get('http://127.0.0.1', $headers);
发送post请求
/**
* @param $sUrl 请求地址
* @param $aData 请求参数
* @return bool|string
*/
function http_post($sUrl, $aData){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $sUrl);
// 这里是头部信息
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $aData); // Post提交的数据包
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
//curl_setopt($ch, CURLOPT_HEADER, 1); //取得返回头信息
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Linux; U; Android 4.1.1; zh-cn; MI 2S Build/JRO03L) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 XiaoMi/MiuiBrowser/1.0"); // 模拟手机请求
$sResult = curl_exec($ch);
if($sError=curl_error($ch)){
die($sError);
}
curl_close($ch);
return $sResult;
}
// 使用
$headers = array(
"Content-Type: application/json",
"Content-Length: " . strlen($jsonStr) . "",
"Accept: application/json",
"client-id:5K8264ILTKCH16CQ2502SI8ZNMTM67VS", //自定义参数
"client-secret:C380BEC2BFD727A4B6845133519F3AD6" //自定义参数
);
http_post('http://127.0.0.1', $headers);
边栏推荐
- kenlm
- numpy np tips: numpy数组的squeeze等处理
- 2020.3.3 notes async/await and promise and Then processes and threads
- GBase 8s 锁的分类
- 如何筛选出和产品相关的词,精准排除掉无效词
- 马斯克发布人形机器人,AI对马斯克为什么意义重大?
- 【Kubernetes系列】Helm的安装使用
- English Grammar - pronunciation rules
- CTF_ Web:8-bit controllable character getshell
- Coinlist how to operate the middle lot number security tutorial
猜你喜欢

文本关键词提取:ansj

How many images can opencv open?

Shutter fittedbox component
![Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding](/img/ad/69fce7cf064479a0ddd477fb935de2.png)
Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding

UCLA | generative pre training for black box optimization

Easyrecovery15 very easy to use computer data recovery software

i. Max development board learning record

Unit test coverage

Coinlist how to operate the middle lot number security tutorial

Value transfer between parent and child components of wechat applet
随机推荐
Uniapp makes mobile app programs, using uni Choosevideo record video, video playback is fuzzy, and the resolution is low
Mysql的order by
Hot and cold, sweet and sour, want to achieve success? Dengkang oral, the parent company of lengsuanling, intends to be listed on the main board of Shenzhen Stock Exchange
2020.3.3 notes async/await and promise and Then processes and threads
Failed to install redis interface
SQL, CTE, flg case problems
Coinlist queuing tutorial to improve the winning rate
GBASE 8s活锁、死锁问题的解决
"How to carry out industrial positioning" in local / Park industrial planning
LabVIEW开发气体调节器
单元测试覆盖率
"Renaissance" in the digital age? The bottom digital collection makes people happy and sad
Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'
How to draw an industry investment map
什么是存储引擎以及MySQL常见的三种数据库存储引擎
Can Navicat directly operate the Android database SQLite
Acmstreamopen return value problem
Synchronous and asynchronous functions (callback function, promise, generator, async/await)
SQL注入详解
【LeetCode】22. bracket-generating