要以JSON格式输出$response,可以使用json_encode函数将$response转换为JSON字符串,然后使用header函数设置响应头为application/json,最后使用echo函数输出JSON字符串。

以下是示例代码:

$ch = curl_init($url);

// 设置请求头
$headers = [
    'Host: api.huoshan.com',
    'Connection: keep-alive',
    'Cache-Control: max-age=0',
    'Upgrade-Insecure-Requests: 1',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Sec-Fetch-Site: none',
    'Sec-Fetch-Mode: navigate',
    'Sec-Fetch-User: ?1',
    'Sec-Fetch-Dest: document',
    'sec-ch-ua: "Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"',
    'sec-ch-ua-mobile: ?0',
    'sec-ch-ua-platform: "Windows"',
    'Accept-Encoding: gzip, deflate, br',
    'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,ga;q=0.6'
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

// 设置响应头为application/json
header('Content-Type: application/json');

// 输出JSON字符串
echo json_encode($response);

curl_close($ch);

这样就可以将$response以JSON格式输出。注意,这里假设$response本身是一个合法的JSON字符串,如果不是,需要根据实际情况进行处理

$ch = curl_init$url; 设置请求头 $headers = Host apihuoshancom Connection keep-alive Cache-Control max-age=0 Upgrade-Insecure-Requests 1 User-Agent Mozilla50 W

原文地址: https://www.cveoy.top/t/topic/ihJy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录