当前位置:网站首页>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);
边栏推荐
- PHP code audit 1 - php Ini
- Retrofit 源码分析
- 马斯克发布人形机器人,AI对马斯克为什么意义重大?
- CMD operation MySQL in Windows
- NFT insider 63: the sandbox reached a cooperation with Time magazine, and YGG established Spain's subdao
- Anaconda安装+TensorFlow安装+Keras安装+numpy安装(包含镜像和版本信息兼容问题)
- 【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。
- Laravel document sorting 11. System architecture
- UCLA | 用于黑盒优化的生成式预训练
- i. Max development board learning record
猜你喜欢

Anaconda安装+TensorFlow安装+Keras安装+numpy安装(包含镜像和版本信息兼容问题)

UCLA | generative pre training for black box optimization

Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand

【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。

How to draw an industry investment map

Lecture record: new application of inertial navigation - inertial measurement

Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'

CTF_ Web: Advanced questions of attack and defense world expert zone WP (1-4)

Value transfer between parent and child components of wechat applet

如何绘制产业招商地图
随机推荐
[openwrt] we recommend a domestically developed version of openwrt, an introduction to istoreos. It is very easy to use. It is mainly optimized. It solves the problem of Sinicization.
Anaconda installation +tensorflow installation +keras installation +numpy installation (including image and version information compatibility issues)
1、项目第二阶段——用户注册和登陆
【LeetCode】148. Sort linked list
Vigilance against over range collection of privacy - ten mobile app violations
Error 1062 is reported during MySQL insertion, but I do not have this field.
SQL注入详解
Laravel document sorting 10. Request life cycle
Numpy NP tips: use OpenCV to interpolate and zoom the array to a fixed shape cv2 resize(res, dsize=(64, 64), interpolation=cv2. INTER_ CUBIC)
How much do you know about the use value of WMS warehouse management system
1280_ C language to find the average value of two unsigned integer
Failed to install redis interface
Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding
Lecture record: data processing methods and applications of various spatial geodetic techniques
Smart contract learning materials
"Renaissance" in the digital age? The bottom digital collection makes people happy and sad
GBASE 8s的多线程结构
Musk released humanoid robot. Why is AI significant to musk?
Retrofit source code analysis
Unit test coverage