|
@@ -28,3 +28,51 @@ function getPlatform(): ?string
|
|
|
}
|
|
|
return $value;
|
|
|
}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 使用curl函数库发送请求
|
|
|
+ */
|
|
|
+function curl_request($url, $method = 'get', $headers = [], $params = [], $type = 'http')
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 初始化curl,返回资源
|
|
|
+ */
|
|
|
+ $curl = curl_init($url);
|
|
|
+ /**
|
|
|
+ * 默认是get请求,如果是post/put请求,设置请求方式和请求参数
|
|
|
+ */
|
|
|
+ if (strtoupper($method) == 'POST') {
|
|
|
+ curl_setopt($curl, CURLOPT_POST, true);
|
|
|
+ curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (strtoupper($method) == 'PUT') {
|
|
|
+ curl_setopt($curl, CURLOPT_PUT, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($headers)) {
|
|
|
+ curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 如果是HTTPS协议,禁止从服务器验证本地证书
|
|
|
+ */
|
|
|
+ if (strtoupper($type) == 'HTTPS') {
|
|
|
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 发送请求,返回结果
|
|
|
+ */
|
|
|
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
|
+ /**
|
|
|
+ * 选择地址
|
|
|
+ * curl_setopt($curl,CURLOPT_URL,$url);
|
|
|
+ */
|
|
|
+ $res = curl_exec($curl);
|
|
|
+ /**
|
|
|
+ * 关闭请求
|
|
|
+ */
|
|
|
+ curl_close($curl);
|
|
|
+ return $res;
|
|
|
+
|
|
|
+}
|
|
|
+
|