| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace App\Servers\Wenlv;
- use \Exception;
- /**
- * 获取博物馆信息API接口
- * @author 唐远望
- * @version 1.0
- * @date 2026-01-06
- */
- class MuseumServer
- {
- private $header;
- private $baseUrl = 'http://nb.ncha.gov.cn';
- /**
- * 构造函数
- * @throws Exception 当API密钥未设置时抛出异常
- *
- */
- public function __construct()
- {
- $this->header = [
- 'content-type:application/json;charset=UTF-8',
- 'accept:*/*',
- 'accept-encoding:gzip, deflate',
- 'accept-language:zh-CN,zh;q=0.9,en;q=0.8',
- 'cookie:Hm_lvt_352975963075889e3e294c05c9b5b506=1775198593; HMACCOUNT=958D4906DE24C8E1; Hm_lpvt_352975963075889e3e294c05c9b5b506=1775203189; JSESSIONID=F3391B6142667B4DEE48F9FD47B90056',
- 'host:nb.ncha.gov.cn',
- 'proxy-connection:keep-alive',
- 'referer:http://nb.ncha.gov.cn/museum.html?page=1&nianfen=2024&level=%E4%BA%8C%E7%BA%A7&showModel=list&order=default',
- 'user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36',
- 'x-requested-with:XMLHttpRequest',
- ];
- }
- /**
- * 尝试将接口返回内容转成 UTF-8,避免 GBK/GB18030 导致的乱码
- * @param string $str
- * @return string
- */
- private function ensureUtf8($str)
- {
- if (!is_string($str) || $str === '') {
- return $str;
- }
- // 强制检测:优先判断是否已经是 UTF-8
- $enc = mb_detect_encoding($str, ['UTF-8', 'GB18030', 'GBK', 'GB2312'], true);
- if ($enc && strtoupper($enc) !== 'UTF-8') {
- return mb_convert_encoding($str, 'UTF-8', $enc);
- }
- // 如果明显出现替换字符(常见于编码不匹配),尝试兜底从 GB18030 转码
- if (strpos($str, '�') !== false) {
- $converted = @mb_convert_encoding($str, 'UTF-8', 'GB18030');
- if (is_string($converted) && $converted !== '') {
- return $converted;
- }
- }
- return $str;
- }
- /**
- * 博物馆分页数据
- * @author 唐远望
- * @version 1.0
- * @date 2026-01-08
- */
- public function getMuseumPageData()
- {
- $url = $this->baseUrl . '/je/api/loadDataNBLNList?current=1&size=15&NB_ND=2024';
- $response = self::curl_get($url, $this->header);
- $response_data = json_decode($response, true);
- if (isset($response_data['rows']) && !empty($response_data['rows'])) {
- $page_data = [
- 'totle' => $response_data['rows']['total'],
- 'current' => $response_data['rows']['current'],
- 'pages' => $response_data['rows']['pages'],
- 'size' => $response_data['rows']['size'],
- ];
- } else {
- $page_data = [
- 'totle' => 0,
- 'current' => 0,
- 'pages' => 0,
- 'size' => 0,
- ];
- }
- return $page_data;
- }
- /**
- * 博物馆数据信息列表
- * @author 唐远望
- * @version 1.0
- * @date 2026-01-08
- */
- public function getMuseumList($page)
- {
- $url = $this->baseUrl . '/je/api/loadDataNBLNList?current='.$page.'&size=100&NB_ND=2024&NB_BWGXZ_NAME=&NB_ZLDJ_NAME=&NB_MFKF=&NB_S_NAME=&NB_SS_NAME=&NB_BWGMC=';
- $response = self::curl_get($url, $this->header);
- $response_data = json_decode($response, true);
- return $response_data;
- }
- /**
- * 获取博物馆详情
- * @author 唐远望
- * @version 1.0
- * @date 2026-01-08
- */
- public function getMuseumDetail($BWG_NB_ID)
- {
- $url = $this->baseUrl . '/je/api/loadDataNBLN?parameter={"current":1,"size":-1,"BWG_NB_ID":"' . $BWG_NB_ID . '"}';
- $response = self::curl_get($url, $this->header);
- $response_data = json_decode($response, true);
- return $response_data;
- }
- /**
- * 发送http post请求
- * @param string $url 请求地址
- * @param array $headers 请求头
- * @param json $postdata 请求数据
- * @param array $options curl配置项
- */
- public static function curl_post($url = '', $headers = '', $postdata = '', $options = array())
- {
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- curl_setopt($ch, CURLOPT_ENCODING, ''); // 添加这一行,让 cURL 自动处理压缩
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- if (!empty($headers)) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- if (!empty($options)) {
- curl_setopt_array($ch, $options);
- }
- $data = curl_exec($ch);
- return $data;
- }
- /**
- * 发送http get请求
- */
- public static function curl_get($url = '', $headers = '', $options = array())
- {
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- curl_setopt($ch, CURLOPT_ENCODING, ''); // 添加这一行,让 cURL 自动处理压缩
- if (!empty($headers)) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- if (!empty($options)) {
- curl_setopt_array($ch, $options);
- }
- $data = curl_exec($ch);
- return $data;
- }
- }
|