Bookmark and Share
Check Google Page Rank Locations of visitors to this page
Click to get Free offers

Wednesday, November 12, 2008

Switch from file get contents to curl


file_get_contents() is deprecated in favor of using the CURL libraries. You will occasionally run accross old code that uses the file_get_contents() that you want to use on servers with the file_get_contents functionality disabled. This shows how to convert from that function to the curl functions.
file_get_contents code

$data = file_get_contents($remoteurl);
curl code

//Initialize the Curl session
$ch = curl_init();

//Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Set the URL
curl_setopt($ch, CURLOPT_URL, $URL);
//Execute the fetch
$data = curl_exec($ch);
//Close the connection

//$data now contains the contents of $URL
View blog reactions

No comments: