Advertisement
g6man

movie_photo.php

Aug 31st, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 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.             'posters' => [],
  20.             'arts' => [],
  21.             'others' => [],
  22.         ];
  23.  
  24.         $json = json_decode($content, true);
  25.         if (empty($json['data'])) {
  26.             throw new Exception("Can't find json data.");
  27.         }
  28.  
  29.         foreach ($json['data'] as $item) {
  30.             if (!array_key_exists('photoCategory', $item)) {
  31.                 continue;
  32.             }
  33.  
  34.             $photoCategory = (int)$item['photoCategory'];
  35.  
  36.             if (array_key_exists('fullname', $item)) {
  37.                 $photo = $item['fullname'];
  38.             } else if (array_key_exists('thumbnail', $item)) {
  39.                 $photo = $item['thumbnail'];
  40.             } else {
  41.                 continue;
  42.             }
  43.  
  44.             if ($photoCategory == 1) {
  45.                 $result['posters'][] = $photo;
  46.             } else if ($photoCategory == 2 or $photoCategory == 50) {
  47.                 $result['arts'][] = $photo;
  48.             } else {
  49.                 $result['others'][] = $photo;
  50.             }
  51.         }
  52.  
  53.         return $result;
  54.     }
  55.  
  56.     function main($movieId)
  57.     {
  58.         try {
  59.             $content = curl('http://movie.daum.net/data/movie/photo/movie/list.json?pageNo=1&pageSize=100&id=' . $movieId);
  60.             $json = [
  61.                 'data' => getJson($content),
  62.             ];
  63.         } catch (Exception $e) {
  64.             $json = [
  65.                 'error' => (string)$e,
  66.             ];
  67.         }
  68.        
  69.         header('Content-Type: application/json');
  70.         $result = json_encode($json, JSON_UNESCAPED_UNICODE);
  71.         echo $result;
  72.     }
  73.  
  74.     main(array_key_exists('id', $_GET) ? (int)$_GET['id'] : 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement