MC Press Online

Monday, Feb 20th

Last updateSun, 19 Feb 2017 12pm

You are here: Home ARTICLES Programming Web Languages TechTip: Talk to the Internet with cURL

Programming / Web Languages

TechTip: Talk to the Internet with cURL

Support MC Press - Visit Our Sponsors

NEW BOOK!

Evolve Your RPG Coding: Move from OPM to ILE ... and Beyond


ORDER YOUR COPY

*******************

Click for this Month's

Bookstore Special Deals

The i5 can run a variety of open-source software, thanks to i5/OS PASE's support of AIX binaries. One fine specimen is cURL (client for URLs), a popular program for Internet communication.

From cURL's manual: "cURL is a tool to transfer data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, TFTP, DICT, TELNET, LDAP or FILE)...cURL offers a busload of useful tricks."

It's easy to use cURL to request pages from Web sites, or, with a little Control Language (CL) code, to automate Web-based tasks:

  • Check whether your site is running.
  • Get Web-based information according to the URL parameters you pass.
  • Execute an action on another server via a URL.
  • Upload a file to a Web site that supports HTTP file uploads.

This article will get you started by demonstrating a simple Web page download.

Get cURL

Some kind souls on the Web have compiled and packaged the AIX binary for us, so we don't have to compile our own. For this demonstration, we'll choose a non-SSL-enabled version of cURL (SSL encryption would require installing many prerequisites).

Download the cURL command-line binary. It is archived at UCLA's Public Domain Software Library for AIX. Choose the download from the Binary column where the row shows AIX=4.2 and Version=7.13.1. Although this package is somewhat old, it is more complete than that supplied for newer versions. The file can also be downloaded directly (this could change over time) from ftp://aixpdslib.seas.ucla.edu/pub/curl/RISC/4.2/exec/curl.7.13.1.tar.Z. The ".tar.z" extension indicates a UNIX-style compressed archive file. Save the file to your hard drive.

Extract and Install cURL

Although you could use PASE's uncompress/tar commands on your iSeries/i5 to extract the contents of the ".tar.z" file, I prefer to extract the files on my Windows PC with a program such as the free ZipGenius.

The extracted contents comprise a directory structure. Of that structure, we are interested in the /usr/local/bin folder. Look inside the bin folder for a file called curl. Copy curl to your i5's root file system. I chose to copy curl to a folder called /coolprograms using a Windows Explorer mapped drive. If I had not been able to map a drive for some reason, I could have used iSeries Navigator or FTP.

Get a Web Page

Let's try it!

Launch the PASE interactive (terminal) environment:
CALL QP2TERM

Once inside PASE:
cd /coolprograms [or whatever folder you copied curl into]
curl www.google.com

If the i5 is connected to the Internet, you will see the output on your screen (Figure 1).


http://www.mcpressonline.com/articles/images/2002/seiden_alan_techtip_cURLV4--05260600.png

Figure 1: The command curl www.google.com retrieves the HTML of Google's home page. (Click images to enlarge.)

Redirect the Output to a File

We may wish to redirect cURL's output to a file for later analysis or other use. In our example, we will create an output directory and then adapt our curl command to send the output there. A redirection symbol (>) sends the HTML to a file of our choice.

mkdir /htmloutput
curl www.google.com > /htmloutput/google.html

Verify the successful download by viewing the contents with UNIX's cat command:
cat /htmloutput/google.html

The results of these commands are shown in Figure 2.

http://www.mcpressonline.com/articles/images/2002/seiden_alan_techtip_cURLV4--05260601.png

Figure 2: cURL sends output to a file whose contents are then displayed by the UNIX cat command.

Automate cURL by Calling It from CL

For ultimate control, we can automate cURL by calling it via QP2SHELL or QP2SHELL2 from our CL programs:

/* The -o means we will indicate where to save our result. */

/* The -s means silent mode: Do not show status or errors. */
/* (Note that the -o and -s arguments must be lowercase) */

CALL PGM(QP2SHELL2) +
PARM('/coolprograms/curl' '-o' +
'/htmloutput/webpage.html' +
'http://www.thesite.com' '-s')

Use this CL command as a template for many possibilities of letting your i5 talk to the Internet.

Resources

Manual Page for cURL
Using cURL to Automate HTTP Jobs
Mailing Lists for cURL
History of cURL by its creator, Daniel Stenberg


 

Alan Seiden

 

Alan Seiden, an authority on PHP development with IBM i, helps companies achieve best practices and top performance when combining PHP, Zend Framework, IBM i, RPG, and DB2. An award-winning speaker, Alan is project manager of Zend's open-source PHP Toolkit for IBM i. Alan Seiden Consulting, Ho-Ho-Kus, N.J.: http://www.alanseiden.com | alan@alanseiden.com | @alanseiden

BLOG COMMENTS POWERED BY DISQUS