Loading a remote file with cURL (PHP or Scriptol)

cURL is a library for transferring files over the Internet or other network, to send or receive them under all existing protocols (http, ftp, ldap).

Is an alternative to fsockopen. It is implemented in PHP and usable in C and C++ too.

  1. Testing the availability of a curl on web hosting
  2. Enabling cURL in XAMPP
  3. Using cURL avec Scriptol
  4. Using cURL with PHP
  5. Using cURL with a RSS reader

1) Testing the availability of cURL on a web hosting

Phpinfo() displays active modules on a server.
Install the following script on your hosting:

<?php  echo phpinfo();?>

If cURL is enabled, a table like this will be displayed:

2) Enabling cURL in a local server (XAMPP for example)

cURL is not enabled by default in PHP, it must be done manually. The menu has a command to do this, but it acts only on the configuration of PHP only and not in the Apache directory.
On Xampp we must change the file PHP.INI in both:

c:\xampp\php\php5.3.0\php.ini
c:\xampp\apache\Apache2.2.11\bin\php.ini 

The following line must be uncommented in both files:

extension=php_curl.dll 

Check that you have the libeay32.dll and ssleay32.dll modules.

3) Using cURL with Scriptol

To use cURL with the scriptol version which compiles in PHP, a definition file of its variables and functions must be included. This interface to the PHP functions is available in the archive (see at bottom of page):

extern
dyn curl_init(cstring curlopt = null)
boolean curl_setopt(dyn, int, dyn)
dyn curl_exec(dyn)
void curl_close(dyn)
array curl_version()
constant int CURLOPT_URL // pass URL
constant int CURLOPT_CONNECTTIMEOUT // limit waiting time forever
constant int CURLOPT_UPLOAD // send file
/extern

There are many other variables that you have to define yourselves according to your needs with lines as this:

constant int CURLOPT_xxx

Two scripts are included in the archive:

curl-check.sol: checks the availability of cURL, displays the version and features.
curl-demo.sol: reads an RSS feed on a remote server.

4) Using cURL with PHP

We use cURL by opening a session with curl_init() and indicating the type of transaction to achieve with curl_setopt().

curl_exec () starts the execution and the session is closed with curl_close().

The PHP manual describes with examples the use of cURL and shows all possible options.

5) Using cURL with a RSS reader

As it appeared in the forum, sometimes the server in a shared hosting does not allow access to another site, and if we want to integrate an RSS feed, which is impossible with DOMDocument-> load or fsockopen becomes possible with cURL.

We replace this line in the function RSS_retrieve:

$doc->load($url);

by :

$hnd = curl_init();
curl_setopt($hnd, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($hnd, CURLOPT_URL, $url);
$data = curl_exec($hnd);
curl_close($hnd); $doc->loadXML($data);

See the RSS reader.

Download the cURL interface and demo.