Friday, December 2, 2011

PHP coding tips - Error URL file-access is disabled in the server configuration

From time to time, you will need to get a web page content in your PHP code and use the content to generate some other contents.

For example, you have customer address, and you want to get Google Maps geocode of the address in your PHP code, you can use following code

$url = "http://maps.google.com/maps/geo?q" . $address . "&output=xml";
$xml = file_get_contents($url);

$status = $xml->Response->Status->code;
if ($status=='200') { //address geocoded correct, show results
$pos = explode(",",$xml->Response->Placemark[0]->Point->coordinates);
$geoLat = $pos[1];
$geoLng = $pos[0];
}

Problems
Some time you will find your call to the URL failed and get no result.

If you print out the error message, you will find something like this

Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/xxxxxx/public_html/index.php on line 222


Solution
In order to run file_get_contents($url), your server need be configured to allow PHP code calling URL access. This configuration is in php.ini file, with following line set value to "on"
allow_url_fopen = On;

So the simple solution to add above line in your php.ini file if it is not there.
Or you can just create one line php.ini file and put the file under your site root.
The issue should be resolved.

Happy coding....

No comments: