123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
- namespace app\common\library;
- use think\facade\Http;
- /**
- * 国密加密
- */
- class HttpUtils
- {
- /**
- * 发起POST请求
- * @param string $url 请求的URL地址
- * @param array $data 请求的POST数据
- * @param array $header 请求的头部信息
- * @return mixed
- */
- public static function post($url, $data = [], $header = [])
- {
- // 初始化cURL会话
- $ch = curl_init();
- // 将数据转换为JSON格式
- if (is_array($data)) {
- $data = json_encode($data);
- // 添加JSON头部
- if (!in_array('Content-Type: application/json', $header)) {
- $header[] = 'Content-Type: application/json';
- }
- }
- // halt($header);
- // 设置cURL选项
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // 执行cURL请求
- $response = curl_exec($ch);
- // 关闭cURL会话
- curl_close($ch);
- // halt(curl_error($ch));
- // 返回响应内容
- return $response;
- }
- }
|