PHP Curl 获取数据并以 JSON 格式输出
///'$ch = curl_init($url);//n//n// 设置请求头//n$headers = [//n 'Host: api.huoshan.com',//n 'Connection: keep-alive',//n 'Cache-Control: max-age=0',//n 'Upgrade-Insecure-Requests: 1',//n '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',//n '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',//n 'Sec-Fetch-Site: none',//n 'Sec-Fetch-Mode: navigate',//n 'Sec-Fetch-User: ?1',//n 'Sec-Fetch-Dest: document',//n 'sec-ch-ua: ///'Not/A)Brand///';v=///'99///', ///'Google Chrome///';v=///'115///', ///'Chromium///';v=///'115///'',//n 'sec-ch-ua-mobile: ?0',//n 'sec-ch-ua-platform: ///'Windows///'',//n 'Accept-Encoding: gzip, deflate, br',//n 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,ga;q=0.6'//n];//n//ncurl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//ncurl_setopt($ch, CURLOPT_HEADER, true);//ncurl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);//ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//n$response = curl_exec($ch);//n//n// 设置响应头为application/json//nheader('Content-Type: application/json');//n//n// 输出JSON字符串//necho json_encode($response);//n//ncurl_close($ch);//n//n// 如果$response不是合法的JSON字符串,需要进行处理//n// 例如:使用json_decode($response, true)将其转换为数组,然后使用json_encode()函数进行编码//n// echo json_encode(json_decode($response, true));//n//n// 或者使用其他方法解析$response的内容,并将其转换为JSON格式//n// 最后使用echo输出JSON字符串//n//n// 注意:这里假设$response本身是一个合法的JSON字符串,如果不是,需要根据实际情况进行处理。//n/
原文地址: https://www.cveoy.top/t/topic/pZWT 著作权归作者所有。请勿转载和采集!