Using cURL instead of file functions

By | April 15, 2010

Some hosts don’t allow the possibility of using the file functions that are built into PHP.

A URL can be used as a filename with this function if the fopen wrappers have been enabled.

So, if the fopen wrappers are turned off, you have no chance of using an external URL to get information (an example would be to get shipping rates from a 3rd party in order to calculate the correct postage).

So, I’m going to show you how I updated a clients codebase to use cURL…it’s really easy.


The non-working portion of code is this;

[php]$url = “$frompcode&Destination_Postcode=$topcode&Country=$dest_country&Weight=$sweight&Service_Type=AIR&Height=$sheight&Width=$swidth&Length=$slength&Quantity=$shipping_num_boxes”;
$myfile = file($url);
foreach($myfile as $vals)
$bits = split(“=”, $vals);
$$bits[0] = $bits[1];

This is basically setting some parameters and requesting a file from AUS POST (an Australian Postage Company). The file contains text which delivers the postage rate to be charged, and some other info that we don’t need.

However because my clients host has the fopen wrappers turned off this was producing an error a bit like this;

Warning: file() [function.file]: URL file-access is disabled in the server configuration in public_html/includes/modules/shipping/auspost.php on line 68

I tried a couple of ways to get around this using htaccess or php.ini, but neither of these worked. So my only option was to recode the problem area of the module to make it use cURL.

You can check if cURL is installed on your server by using the server page in your osCommerce Admin Area. As I had not used cURL before, I spent a little familiaring myself with it, and then just jumped into the code feet-first. With a little tinkering I got it to work.


You can read more about cURL here. The basic idea is to open up a connection from your site to another, grab the needed values and then close the connection.

Code Changes

Step 1. Initialize the cURL session

[php]$my_curl = curl_init();[/php]

This is easy enough to understand, I’d have thought.

Step 2. Set the URL of the page to be opened

[php]curl_setopt($my_curl, CURLOPT_URL, ‘’ . $frompcode . ‘&Destination_Postcode=’. $topcode .’&Country=’. $dest_country .’&Weight=’. $sweight .’&Service_Type=AIR&Height=’. $sheight .’&Width=’. $swidth .’&Length=’. $slength .’&Quantity=’. $shipping_num_boxes);[/php]

Again, this is easy enough. It’s the same code as the problematic codebase – but set out slightly differently as I prefer my code like this.

Step 3. Ask cURL to return the results in a variable

[php]curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, 1);
$my_contents = curl_exec($my_curl);[/php]

At this point, $my_contents consists of the text that is returned by Aus Post. Which is problematic, as we need to get it into an array for useable purposes! But first, let’s close the cURL connection;

Step 4. Close the connection

[php]curl_close ($my_curl);[/php]

Step 5. Get the $my_contents into an array

[php]$my_contents = explode(” “, $my_contents);[/php]

Here we are “exploding” the text in the variable using a space.

Step 6. Use the individual array pieces as needed

[php]foreach($my_contents as $vals)
$bits = split(“=”, $vals);
$$bits[0] = $bits[1];

This was part of the standard codebase, but I have made it use $my_contents to work with.

So, the full new cURL code goes like this:

[php]$my_curl = curl_init();
curl_setopt($my_curl, CURLOPT_URL, ‘’ . $frompcode . ‘&Destination_Postcode=’. $topcode .’&Country=’. $dest_country .’&Weight=’. $sweight .’&Service_Type=AIR&Height=’. $sheight .’&Width=’. $swidth .’&Length=’. $slength .’&Quantity=’. $shipping_num_boxes);
curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, 1);
$my_contents= curl_exec($my_curl);
curl_close ($my_curl);

$my_contents= explode(” “, $my_contents);
foreach($my_contents as $vals)
$bits = split(“=”, $vals);
$$bits[0] = $bits[1];

I hope that this made some sort of sense and will help anyone in the future. There are lots of cURL tutorials available if you want more detailed information. Good luck!

Please note, that I am nowhere near knowledgable on cURL, and this post is just to show that it can be done, and done quite easily by anyone with just a little PHP knowledge and the ability to search Google.

Leave a Reply

Your email address will not be published.