Specifications
In terms of other FTP functions, almost anything that you can do from an FTP command line,
you can do with the FTP functions. You can find the specific functions corresponding to each
FTP command in the PHP online manual at
http://php.net/manual/ref.ftp.php
The exception is mget (multiple get), but you can use ftp_nlist() to get a list of files and
then fetch them as required.
Generic Network Communications with cURL
PHP (from version 4.0.2 onwards) has a set of functions that acts as an interface to cURL, the
Client URL library functions from libcurl, written by Daniel Stenberg.
Previously in this chapter, we looked at using the fopen() function and the file-reading func-
tions to read from a remote file using HTTP. This is pretty much the limit of what you can do
with fopen(). We’ve also seen how to make FTP connections using the FTP functions.
The cURL functions enable you to make connections using FTP, HTTP, HTTPS, Gopher,
Telnet, DICT, FILE, and LDAP. You can also use certificates for HTTPS, send HTTP POST
and HTTP GET parameters, upload files via FTP upload or HTTP upload, work through prox-
ies, set cookies, and perform simple HTTP user authentication.
In other words, just about any kind of network connection that you’d like to make can be done
using cURL.
To use cURL with PHP, you will need to download libcurl, compile it, and run PHP’s
configure script with the --with-curl=[path] option. The directory in path should be the
one that contains the lib and include directories on your system. You can download the library
from
http://curl.haxx.se/
Be aware that you will need a version of cURL from 7.0.2-beta onwards to work with PHP.
There are only a few simple functions to master in order to use the power of cURL. The typi-
cal procedure for using it is
1. Set up a cURL session with a call to the curl_init() function.
2. Set any parameters for transfer with calls to the
curl_setopt() function. This is where
you set options such as the URL to connect to, any parameters to send to that URL, or
the destination of the output from the URL.
3. When everything is set up, call curl_exec() to actually make the connection.
4. Close the cURL session by calling
curl_close().
Using Network and Protocol Functions
C
HAPTER 17
17
USING NETWORK
AND
PROTOCOL
FUNCTIONS
387
22 7842 CH17 3/6/01 3:39 PM Page 387










