How to Use cURL to Read a File in a Variable PHP

How to Use cURL to Read a File in a Variable PHP thumbnail
The libcurl fetches files from across the Internet.

Client Uniform Resource Locator (cURL) is a command line tool and library (libcurl) for transferring files over a network. cURL supports most popular Internet protocols, including HyperText Transfer Protocol (HTTP and HTTPS), file transfer protocol (FTP), gopher, telnet and Lightweight Directory Access Protocol (LDAP). You can use libcurl in your PHP scripts to fetch Web pages, news feeds and other files over the Internet. If you don't need to save a local copy of the fetched file, fetch it directly into variable. This method is faster, neater and more efficient than creating a local file and opening it.

Instructions

    • 1

      Initialize a cURL session by calling the "curl_init" function. This function returns a cURL handle you can use with other cURL functions to configure and fetch the file. You can provide the URL (file address) to "curl_init," for example:

      $curlhandle = curl_init('http://www.whitehouse.gov/feed/blog') ;

      Alternatively, you can initialize the cURL session without passing any parameters to "curl_init," and set the URL using the "curl_setopt" function. This method is useful if you are recycling the cURL handle to fetch multiple files.

      $curlhandle = curl_init() ;
      curl_setopt($curlhandle, CURLOPT_URL, 'http://www.whitehouse.gov/feed/blog') ;

    • 2

      Set the cURL session to return the fetched content when "curl_exec" so you can assign the contents of the fetched file to a variable. Set the "CURLOPT_RETURNTRANSFER" option to "true" using the "culr_setopt" function:

      curl_setopt($curlhandle, CURLOPT_RETURNTRANSFER, true);

      If "CURLOPT_RETURNTRANSFER" is set to "false," the "curl_exec" returns a Boolean "true" or "false" to indicate whether or not the file was fetched successfully, and print the file. If you want to process the file contents (for example, pulling headlines from a news feed), you need to capture the file contents to a variable.

    • 3

      Call the "curl_exec" function to fetch the file. Pass "curl_exec" the cURL handle variable. Assign the results of the fetch to a variable. If the fetch was successful, the variable contains the contents of the file. If the fetch failed, the variable contains the value "false."

      $contents = curl_exec ($curlhandle);

    • 4

      Close the session as soon as you are done with it. If you use the same session to fetch and process several files, leave it open until you are done. If you only need to fetch a single file, close the session as soon as you get the file.

      curl_close ($curlhandle);

    • 5

      Write the code to use the file contents. No matter how you are using the file contents, whether you are printing, saving, formatting, parsing or sending it, check that the fetch was successful first. Use an if-block to check your contents variable. If the variable contains "false," the transfer failed.

      if ($contents == false) {
      echo "Fetch failed" ;
      }
      else {
      echo $contents ;
      }

Tips & Warnings

  • Set the cURL timeout option to keep cURL from waiting too long for the file. The numerical value (the third parameter to "curl_setopt") set for the "CURLOPT_TIMEOUT" options is the number of seconds to wait. Setting this value to "0" disables the timeout, causing cURL to wait forever for the file:

  • curl_setopt($curlhandle, CURLOPT_TIMEOUT, 3);

Related Searches:

References

Resources

  • Photo Credit puppy playtime with toy in yard image by Paul Retherford from Fotolia.com

Comments

You May Also Like

Related Ads

Featured