file_get_contents() Alternative

Being new to php I was totally unaware of this but in the move from php version 4 to 5 URL file-access is disabled in the server by default. This is to prevent cross site scripting attacks or XSS attacks. My first inclination was to have my server enable this feature but then they wanted to charge me money for it. So after some google searching and modifying of some code this is what I came up with:

function getPage($url, $referer, $timeout){
if(!isset($timeout))
$timeout=30;
$curl = curl_init();
if(strstr($referer,"://")){
curl_setopt ($curl, CURLOPT_REFERER, $referer);
}
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
$html = curl_exec ($curl);
curl_close ($curl);
return $html;
}

This function will need to be included in any file that uses the file_get_contents() call. If your trying to fix a wordpress theme this will probably go into your functions.php. Now You will need replace all of your file_get_contents() with the new call:

getPage("http://YourDomain.Com", "http://google.com", "30");

The 2nd variable in the call is your referer and the 3rd variable is the timeout. I suppose you could modify this so that the referer and timeout was hardcoded too. Let me know how it works!

Cheers


About this entry