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 getDetailSummarize($content)
- {
- $find = '<div class="detail_summarize">';
- $start = strpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find detail_summarize start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '<script', $start);
- if ($end === false) {
- throw new Exception("Can't find detail_summarize end point.");
- }
- return substr($content, $start, $end - $start);
- }
- function getTitleYear($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.");
- }
- $title_year = trim(substr($content, $start, $end - $start));
- if (preg_match('/(.*?) \((\d{4})\)/', $title_year, $matches) == 0) {
- throw new Exception("Can't find regex point.");
- }
- $title = $matches[1];
- $year = (int)$matches[2];
- return [$title, $year];
- }
- function getOriginalTitle($content)
- {
- $find = '<span class="txt_origin">';
- $start = strpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find original_title start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '</span>', $start);
- if ($end === false) {
- throw new Exception("Can't find original_title end point.");
- }
- return trim(substr($content, $start, $end - $start));
- }
- function getRating($content)
- {
- $find = '<em class="emph_grade">';
- $start = strpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find rating start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '</em>', $start);
- if ($end === false) {
- throw new Exception("Can't find rating end point.");
- }
- return (float)substr($content, $start, $end - $start);
- }
- function getTagline($content)
- {
- $find = '<strong class="tit_copy">';
- $start = strpos($content, $find);
- if ($start === false) {
- // throw new Exception("Can't find tagline start point.");
- return '';
- }
- $start += strlen($find);
- $end = strpos($content, '</strong', $start);
- if ($end === false) {
- // throw new Exception("Can't find tagline end point.");
- return '';
- }
- return trim(str_replace(["\r", "\n", " "], " ", strip_tags(substr($content, $start, $end - $start))));
- }
- function getListMovie($content)
- {
- $find = '<dl class="list_movie list_main">';
- $start = strpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find list_movie start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '</dl>', $start);
- if ($end === false) {
- throw new Exception("Can't find list_movie end point.");
- }
- return substr($content, $start, $end - $start);
- }
- function getGenresContries($content)
- {
- $find = '<dd class="txt_main">';
- $start = strpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find genres start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '</dd>', $start);
- if ($end === false) {
- throw new Exception("Can't find genres end point.");
- }
- $genres = explode('/', str_replace(' ', '', substr($content, $start, $end - $start)));
- $find = '<dd>';
- $start = strpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find countries start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '</dd>', $start);
- if ($end === false) {
- throw new Exception("Can't find countries end point.");
- }
- $countries = explode(',', str_replace(["\n", "\t", ' '], '', substr($content, $start, $end - $start)));
- return [$genres, $countries];
- }
- function getOriginallyAvailableAt($content)
- {
- if (preg_match('/(\d{4}\.\d{2}\.\d{2})\s*개봉/u', $content, $matches) == 0) {
- return null;
- }
- return $matches[1];
- }
- function getDurationContentRating($content)
- {
- if (preg_match('/(\d+)분,(.*)<\/dd>/u', $content, $matches) == 0) {
- $duration = 0;
- $contentRating = null;
- } else {
- $duration = (int)$matches[1];
- $contentRating = trim($matches[2]);
- }
- return [$duration, $contentRating];
- }
- function getSummary($content)
- {
- $find = '<div class="desc_movie">';
- $start = strpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find summary start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '</div', $start);
- if ($end === false) {
- throw new Exception("Can't find summary end point.");
- }
- return trim(str_replace(["\r\n", "\r", "\n\n"], "\n", strip_tags(substr($content, $start, $end - $start))));
- }
- function getPosterUrl($content)
- {
- $find = '<img src="';
- $start = strpos($content, $find, $prepos);
- if ($start === false) {
- throw new Exception("Can't find poster start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '"', $start);
- if ($end === false) {
- throw new Exception("Can't find poster end point.");
- }
- return substr($content, $start, $end - $start);
- }
- function getJson($content)
- {
- $detailSummarize = getDetailSummarize($content);
- list($title, $year) = getTitleYear($detailSummarize);
- $originalTitle = getOriginalTitle($detailSummarize);
- $rating = getRating($detailSummarize);
- $tagline = getTagline($detailSummarize);
- $summary = getSummary($detailSummarize);
- $posterUrl = getPosterUrl($detailSummarize);
- $listMovie = getListMovie($detailSummarize);
- list($genres, $countries) = getGenresContries($listMovie);
- $originallyAvailableAt = getOriginallyAvailableAt($listMovie);
- list($duration, $contentRating) = getDurationContentRating($listMovie);
- $result = [
- 'title' => $title,
- 'year' => $year,
- 'original_title' => $originalTitle,
- 'rating' => $rating,
- 'tagline' => $tagline,
- 'genres' => $genres,
- 'countries' => $countries,
- 'originally_available_at' => $originallyAvailableAt,
- 'duration' => $duration,
- 'content_rating' => $contentRating,
- 'summary' => $summary,
- 'poster_url' => $posterUrl,
- ];
- return $result;
- }
- function main($movieId)
- {
- try {
- $content = curl('https://movie.daum.net/moviedb/main?movieId=' . $movieId);
- $json = [
- 'data' => getJson($content),
- ];
- } catch (Exception $e) {
- $json = [
- 'error' => (string)$e,
- ];
- }
- header('Content-Type: application/json');
- $result = json_encode($json, JSON_UNESCAPED_UNICODE);
- echo $result;
- }
- main(array_key_exists('id', $_GET) ? (int)$_GET['id'] : 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement