We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17032
    • 2 Posts
    Hi everyone,

    i use file_get_contents/fopen in a bunch of snipets for interconecting sites. The problem is, that from time to time
    one of the servers goes down. When this happens the file_get_contents will generate an error and the whole modx system
    dies out. Is there a way to catch this error before it kills the site? I have tried replacing the error handle, use try...catch,
    nothing works. When the file_get_contents returns an error, the page where the snippet is used doesn’t display any more, just a big
    modx error. Till now i found no solution on google or anywhere. I’m using modx evolution. Help is appriciated ...

    Thank you.
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      When file_get_contents() fails, you have to try using curl. It’s a lot more complicated, and it’s not always compiled into PHP with all the options you want or need, but it has much more robust handling for intermittent servers... and all servers are intermittent.
        • 3749
        • 24,544 Posts
        Would it work to use:

        @file_get_contents()


        and examine $http_response_header before continuing?

        $http_response_header should still be set even when file_get_contents() fails.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 17032
          • 2 Posts
          Quote from: BobRay at Aug 27, 2010, 03:14 AM

          Would it work to use:

          @file_get_contents()


          and examine $http_response_header before continuing?

          $http_response_header should still be set even when file_get_contents() fails.

          Nope, @ doesn’t solve the problem, and another problem is that the code doesn’t continue to execute (correct me if i’m wrong).

          CURL - is an idea, i have to try it. Thank you.
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            Exactly: using @ to suppress errors doesn’t make your script retry the problematic server, it just makes it fail silently. If you need to keep pounding a server until it yields the files/pages you want, you have to use CURL.