Quick Tip: An Alternative To file_get_contents

One of the more common PHP functions developers use in order to make remote requests is file_get_contents. The function makes it easy to send requests to another URL and then handle the response.

It’s especially handy if you need to make an Ajax request to another site but aren’t allowed to do so from the client-side because of cross-site-scripting.

That said, cURL is often a better option than file_get_contents because it gives you far more control over the options that are set when setting up the request to be made. file_get_contents simply doesn’t do that. Instead, it provides enough options for making simple GET requests.

No, not that kind of curl.
No, not that kind of curl.

For these reasons, it’s often a good idea to use cURL when you need to provide a more fine-tuned request. Even more so, it’s helpful to have all of the functionality wrapped up in a utility function you can drop in your project whenever you need it.

Alternative To file_get_contents

Given all of the above, here’s a very basic function you can be use in place of file_get_contents that uses cURL to complete the request as in this gist;

Notice in the code above, there aren’t many options that are being specified for cURL. It’s harder to anticipate exactly what you may need given your project, so the function is left as simple as possible so you can add (or even remove) the options that you don’t need to further refine your request.

For more information about either of the functions listed above, I recommend checking out the PHP manual for:

If there’s anything you’d add to the code above, don’t hesitate to do so in the comments.

If you're reading this in an RSS reader, you can view the source code by clicking on the 'gist' link.

11 Replies to “Quick Tip: An Alternative To file_get_contents”

  1. If you’re doing this in a plugin or theme for general use, you’d probably want to use the WordPress HTTP API instead of calling cURL directly.

    `

    $response = wp_remote_get( ‘https://api.github.com/users/blobaugh’ );

    $body = wp_remote_retrieve_body( $response );

    `

    You can still specify quite a few options if needed, and WordPress will work around lots of edge cases, including if cURL is not installed on the server.

      1. But then I wouldn’t have had the pleasure of commenting! Or maybe I’d say what I should say – thanks for keeping all the options fresh in our minds, even when we’ve forgotten what you showed us before.

  2. The reason I stopped using file_get_contents for remote files is I found out the hard way that some installs of PHP block file_get_contents for remote URLs. The same holds true forfopen and it’s related functions.

    I can never remember what the cURL options are or when to use each. In WordPress, I leverage the HTTP API. Outside of WP, I like using Guzzle to handle the low level cURL.

  3. In the old days when APIs only offered XML responses I used simplexml_load_file (it’s till handy for xml-only responses) but now every API returns json so curl is my method of choice.

    Note: setting CURLOPT_RETURNTRANSFER to true is important to ensure the raw output is returned. Else the curl_exec() will simply return true/false on success/failure.

    So you can do:

    $url = ‘http://api.json.com’;

    $curl_request = curl_init( $url );

    curl_setopt( $curl_request, CURLOPT_RETURNTRANSFER, true );

    $json = curl_exec( $curl_request );

    // parse the json

  4. Hi

    file_get_contents & fopen still have their place, but only for dealing with files on the local server. That’s where they should be left.

    Any remote stuff, then invariably via cUrl. Occasionally stream sockets if the connection requires it.

    Stephen

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.