MovieData::fetchImdbData PHP Method

fetchImdbData() private method

private fetchImdbData ( $url )
    private function fetchImdbData($url)
    {
        // Decides what language the IMDB titles should be fetched in.
        $header[] = "Accept-Language: en-us,en";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        if (!$data) {
            throw new Exception(L::get("MOVIE_FETCH_ERROR"));
        }
        /* GENRE */
        $arrReturned = $this->matchRegex($data, '~href="/genre/(.*)(?:\\?.*)"(?:\\s+|)>(.*)</a>~Ui');
        if (count($arrReturned[1])) {
            foreach ($arrReturned[1] as $strName) {
                $arrReturn[] = trim($strName);
            }
            $arrReturn = array_slice($arrReturn, 1, 4);
            $info["genres"] = join(", ", array_unique($arrReturn));
        }
        /* Tagline */
        $info["tagline"] = '';
        if (@preg_match('!Taglines:</h4>\\s*(.*?)\\s*<!ims', $data, $match)) {
            $info["tagline"] = trim($match[1]);
        }
        /* Antal säsonger */
        preg_match("/episodes\\?season=(\\d+)/ms", $data, $matches);
        $info["seasoncount"] = 0 + $matches[1];
        if ($info["seasoncount"] == 0) {
            preg_match("/href=\"episodes#season-(\\d+)\"/ms", $data, $matches);
            $info["seasoncount"] = 0 + $matches[1];
        }
        /* Cover! */
        $info["photo"] = 0;
        if ($strReturn = $this->matchRegex($data, '~src="(.*)"\\nitemprop="image" \\/>~Ui', 1)) {
            $info["photo"] = $strReturn;
        }
        /* Rating */
        if (preg_match('!<span itemprop="ratingValue">(\\d{1,2}\\.\\d)!i', $data, $match)) {
            $info["rating"] = $match[1];
        } else {
            $info["rating"] = 0;
        }
        /* Title + Year  */
        if (@preg_match('!<title>(IMDb\\s*-\\s*)?(.*) \\((.*)(\\d{4}|\\?{4}).*\\)(.*)(\\s*-\\s*IMDb)?</title>!', $data, $match)) {
            $info["title"] = htmlspecialchars_decode($match[2]);
            if ($match[3] == "????") {
                $info["year"] = "";
            } else {
                $info["year"] = $match[4];
            }
        }
        preg_match("/<span class=\"title-extra\">(.*?)<i>\\(original title\\)<\\/i>/ms", $data, $match);
        if (strlen($match[1]) > 2) {
            $info["title"] = trim($match[1]);
        }
        /* Director */
        $info['director'] = "";
        $strContainer = $this->matchRegex($data, "~(?:Director|Directors):</h4>(.*)</div>~Uis", 1);
        $arrReturned = $this->matchRegex($strContainer, '~href="/name/nm(\\d+)/(?:.*)" itemprop=\'(?:\\w+)\'><span class="itemprop" itemprop="name">(.*)</span>~Ui');
        if (count($arrReturned[2])) {
            $arrReturn = array();
            foreach ($arrReturned[2] as $i => $strName) {
                $arrReturn[] = trim($strName);
            }
            $info['director'] = join(", ", $arrReturn);
        }
        /* Writer */
        $info['writer'] = "";
        $strContainer = $this->matchRegex($data, '~(?:Writer|Writers):</h4>(.*)</div>~Uis', 1);
        $arrReturned = $this->matchRegex($strContainer, '~href="/name/nm(\\d+)/(?:.*)" itemprop=\'(?:\\w+)\'><span class="itemprop" itemprop="name">(.*)</span>~Ui');
        if (count($arrReturned[2])) {
            $arrReturn = array();
            foreach ($arrReturned[2] as $i => $strName) {
                $arrReturn[] = trim($strName);
            }
            $info['writer'] = join(", ", $arrReturn);
        }
        $info['cast'] = "";
        $intLimit = 20;
        $arrReturned = $this->matchRegex($data, '~<span class="itemprop" itemprop="name">(.*)</span>~Ui');
        if (count($arrReturned[1])) {
            $arrReturn = array();
            foreach ($arrReturned[1] as $i => $strName) {
                if ($i >= $intLimit) {
                    break;
                }
                if (trim($strName) !== $info["title"]) {
                    $arrReturn[] = trim($strName);
                }
            }
            $arrReturn = array_slice(array_unique($arrReturn), 0, 8);
            $info['cast'] = join(", ", $arrReturn);
        }
        /* Runtime */
        $info['runtime'] = trim($this->match('/Runtime:<\\/h4>.*?([0-9]+) min.*?<\\/div>/ms', $data, 1));
        /* IMDB-ID */
        $info['imdbid'] = $this->match('/<link rel="canonical" href="http:\\/\\/www.imdb.com\\/title\\/(tt[0-9]+)\\/" \\/>/ms', $data, 1);
        return $info;
    }