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, 'class="episode_info">', $start);
- if ($end === false) {
- throw new Exception("Can't find detail_summarize 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 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 getGenres($content)
- {
- $find = '<dd class="f_l">';
- $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.");
- }
- return explode('/', str_replace(' ', '', substr($content, $start, $end - $start)));
- }
- function getStudio($content)
- {
- $find = '<em class="emph_g">';
- $start = strpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find studio start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '</em>', $start);
- if ($end === false) {
- throw new Exception("Can't find studio end point.");
- }
- return trim(substr($content, $start, $end - $start));
- }
- function getOriginallyAvailableAt($content)
- {
- if (preg_match('/(\d{4}\.\d{2}\.\d{2})~/', $content, $matches) == 0) {
- // throw new Exception("Can't find originally_available_at point.");
- return null;
- }
- return $matches[1];
- }
- 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);
- $title = getTitle($detailSummarize);
- $rating = getRating($detailSummarize);
- $genres = getGenres($detailSummarize);
- $studio = getStudio($detailSummarize);
- $originallyAvailableAt = getOriginallyAvailableAt($detailSummarize);
- $summary = getSummary($detailSummarize);
- $posterUrl = getPosterUrl($detailSummarize);
- $result = [
- 'title' => $title,
- 'original_title' => '',
- 'rating' => $rating,
- 'genres' => $genres,
- 'studio' => $studio,
- 'originally_available_at' => $originallyAvailableAt,
- 'summary' => $summary,
- 'poster_url' => $posterUrl,
- ];
- return $result;
- }
- function main($programId)
- {
- try {
- $content = curl('https://movie.daum.net/tv/main?tvProgramId=' . $programId);
- $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