채택완료

노래리스트를 가져올수있는 질문

https://program.kbs.co.kr/2radio/radio/trot/pc/index.html

 

방송국의 하단에 그날마다 방송되는 노래를 리스트 목록으로 보여주는데

 

혹시 이걸 가져올수있는 방법이 있을까요

 

매번 홈페이지 방문해서 복사해서 게시판에 등록하기 힘들더라구요

 

해당 노래 리스트만 가져오는 방법을 찾아야하는데 혹시 아시는분 계실까요

|

답변 1개 / 댓글 1개

채택된 답변
+20 포인트

API 로 호출되네요

 

https://kong2017.kbs.co.kr/api/mobile/select_song_list?program_code=R2024-0030,R2024-0247&rtype=jsonp&request_date=20241203&page=1&page_size=7&&callback=angular.callbacks._7

 

아래는 위 코드 기반으로 데이터 출력방법입니다.

 

Copy
<?php

// KBS 노래 리스트를 가져오기 위한 URL 생성

$url = 'https://kong2017.kbs.co.kr/api/mobile/select_song_list?' . http_build_query([

    'program_code' => 'R2024-0030,R2024-0247',

    'rtype' => 'jsonp',

    'request_date' => '20241203',

    'page' => 1,

    'page_size' => 7,

    'callback' => 'angular.callbacks._7'

]);

 

// cURL 세션 초기화

$ch = curl_init($url);

 

// cURL 옵션 설정

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 운영환경에서는 주의 필요

curl_setopt($ch, CURLOPT_HTTPHEADER, [

    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'

]);

 

// cURL 실행 및 응답 받기

$response = curl_exec($ch);

 

// cURL 오류 확인

if (curl_errno($ch)) {

    echo 'cURL 오류: ' . curl_error($ch);

    exit;

}

 

// cURL 세션 종료

curl_close($ch);

 

// Angular 콜백 래퍼 제거

$json_str = preg_replace('/^angular\.callbacks\._7\((.*)\)$/', '$1', $response);

 

// JSON 디코딩

$data = json_decode($json_str, true);

 

// JSON 파싱 오류 확인

if ($data === null) {

    echo 'JSON 파싱 오류: ' . json_last_error_msg();

    exit;

}

 

// HTML 시작

echo "<!DOCTYPE html>

<html lang='ko'>

<head>

    <meta charset='UTF-8'>

    <title>KBS 노래 리스트</title>

    <style>

        body { font-family: Arial, sans-serif; }

        table {

            width: 100%;

            border-collapse: collapse;

            margin-top: 20px;

        }

        th, td {

            border: 1px solid #ddd;

            padding: 8px;

            text-align: left;

        }

        th {

            background-color: #f2f2f2;

        }

    </style>

</head>

<body>

    <h1>KBS 노래 리스트</h1>

    <table>

        <thead>

            <tr>

                <th>제목</th>

                <th>아티스트</th>

                <th>앨범</th>

                <th>재생 시간</th>

            </tr>

        </thead>

        <tbody>";

 

// 노래 정보 테이블에 추가

if (isset($data['items']) && is_array($data['items'])) {

    foreach ($data['items'] as $song) {

        // 분:초 형식으로 시간 변환

        $minutes = floor($song['duration'] / 60);

        $seconds = $song['duration'] % 60;

        $duration = sprintf("%02d:%02d", $minutes, $seconds);

 

        echo "<tr>

                <td>" . htmlspecialchars($song['song_title']) . "</td>

                <td>" . htmlspecialchars($song['artist']) . "</td>

                <td>" . htmlspecialchars($song['album_name']) . "</td>

                <td>" . $duration . "</td>

              </tr>";

    }

} else {

    echo "<tr><td colspan='4'>노래를 찾을 수 없거나 데이터 구조가 유효하지 않습니다.</td></tr>";

}

 

// HTML 닫기

echo "</tbody>

    </table>

</body>

</html>";

?>

 

978180634_1733367343.3798.png

 

 

 

만약 응용해서, 날짜 데이터를 받아서 출력하려면

 

Copy
// 기본 날짜 설정 (오늘 날짜)

$request_date = date('Ymd');

 

// GET 파라미터로 날짜 받기

if (isset($_GET['date']) && preg_match('/^\d{8}$/', $_GET['date'])) {

    $request_date = $_GET['date'];

}

 

// KBS 노래 리스트를 가져오기 위한 URL 생성

$url = 'https://kong2017.kbs.co.kr/api/mobile/select_song_list?' . http_build_query([

    'program_code' => 'R2024-0030,R2024-0247',

    'rtype' => 'jsonp',

    'request_date' => $request_date,

    'page' => 1,

    'page_size' => 30,  // 더 많은 노래 가져오기

    'callback' => 'angular.callbacks._7'

]);

 

해당 부분 수정하시면 되며

 

파일명?date=20241204  로바꾸시면 12월 4일 데이터가 나옵니다.

답변에 대한 댓글 1개

이렇게 섬세하고 꼼꼼한 설명 감사합니다. 드디어 불필요하게 매번 복사해올 필요없고
수고가 덜었네요

답변을 작성하려면 로그인이 필요합니다.