Specifications
The only things that change with the application are the URL that you connect to and the para-
meters you set with curl_opt(). There are a large number of these that can be set.
Some typical applications of cURL are
• Downloading pages from a server that uses HTTPS (because fopen() can’t be used for
this purpose)
• Connecting to a script that normally expects data from an HTML form using POST
• Writing a script to send multiple sets of test data to your scripts and checking the output
We will consider the first example—it’s a simple application that can’t be done another way.
This example, shown in Listing 17.5, will connect to the Equifax Secure Server via HTTPS,
and write the file it finds there to a file on our Web server.
LISTING 17.5 https-curl.php—Script to Make HTTPS Connections
<?
echo “<h1>HTTPS transfer with cURL</h1>”;
$outputfile = “$DOCUMENT_ROOT/../writable/equifax.html”;
$fp = fopen($outputfile, “w”);
echo “Initializing cURL session...<br>”;
$ch = curl_init();
echo “Setting cURL options...<br>”;
curl_setopt ($ch, CURLOPT_URL, “https://equifaxsecure.com”);
curl_setopt ($ch, CURLOPT_FILE, $fp);
echo “Executing cURL session...<br>”;
curl_exec ($ch);
echo “Ending cURL session...<br>”;
curl_close ($ch);
fclose($fp);
?>
Let’s go through this script. We begin by opening a local file using fopen(). This is where we
are going to store the page we transfer from the secure connection.
When this is done, we need to create a cURL session using the curl_init() function:
$ch = curl_init();
This function returns a handle for the cURL session. You can call it like this, with no parame-
ters, or optionally you can pass it a string containing the URL to connect to. You can also set
the URL using the curl_setopt() function, which is what we have done in this case:
curl_setopt ($ch, CURLOPT_URL, “https://equifaxsecure.com”);
curl_setopt ($ch, CURLOPT_FILE, $fp);
Advanced PHP Techniques
P
ART IV
388
22 7842 CH17 3/6/01 3:39 PM Page 388










