Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function curl($url)
- {
- $ua = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36';
- $curl = curl_init($url);
- curl_setopt($curl, CURLOPT_USERAGENT, $ua);
- curl_setopt($curl, CURLOPT_FAILONERROR, true);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- $content = curl_exec($curl);
- curl_close($curl);
- if (empty($content)) {
- throw new Exception("Can't get content, url: " . $url);
- }
- return $content;
- }
- function getMovieDetail($content)
- {
- $find = '<div class="movie_detail">';
- $start = strpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find movie_detail start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '<div class="wrap_bnr">', $start);
- if ($end === false) {
- throw new Exception("Can't find movie_detail end point.");
- }
- return substr($content, $start, $end - $start);
- }
- function getTitle($content)
- {
- $find = '<strong class="tit_movie">';
- $start = strpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find title start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '</strong>', $start);
- if ($end === false) {
- throw new Exception("Can't find title end point.");
- }
- return substr($content, $start, $end - $start);
- }
- function getName($content)
- {
- $find = '<em class="emph_point">';
- $start = strpos($content, $find);
- if ($start === false) {
- return null;
- }
- $start += strlen($find);
- $end = strpos($content, '</em>', $start);
- if ($end === false) {
- return null;
- }
- return substr($content, $start, $end - $start);
- }
- function getRole($content)
- {
- $find = '<span class="txt_join">';
- $start = strpos($content, $find);
- if ($start === false) {
- return null;
- }
- $start += strlen($find);
- $end = strpos($content, '</span>', $start);
- if ($end === false) {
- return null;
- }
- $span = trim(substr($content, $start, $end - $start));
- if (mb_substr($span, -2) == ' 역') {
- $span = mb_substr($span, 0, -2);
- }
- return $span;
- }
- function getPhoto($content)
- {
- $find = '<img src="';
- $start = strpos($content, $find);
- if ($start === false) {
- return null;
- }
- $start += strlen($find);
- $end = strpos($content, '"', $start);
- if ($end === false) {
- return null;
- }
- return substr($content, $start, $end - $start);
- }
- function getCast($content)
- {
- $result = [];
- $roleCount = [];
- $list = explode('class="link_join">', $content);
- foreach ($list as $item) {
- $name = getName($item);
- if (is_null($name)) {
- continue;
- }
- $role = getRole($item);
- if (is_null($role)) {
- continue;
- }
- if (array_key_exists($role, $roleCount)) {
- $roleCount[$role] += 1;
- $role = $role . ' ' . $roleCount[$role];
- } else {
- $roleCount[$role] = 1;
- }
- $photo = getPhoto($item);
- $result[] = [
- 'name' => $name,
- 'role' => $role,
- 'photo' => $photo,
- ];
- }
- return $result;
- }
- function getDirectors($content, &$result)
- {
- $directors = getCast($content);
- foreach ($directors as $item) {
- $role = $item['role'];
- if (mb_strpos($role, '감독') !== false or mb_strpos($role, '연출') !== false or mb_strpos($role, '기획') !== false) {
- $result['directors'][] = $item;
- } elseif (mb_strpos($role, '제작') !== false or mb_strpos($role, '프로듀서') !== false) {
- $result['producers'][] = $item;
- } elseif (mb_strpos($role, '극본') !== false or mb_strpos($role, '각본') !== false) {
- $result['writers'][] = $item;
- } else {
- if (!array_key_exists('ohers')) {
- $result['others'] = [];
- }
- $result['others'][] = $item;
- }
- }
- }
- function getJson($content, &$result)
- {
- $result = [
- 'roles' => [],
- 'directors' => [],
- 'producers' => [],
- 'writers' => [],
- ];
- $movieDetail = getMovieDetail($content);
- list($castContent, $directorContent) = explode('<h4 class="tit_movie">제작진</h4>', $movieDetail);
- $result['roles'] = getCast($castContent);
- getDirectors($directorContent, $result);
- return $result;
- }
- function main($programId)
- {try {
- $content = curl('https://movie.daum.net/tv/crew?tvProgramId=' . $programId);
- $json = [
- 'data' => getJson($content, $result),
- ];
- } catch (Exception $e) {
- $json = [
- 'error' => (string)$e,
- ];
- }
- header('Content-Type: application/json');
- $result = json_encode($json, JSON_UNESCAPED_UNICODE);
- echo $result;
- }
- error_reporting(E_ALL);
- main(array_key_exists('id', $_GET) ? (int)$_GET['id'] : 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement