Easily including a file from another site

Discussion in 'PHP' started by mickmel, Jun 23, 2007.

  1. #1
    I have a script that runs on a site completely separate from mine. When I run it, the result of that script is a number - that's it. No HTML around it or anything.

    I want to be able to drop that result into a single variable as easily as possible. Something like:

    $the_result = file("http://www.whatever.com/specialscript.php");

    Of course, doing that puts the result in an array, which I'd need to go through.

    I'm going to have to call this script on a variety of servers quite often, so I need the most efficient way to get the result back. Make sense?
     
    mickmel, Jun 23, 2007 IP
  2. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #2
    
    $myvar = file_get_contents($url);
    
    PHP:
    Does exactly what you want, gets the result of a URL and returns it as a string.
     
    mrmonster, Jun 23, 2007 IP
  3. mickmel

    mickmel Peon

    Messages:
    384
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah, that was way too easy. :)

    Thanks!
     
    mickmel, Jun 23, 2007 IP
  4. mickmel

    mickmel Peon

    Messages:
    384
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #4
    One more quick question about this. If the file isn't there (404), it comes back ugly (big error message on the page).

    How can I trap that error and display my own error message?
     
    mickmel, Jun 24, 2007 IP
  5. melol2

    melol2 Active Member

    Messages:
    511
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #5
    <?
    if (strpos($mystr, "404") === true) {
    $mystr = "There was an error";
    }
    ?>
     
    melol2, Jun 24, 2007 IP
  6. mickmel

    mickmel Peon

    Messages:
    384
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The problem is that as soon as I run file_get_contents the error appears. I could see that and show a custom error after the fact, but the mess is already on their screen. How can I prevent that?
     
    mickmel, Jun 24, 2007 IP
  7. OIOplus

    OIOplus Peon

    Messages:
    233
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can hide the error using the @ syntax:

    $myvar = @file_get_contents($url);
    Code (markup):
     
    OIOplus, Jun 24, 2007 IP
  8. mickmel

    mickmel Peon

    Messages:
    384
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #8
    That's what I was after. Thanks!
     
    mickmel, Jun 24, 2007 IP
  9. mickmel

    mickmel Peon

    Messages:
    384
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ok, now another problem. For one of them, I'm getting the following error:

    Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/xxxxxx/public_html/cron/userposts.php on line 23
    
    Warning: file_get_contents(http://www.xxx.com/file.php?userid=57) [function.file-get-contents]: failed to open stream: Success in /home/xxxxxx/public_html/cron/userposts.php on line 23
    Code (markup):
    Any ideas? The addresses are correct and I can pull them up in a browser.
     
    mickmel, Jun 26, 2007 IP