g6man

movie_search.php

Aug 31st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.65 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 getPageData($url, &$result)
  17.     {
  18.         $content = curl($url);
  19.         $json = json_decode($content, true);
  20.         foreach ($json['data'] as $item) {
  21.             $year = $item['prodYear'];
  22.             $id = $item['movieId'];
  23.             $title = strip_tags($item['titleKo']);
  24.  
  25.             $result[] = [
  26.                 'year' => (string)$year,
  27.                 'id' => (string)$id,
  28.                 'title' => $title,
  29.             ];
  30.         }
  31.  
  32.         return $json;
  33.     }
  34.  
  35.     function getJson($mediaName)
  36.     {
  37.         $result = [];
  38.         $url = 'http://movie.daum.net/data/movie/search/v2/movie.json?size=20&start=1&searchText=' . urlencode($mediaName);
  39.         $json = getPageData($url, $result);
  40.         $last_page = $json['page']['last'];
  41.         for ($page = 2; $page <= $last_page; ++$page) {
  42.             $prev_page = $page - 1;
  43.             $url = str_replace('&start=' . $prev_page, '&start=' . $page, $url);
  44.             getPageData($url, $result);
  45.         }
  46.  
  47.         return $result;
  48.     }
  49.  
  50.     function main($mediaName)
  51.     {
  52.         try {
  53.             $json = [
  54.                 'data' => getJson($mediaName),
  55.             ];
  56.         } catch (Exception $e) {
  57.             $json = [
  58.                 'error' => (string)$e,
  59.             ];
  60.         }
  61.        
  62.         header('Content-Type: application/json');
  63.         $result = json_encode($json, JSON_UNESCAPED_UNICODE);
  64.         echo $result;
  65.     }
  66.  
  67.     main(array_key_exists('name', $_GET) ? $_GET['name'] : '');
Add Comment
Please, Sign In to add comment