Class for caching/grabbing a webpage?

Discussion in 'PHP' started by alfredn, Jun 13, 2008.

  1. #1
    Do not refer me to PEAR or Curl please (or any other large libraries ;]). I'd like to know if anyone knows of or has a class that will cache a webpage and place it in a folder as needed.

    Thanks in advance
     
    alfredn, Jun 13, 2008 IP
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    I can code you a class that uses a TCP socket to connect, query, and cache at a specified location.
    Would be in the range of 30 to 45 lines of code, would that work for you?
     
    ToddMicheau, Jun 13, 2008 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    If you create it, send me a copy, i'm very intrested how you do this all in 30-45 lines :)
     
    EricBruggema, Jun 13, 2008 IP
  4. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Lol, sure, I'm going to zip it and throw the link up. And it's a total of 35 lines, not including comments on usage. I've tested this and it works on my Linux PHP 5.2.1

    I'm not sure but I think classing changes from 4 to 5. . . That and sockets need to be turned on (I think on is default. . .)

    Usage is simple:

    
    include('webCache.php');
    $WC =& new webCacher();
    $WC->cacheURL("http://www.google.com/", "test.html");
    
    PHP:
    The page is then saved in the same directory as "test.html" (headers and all, though a simple function can easily be written to remove them if not wanted), if you changed the "test.html" to "../cachedPages/newCache.php" it will be saved in that directory under that file name. Simple enough I think =]

    Let me know if there's problems: http://bubblecoder.com/webCache.zip
     
    ToddMicheau, Jun 13, 2008 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Very simple way...
    function cache($site, $relative_location)
    {
     return file_put_contents($relative_location, file_get_contents($site), LOCK_EX);
    }
    
    PHP:
    You can use a simple if to check if it already exists.
     
    Danltn, Jun 13, 2008 IP
  6. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Heh, forgot all about file_get_contents(), my mind jumped straight to sockets xD
    Geuss that's a more reasonable method than mine if you don't need to mod headers =]
     
    ToddMicheau, Jun 13, 2008 IP
  7. alfredn

    alfredn Peon

    Messages:
    1,198
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks guys but well, I probably should have been specific; im looking for something that'll save the images as well, and if possible, any links on the page that go to images (save those as well). I code myself so file_put_contents has been tried. But I appreciate it anyhow :]. I probably could write something, but tbh, I really don't want to lol.
     
    alfredn, Jun 15, 2008 IP