Advertisement
danine1

imdb

Nov 3rd, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.90 KB | None | 0 0
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', true);
  5.  
  6. define('CACHE_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cache');
  7.  
  8.  
  9. function download_from_internet($url)
  10. {
  11.     $ch = curl_init();
  12.     curl_setopt($ch, CURLOPT_URL, $url);
  13.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  15.     curl_setopt($ch, CURLOPT_HTTPGET, true);
  16. //  curl_setopt($ch, CURLOPT_COOKIEFILE, CACHE_DIR . DIRECTORY_SEPARATOR . 'cookies.txt');
  17. //  curl_setopt($ch, CURLOPT_COOKIEJAR, CACHE_DIR . DIRECTORY_SEPARATOR . 'cookies.txt');
  18. //  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
  19.     $source = curl_exec ($ch);
  20.  
  21.     return $source;
  22. }
  23.  
  24.  
  25. function download_with_cache($url, $ttl = 86400)
  26. {
  27.     $cache_file = CACHE_DIR . DIRECTORY_SEPARATOR . strtr($url, ':/?|#', '.....').'xxx';
  28.  
  29.  
  30.     if (!is_dir(CACHE_DIR)) {
  31.         mkdir(CACHE_DIR, 0755);
  32.     }
  33.  
  34.     // these lines will check and create a cache directory if there is none
  35.     if (
  36.         is_file($cache_file)
  37.         &&
  38.         filemtime($cache_file) > time() - $ttl)
  39.     {
  40.         $contents = file_get_contents($cache_file);
  41.     } else { //
  42.         $contents = download_from_internet($url);
  43.         file_put_contents($cache_file, $contents);
  44.     }
  45.  
  46.     return $contents;
  47. }
  48.  
  49.  
  50. $url = "http://www.imdb.com/genre/";
  51.  
  52. $contents = download_with_cache($url);
  53.  
  54. header('Content-type: text/plain');
  55.  
  56. $contents = download_with_cache($url);
  57.  
  58. $contents = substr($contents, strpos($contents, '<table class="genre-table"'));//goes to imdb and targets the table
  59. $contents = substr($contents, 0, strpos($contents, '</table'));
  60. $contents = strip_tags($contents, '<a>');
  61. foreach (explode("\n", $contents) as $line) {
  62.     if (strpos($line, '&#xBB;') !== false) {
  63.         echo "'" , trim(str_replace('&#xBB;', '', strip_tags($line))) , "',\n";
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement