g6man

movie_cast.php

Aug 31st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.07 KB | None | 0 0
  1. <?php
  2.     function curl($url)
  3.     {
  4.         $ua = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36';
  5.         $curl = curl_init($url);
  6.         curl_setopt($curl, CURLOPT_USERAGENT, $ua);
  7.         curl_setopt($curl, CURLOPT_FAILONERROR, true);
  8.         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  9.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  10.         $output = curl_exec($curl);
  11.         curl_close($curl);
  12.  
  13.         return $output;
  14.     }
  15.  
  16.     function getJson($content)
  17.     {
  18.         $result = [
  19.             'roles' => [],
  20.             'directors' => [],
  21.             'producers' => [],
  22.             'writers' => [],
  23.         ];
  24.         $roleCount = [];
  25.  
  26.         $json = json_decode($content, true);
  27.         if (empty($json['data'])) {
  28.             throw new Exception("Can't find json data.");
  29.         }
  30.  
  31.         foreach ($json['data'] as $item) {
  32.             if (!array_key_exists('castcrew', $item)) {
  33.                 continue;
  34.             }
  35.  
  36.             $cast = $item['castcrew'];
  37.             if (!array_key_exists('castcrewCastName', $cast)) {
  38.                 continue;
  39.             }
  40.  
  41.             $roleName = $cast['castcrewCastName'];
  42.  
  43.             if (array_key_exists('nameKo', $item)) {
  44.                 $castName = $item['nameKo'];
  45.             } else if (array_key_exists('nameEn', $item)) {
  46.                 $castName = $item['nameEn'];
  47.             } else {
  48.                 continue;
  49.             }
  50.  
  51.             if (array_key_exists('photo', $item)) {
  52.                 if (array_key_exists('fullname', $item['photo'])) {
  53.                     $photo = $item['photo']['fullname'];
  54.                 } else {
  55.                     $photo = null;
  56.                 }
  57.             } else {
  58.                 $photo = null;
  59.             }
  60.  
  61.             $data = [
  62.                 'name' => $castName,
  63.                 'role' => $roleName,
  64.                 'photo' => $photo,
  65.             ];         
  66.            
  67.             if (mb_strpos($roleName, '감독') !== false or mb_strpos($roleName, '연출') !== false) {
  68.                 $result['directors'][] = $data;
  69.             } elseif (mb_strpos($roleName, '제작') !== false) {
  70.                 $result['producers'][] = $data;
  71.             } elseif (mb_strpos($roleName, '극본') !== false or mb_strpos($roleName, '각본') !== false) {
  72.                 $result['writers'][] = $data;
  73.             } elseif (mb_strpos($roleName, '주연') !== false or mb_strpos($roleName, '조연') !== false or mb_strpos($roleName, '출연') !== false or mb_strpos($roleName, '진행') !== false) {
  74.                 if (!array_key_exists('castcrewTitleKo', $cast)) {
  75.                     continue;
  76.                 }
  77.  
  78.                 $role = $cast['castcrewTitleKo'];
  79.                 if (array_key_exists($role, $roleCount)) {
  80.                     $roleCount[$role] += 1;
  81.                     $data['role'] = $role . ' ' . $roleCount[$role];
  82.                 } else {
  83.                     $roleCount[$role] = 1;
  84.                     $data['role'] = $role;
  85.                 }
  86.  
  87.                 $result['roles'][] = $data;
  88.             } else {
  89.                 if (!array_key_exists('others')) {
  90.                     $result['others'] = [];
  91.                 }
  92.                
  93.                 $result['others'][] = $data;
  94.             }
  95.         }
  96.  
  97.         return $result;
  98.     }
  99.  
  100.     function main($movieId)
  101.     {
  102.         try {
  103.             $content = curl('http://movie.daum.net/data/movie/movie_info/cast_crew.json?pageNo=1&pageSize=100&movieId=' . $movieId);
  104.             $json = [
  105.                 'data' => getJson($content),
  106.             ];
  107.         } catch (Exception $e) {
  108.             $json = [
  109.                 'error' => (string)$e,
  110.             ];
  111.         }
  112.        
  113.         header('Content-Type: application/json');
  114.         $result = json_encode($json, JSON_UNESCAPED_UNICODE);
  115.         echo $result;
  116.     }
  117.  
  118.     main(array_key_exists('id', $_GET) ? (int)$_GET['id'] : 0);
Add Comment
Please, Sign In to add comment