박스오피스모조의 연간 영화 해외 수입을 긁어올 수 있습니다.



1977년부터 현재의 데이터입니다. 국내(미국)와 해외 데이터를 긁어올 수 있으며 파라미터에 긁어오고 싶은 년도를 입력합니다.


해외 데이터는 API_BoxofficeMojo_Worldwide 함수를 사용하고, 국내(미국) 데이터는 API_BoxofficeMojo_Domestic() 함수를 사용합니다.


반환 값은 두 개의 배열을 반환하는데, 하나는 제목이고, 두 번째는 데이터 값입니다.


<?php
function API_BoxofficeMojo_DOMHtml( $html )
{
  $dom = new DOMDocument();
  libxml_use_internal_errors(true);
  $dom->loadHTML($html);

  $thArray = $tdArray = $array = array();

  $th = $dom->getElementsByTagName('th');
  foreach ( $th as $th ) {
    $thArray[] = trim($th->nodeValue);
  }
  $count = count($thArray);

  $td = $dom->getElementsByTagName('td');
  $i = 0;
  foreach( $td as $td ) {
    if(count($tdArray[$i]) != $count) {
      $tdArray[$i][] = trim($td->nodeValue);
    } else {
      $i++;
      $tdArray[$i][] = trim($td->nodeValue);
    }
  }

  return array($thArray, $tdArray);
}

function API_BoxofficeMojo_call( $get )
{
  $url = 'https://www.boxofficemojo.com/' . $get;

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);

  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

  $response  = curl_exec($ch);
  curl_close($ch);

  return $response;
}

function API_BoxofficeMojo_Domestic($date)
{
  if(!preg_match("/^([1-2][0-9]{3})$/", $date))
  {
    $date = date("Y");
  }

  $table = API_BoxofficeMojo_call('year/' . $date . '/?grossesOption=calendarGrosses');
  if(preg_match('/<table.*?>(.*?)<\/[\s]*table>/si', $table, $matchs))
  {
    return API_BoxofficeMojo_DOMHtml($matchs[1]);
  }

  return array();
}

function API_BoxofficeMojo_Worldwide($date)
{
  if(!preg_match("/^([1-2][0-9]{3})$/", $date))
  {
    $date = date("Y");
  }

  $table = API_BoxofficeMojo_call('year/world/' . $date . '/');
  if(preg_match('/<table.*?>(.*?)<\/[\s]*table>/si', $table, $matchs))
  {
    return API_BoxofficeMojo_DOMHtml($matchs[1]);
  }

  return array();
}


$year = date('Y');

// 미국(국내) 연간 박스오피스
$domestic = API_BoxofficeMojo_Domestic($year);
print_r($domestic);
/*
결과:
Array (
  [0] => Array (
    [0] => Rank
    [1] => Release Group
    [2] => Worldwide
    [3] => Domestic
    [4] => %
    [5] => Foreign
    [6] => %
  )

  [1] => Array (
    [0] => Array (
      [0] => 1
      [1] => Ant-Man and the Wasp: Quantumania
      [2] => $463,805,863
      [3] => $206,777,755
      [4] => 44.6%
      [5] => $257,028,108
      [6] => 55.4%
    )

    [1] => Array (
      [0] => 2
      [1] => Creed III
      [2] => $226,744,403
      [3] => $130,444,403
      [4] => 57.5%
      [5] => $96,300,000
      [6] => 42.5%
    )
    ...
  )
)
*/

// 해외 연간 박스오피스
$worldwide = API_BoxofficeMojo_Worldwide($year);
print_r($worldwide);

?>



0 댓글