skip to Main Content

Using CURL and PHP

CURL is built-in to the usual up-to-date PHP distributions. (You may need to enable it in php.ini). CURL lets you programmatically transfer files using all the usual protocols, such as HTTP and FTP. Among other things, it lets you write PHP to simulate posting forms. In other words, it’s useful for some test scripts, pretending to be your tame user. And it is free, open-source code.

You’ll find plenty of info about CURL on the web which I won’t attempt to reproduce. A good starting point is curl.haxx.se/libcurl/php/. I’m indebted to smart-guy Richard Garbutt at altcom, for pointing me in this direction.

CURL and PHP – Setup

Assuming you already have PHP5 set up, (PHP4 probably OK too), then simply visit PHP.INI and remove the semi-colon in front of

extension=php_curl.dll

and restart Apache.

CURL Usage

This example is copied verbatim from curl.haxx.se, all credit due.

<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2&postvar3=value3");

curl_exec ($ch);
curl_close ($ch); 
?>

I was using something similar to provide form inputs to the Drupal Skills database, which I built for altcom.

Back To Top