Author Topic: HTTP Download from website?  (Read 478 times)

0 Members and 1 Guest are viewing this topic.

Offline primetchTopic starter

  • Irregular
    • View Profile
HTTP Download from website?
« on: February 12, 2008, 10:07:37 AM »
How can a PHP-based website download pages off of other websites (news-related websites, for example) and store them in a temporary location, retrieving part of the file and displaying it within a page?

I did a quick search for HTTP get but didn't get any results. (I programmed AutoIt before PHP, if that's any help.)

Online thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
    • View Profile
Re: HTTP Download from website?
« Reply #1 on: February 12, 2008, 11:26:05 AM »
You can simply fetch files into a variable using file_get_contents().

Offline rhodesa

  • Guru
  • Freak!
  • *
  • Gender: Male
  • Bold and nosy. I'm famous for that.
    • View Profile
    • VectorLoft.com
Re: HTTP Download from website?
« Reply #2 on: February 12, 2008, 11:30:10 AM »
Pretty much all of the function for handling local files will work with remote files too. Here is an example :

Code: [Select]
<?php
  
function getTitle($url) {
    
$expire_time 15 60//15 minutes
    
$temp_file '.tmp_'.md5($url); //Make unique filename

    //Check cache
    
if(is_file($temp_file) && filemtime($temp_file) + $expire_time time())
      return 
file_get_contents($temp_file);

    
//Get contents
    
$contents file_get_contents($url);
    
    
//Find title
    
if(!preg_match("/<title>(.+)<\/title>/",$contents,$matches))
      return 
false;
      
    
//Update cache
    
file_put_contents($temp_file,$matches[1]);

    return 
$matches[1];
  }
  print 
getTitle("http://www.phpfreaks.com/forums/index.php");
?>
Aaron Rhodes
Lead Developer

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.