请在此详细描述你所遇到的问题
crmeb/services/HttpService.php postRequest 无法发起 json 请求,只能默认请求
【产品名称】: 多商户系统 (PHP)
【产品版本】:例:v3.4.0
【部署方式】:inux宝塔面板
【部署环境】:线上环境
【php版本】:例:7.4
【Mysql版本】:例:5.7
【使用终端】:例:小程序/ H5 / 公众号/ PC / APP / 后台
/**
* 模拟POST发起请求
* @param $url
* @param array $data
* @param bool|array $header
* @param int $timeout
* @param bool $isJson 是否发送 JSON 请求 (默认 false)
* @return bool|string
*/
public static function postRequest($url, array $data = array(), $header = false, $timeout = 10, bool $isJson = false)
{
// 核心优化:如果是 JSON 请求
if ($isJson) {
// 1. 将数组转为 JSON 字符串
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
// 2. 处理 Header,确保 Content-Type 为 json
if (!is_array($header)) {
$header = [];
}
// 检查 Header 中是否已包含 Content-Type,避免重复
$hasContentType = false;
foreach ($header as $item) {
if (stripos($item, 'Content-Type') !== false) {
$hasContentType = true;
break;
}
}
// 如果没传 Content-Type,自动加上
if (!$hasContentType) {
$header[] = 'Content-Type: application/json; charset=UTF-8';
}
}
// 调用底层 request 方法
return self::request($url, 'post', $data, $header, $timeout);
}
