|
楼主 |
发表于 2023-3-31 21:42
|
显示全部楼层
- <?php
- // 设置错误报告级别为0,不报告任何错误
- error_reporting(0);
- // 设置响应的内容类型为XML,并指定字符集为UTF-8
- header('Content-Type: text/xml;charset=UTF-8', true, 200);
- // 频道信息的数组,键为频道名称,值为频道ID
- $channels = array(
- 'BBC Radio 1' => 'p00fzl8v',
- 'BBC Radio 1Xtra' => 'p00fzl9b',
- 'BBC Radio 2' => 'p00fzl86',
- 'BBC Radio 3' => 'p00fzl8q',
- 'BBC Radio 4' => 'p00fzl8j',
- 'BBC Radio 4 Extra' => 'p00fzl9g',
- 'BBC Radio 5 Live' => 'p00fzl94',
- 'BBC Radio 5 Live Sports Extra' => 'p00fzl9z',
- 'BBC Radio 6 Music' => 'p00fzl89',
- 'BBC Asian Network' => 'p00fzl9m',
- 'BBC World Service' => 'p00fzl9p'
- );
- // XML文档的根元素名称,此处为“tv”
- $xml_root_element_name = 'tv';
- // 初始化XML文档对象
- $xml = new SimpleXMLElement('<'.$xml_root_element_name.'/>');
- // 遍历所有频道和日期,将电子节目指南信息添加到XML文档对象中
- foreach ($channels as $channel => $id) {
- for ($i = 0; $i < 7; $i++) {
- $date = date("Y/m/d", strtotime("+" . $i . " day"));
- $result = getepg($id, $date);
- $starttime = json_decode(explode("|", $result)[0]);
- $endtime = json_decode(explode("|", $result)[1]);
- $titles = json_decode(explode("|", $result)[2]);
- $descs = json_decode(explode("|", $result)[3]);
- for ($j = 0; $j < count($starttime); $j++) {
- // 创建XML元素并设置属性和子元素
- $programme = $xml->addChild('programme');
- $programme->addAttribute('start', $starttime[$j].' +0800');
- $programme->addAttribute('stop', $endtime[$j].' +0800');
- $programme->addAttribute('channel', $channel);
- $title = $programme->addChild('title', $titles[$j]);
- $title->addAttribute('lang', 'en');
- $desc = $programme->addChild('desc', $descs[$j]);
- $desc->addAttribute('lang', 'en');
- }
- }
- }
- // 将XML文档对象输出为字符串
- $re = $xml->asXML();
- // 将XML字符串保存到文件中
- $xml_file = 'epg.xml';
- file_put_contents(__DIR__ . '/' . $xml_file, $re);
- // 输出XML字符串
- print_r($re);
- function getepg($id, $date)
- {
- $url = "https://www.bbc.co.uk/schedules/" . $id . "/" . $date . "?utcoffset=%2B08%3A00";
- $header = array(
- 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0',
- 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
- 'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
- 'Accept-Encoding: gzip, deflate, br',
- 'Connection: keep-alive',
- 'Referer: https://www.bbc.co.uk/schedules/' . $id,
- );
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate, br');
- $result = curl_exec($ch);
- curl_close($ch);
- $result = str_replace('@', '', '{"@context":"' . explode('</script>', explode('{"@context":"', $result)[1])[0]);
- $result = json_decode($result)->graph;
- for ($i = 0; $i < count($result); $i++) {
- $starttime[] = date("YmdHis", strtotime(str_replace(['-', 'T', ':'], '', explode('+', $result[$i]->publication->startDate)[0]) . ' +0800'));
- $endtime[] = date("YmdHis", strtotime(str_replace(['-', 'T', ':'], '', explode('+', $result[$i]->publication->endDate)[0]) . ' +0800'));
- $titles[] = $result[$i]->partOfSeries->name;
- $descs[] = $result[$i]->description;
- }
- return json_encode($starttime) . "|" . json_encode($endtime) . "|" . json_encode($titles) . "|" . json_encode($descs);
- }
复制代码 |
|