TechTip: Talk to the Internet with cURL PDF Print E-mail
Tips & Techniques - Web Languages
Written by Alan Seiden   
Thursday, 18 May 2006 18:00

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
About the Author:

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

Read More >>
Last Updated on Wednesday, 17 September 2014 09:29
 
User Rating: / 1
PoorBest 

WEBCAST

MC Webcast CenterFEATURED WEBCAST

See What i Can Do With Modern RPG Development

Your applications deserve more than just a new look! Change the perception of the IBM i as “old” with modern development tips from IBM and Profound Logic Software.
Watch as Barbara Morris, Brian May and Alex Roytman dive beneath the GUI surface to show the benefits of:

 

  • Working with free-format RPG in IBM i TR7
  • Breaking free from “The Cycle” of traditional RPG development
  • Going beyond the 10 character limit in display and database files
  • And more!

You'll also see a live demonstration of these techniques as the presenters create a modern web application before your eyes! 

 

Watch the Webinar Now!

 

TRIAL SOFTWARE

MS Office Connector for Query/400

NGS' Qport Office enables Windows users to run IBM Query/400 queries to: 

 - Create and update Excel spreadsheets and Access databases

 - Create Word documents

 - Send to Windows screen and PC printers

No query conversion is required. Works with i5/OS V5R1 & above. Installs in minutes!

If you don’t have a budget to replace IBM Query/400, but want your users to have one click enhanced output of their queries.… Request the online license agreement and product download instructions today!

Offer good through December 31, 2013.


   MC-STORE.COM