Tuesday, January 19, 2010

URL file-access is disabled in the server configuration

One of my PHP app used to send out SMS alerts using HTTP based SMS Gateway. Everything was working fine till last week when the SMS stopped coming. On running a stand alone PHP page using the same function to send SMS as the page in the application, got this error -

"Warning: fopen(): URL file-access is disabled in the server configuration"

Googling the error told me that allow_url_fopen is disable in the server configuration.. now though I agree that it is good to secure the server but doing it without informing your hosting customers is not a good practice.. anyways the workaround was found with due thanks to Luiz Miguel Axcar in the user comments sections on http://php.net/manual/en/function.fopen.php

To quote him:
If you are getting message "Warning: fopen(): URL file-access is disabled in the server configuration", you can use function below to get the content from a local or remote file.

Function uses CURL lib, follow the link to get help: http://www.php.net/curl

< ?php
/*
* @return string
* @param string $url
* @desc Return string content from a remote file
* @author Luiz Miguel Axcar (email removed)
*/

function get_content($url)
{
$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);

ob_start();

curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();

ob_end_clean();

return $string;
}

#usage:
$content = get_content ("http://www.php.net");
var_dump ($content);
?>

No comments:

Post a Comment