File_Get_Contents problem with url and variable being passed

Discussion in 'PHP' started by jeeplaw, Dec 3, 2010.

  1. #1
    I've got a tiny script that is failing on me, and i'm not sure why. Getting your run of the mill:

    <BR><B>Parse error</B>: syntax error, unexpected T_STRING in <B>/home/theriv00/domains/ccddd.com/public_html/cacheparse.php</B> on line <B>4</B><BR>

    Here's the code:

    <?php
    $n= $_GET['n'];
    $data = file_get_contents('http://webcache.googleusercontent.com/search?sourceid=navclient&ie=UTF-8&q=cache=http://www.'.$n.');
    $regex = '/snapshot of the page as it appeared on (.+?) GMT/';
    preg_match($regex,$data,$match);
    echo $match[1];
    ?>

    Why would the script be failing on line 4? (the regex entry). Something tells me it's the line above with the file_get_contents call.

    I have a program that is calling this script and is passing a domain name ($n) to the file_get_contents) call. The tricky part is that i'm having the script parse google but passing the domain name as the variable. Any ideas where I'm falling apart?

    http://www.mysite.com/cacheparser.php?n=domain.com
     
    Last edited: Dec 3, 2010
    jeeplaw, Dec 3, 2010 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Use it as this :
    $data = file_get_contents('http://webcache.googleusercontent.com/search?sourceid=navclient&ie=UTF-8&q=cache=http://www.'.$n);
     
    tvoodoo, Dec 4, 2010 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    
    //$n= $_GET['n'];
    
    $n = "yahoo.com"; // or could be http://www.yahoo.com either way is fine
    
    $data = file_get_contents("http://webcache.googleusercontent.com/search?q=cache:$n");
    
    //echo $data;
    
    $regex = '/snapshot of the page as it appeared on (.+?) GMT/';
    preg_match($regex,$data,$match);
    echo $match[1]; // 2 Dec 2010 01:42:33;
    PHP:
    EDIT: or cURL

    
    //$n= $_GET['n'];
    
    $n = "yahoo.com"; // or could be http://www.yahoo.com either way is fine
    
    $url = "http://webcache.googleusercontent.com/search?q=cache:$n";
    
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, '5');
    $data = curl_exec($ch);
    curl_close($ch);
    
    //echo $data;
    
    $regex = '/snapshot of the page as it appeared on (.+?) GMT/';
    preg_match($regex,$data,$match);
    echo $match[1]; // 2 Dec 2010 01:42:33
    PHP:
    On different servers you might get different date formats, so results could be Dec 2, 2010 01:42:33 if that's a problem lets us know.
     
    Last edited: Dec 4, 2010
    MyVodaFone, Dec 4, 2010 IP
  4. jeeplaw

    jeeplaw Well-Known Member

    Messages:
    827
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    118
    #4
    voda, thanks for the help again. It's not so much the end result, it's the in between that's teaching me. Thanks
     
    jeeplaw, Dec 6, 2010 IP
    MyVodaFone likes this.