cURL用法举例

节选自http://curl.netmirror.org/docs/manual.html

SIMPLE USAGE
Get the main page from Netscape's web-server:
curl http://www.netscape.com/

Get a web page from a server using port 8000:
curl http://www.weirdserver.com:8000/


DOWNLOAD TO A FILE
Get a web page and store in a local file:
curl -o thatpage.html http://www.netscape.com/

Get a web page and store in a local file, make the local file get the name of the remote document (if no file name part is specified in the URL, this
will fail):
curl -O http://www.netscape.com/index.html


USING PASSWORDS
FTP
To ftp files using name+passwd, include them in the URL like:
curl ftp://name:passwd@machine.domain:port/full/path/to/file

or specify them with the -u flag like
curl -u name:passwd ftp://machine.domain:port/full/path/to/file

HTTP
Curl also supports user and password in HTTP URLs, thus you can pick a file like:
curl http://name:passwd@machine.domain/full/path/to/file

or specify user and password separately like in
curl -u name:passwd http://machine.domain/full/path/to/file


PROXY
Get an ftp file using a proxy named my-proxy that uses port 888:
curl -x my-proxy:888 ftp://ftp.leachsite.com/README

Get a file from a HTTP server that requires user and password, using the same proxy as above:
curl -u user:passwd -x my-proxy:888 http://www.get.this/

Some proxies require special authentication. Specify by using -U as above:
curl -U user:passwd -x my-proxy:888 http://www.get.this/


RANGES
With HTTP 1.1 byte-ranges were introduced. Using this, a client can request to get only one or more subparts of a specified document. Curl supports this with the -r flag.
Get the first 100 bytes of a document:
curl -r 0-99 http://www.get.this/

Get the last 500 bytes of a document:
curl -r -500 http://www.get.this/

Curl also supports simple ranges for FTP files as well. Then you can only specify start and stop position. Get the first 100 bytes of a document using FTP:
curl -r 0-99 ftp://www.get.this/README


DETAILED INFORMATION
Different protocols provide different ways of getting detailed information about specific files/documents. To get curl to show detailed information about a single file, you should use -I/--head option. It displays all available info on a single file for HTTP and FTP. The HTTP information is a lot more extensive.

For HTTP, you can get the header information (the same as -I would show) shown before the data by using -i/--include. Curl understands the -D/--dump-header option when getting files from both FTP and HTTP, and it will then store the headers in the specified file. Store the HTTP headers in a separate file (headers.txt in the example):
curl --dump-header headers.txt curl.haxx.se

Note that headers stored in a separate file can be very useful at a later time if you want curl to use cookies sent by the server. More about that in the cookies section.

EXTRA HEADERS
When using curl in your own very special programs, you may end up needing to pass on your own custom headers when getting a web page. You can do this by using the -H flag.

Example, send the header "X-you-and-me: yes" to the server when getting a page:
curl -H "X-you-and-me: yes" www.love.com

This can also be useful in case you want curl to send a different text in a header than it normally does. The -H header you specify then replaces the header curl would normally send. If you replace an internal header with an empty one, you prevent that header from being sent. To prevent the Host: header from being used:
curl -H "Host:" www.server.com



网上还有很多有关cURL的文档,这里收藏了一些:
Using cURL to automate HTTP jobs
用 curl 和 scsh 编写 web 脚本
CLI Magic: Use cURL to measure Web site statistics
Do everything right from the command line
Talk to the Internet with cURL
Using cURL with PHP
Simplify Network Programming with libCURL
cURL官方网站

没有评论:

发表评论