curl It is a command-line tool that sends out network requests and then retrieves and extracts data that is displayed on stdout.
It supports a variety of protocols. Here's an example of how to use it in website development.
1. Viewing Web Source Code
You can see the source code of the web page by adding the web address directly after the curl command. Let's take the website www.sina.com as an example.
curl www.sina.com
D:\N3verL4nd\Desktop>curl www.sina.com
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
If you want to save this page, you can use the - o parameter, which is equivalent to using the wget command.
Curl-o [filename] www.sina.com
2. Automatic jump
Some websites jump automatically. With the - L parameter, curl jumps to the new address.
curl -L www.sina.com
Type the command above and the result will automatically jump to www.sina.com.cn.
Display header information
- The i parameter can display the header information of http response, along with the page code.
curl -i www.sina.com
D:\N3verL4nd\Desktop>curl -i www.sina.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 27 Jul 2017 08:30:03 GMT
Content-Type: text/html
Content-Length: 178
Connection: close
Location: http://www.sina.com.cn/
Expires: Thu, 27 Jul 2017 08:30:38 GMT
Cache-Control: max-age=120
Age: 85
Via: http/1.1 cnc.beixian.ha2ts4.214 (ApacheTrafficServer/4.2.1.1 [cRs f ]), http/1.1 gwbn.beijing.ha2ts4.24 (ApacheTrafficServer/4.2.1.1 [cRs f ])
X-Cache: HIT.214
X-Via-CDN: f=edge,s=gwbn.beijing.ha2ts4.21.nb.sinaedge.com,c=124.207.38.7;f=Edge,s=gwbn.beijing.ha2ts4.24,c=219.238.4.21;f=edge,s=cnc.beixian.ha2ts4.205.nb.sinaedge.com,c=172.16.110.24;f=Edge,s=cnc.beixian.ha2ts4.214,c=219.238.4.22
X-Cache: HIT.unknown
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
- The I parameter displays only the header information of http response.
IV. Displaying Communication Processes
- The v parameter can display the whole process of an http communication, including port connection and http request header information.
curl -v www.sina.com
D:\N3verL4nd\Desktop>curl -v www.sina.com
* Rebuilt URL to: www.sina.com/
* Trying 219.238.4.9...
* TCP_NODELAY set
* Connected to www.sina.com (219.238.4.9) port 80 (#0)
> GET / HTTP/1.1
> Host: www.sina.com
> User-Agent: curl/7.53.1
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Thu, 27 Jul 2017 08:31:07 GMT
< Content-Type: text/html
< Content-Length: 178
< Connection: close
< Location: http://www.sina.com.cn/
< Expires: Thu, 27 Jul 2017 08:31:23 GMT
< Cache-Control: max-age=120
< Age: 104
< Via: http/1.1 cnc.beixian.ha2ts4.214 (ApacheTrafficServer/4.2.1.1 [cRs f ]), http/1.1 gwbn.beijing.ha2ts4.24 (ApacheTrafficServer/4.2.1.1 [cRs f ])
< X-Cache: HIT.214
< X-Via-CDN: f=edge,s=gwbn.beijing.ha2ts4.24.nb.sinaedge.com,c=124.207.38.7;f=Edge,s=gwbn.beijing.ha2ts4.24,c=219.238.4.24;f=edge,s=cnc.beixian.ha2ts4.213.nb.sinaedge.com,c=172.16.110.24;f=Edge,s=cnc.beixian.ha2ts4.214,c=219.238.4.22
< X-Cache: HIT.unknown
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Closing connection 0
D:\N3verL4nd\Desktop>
If you don't think the above information is enough, the following command allows you to view the communication process in more detail.
curl –trace output.txt www.sina.com
perhaps
curl –trace-ascii output.txt www.sina.com
After running, please open the output.txt file to see.
5. Sending Form Information
There are two ways to send form information: GET and POST. GET method is relatively simple, as long as the data is attached to the back of the address.
curl example.com/form.cgi?data=xxx
The POST method must separate the data from the web address, and curl uses the data parameter.
curl -X POST –data "data=xxx" example.com/form.cgi
perhaps
curl -d "data=xxx" example.com/form.cgi
If your data is not encoded by the form, you can also have curl code for you, with the parameter -- data-urlencode.
curl -X POST–data-urlencode "date=April 1" example.com/form.cgi
VI. HTTP Verbs
curl's default HTTP verb is GET, and other verbs can be supported with the - X parameter.
curl -X POST www.example.com
curl -X DELETE www.example.com
7. Document upload
Assume that the form uploaded by the file is as follows:
<form method="POST" enctype='multipart/form-data' action="upload.cgi">
<input type=file name=upload>
<input type=submit name=press value="OK">
</form>
You can use curl to upload files like this:
curl –form upload=@localfilename –form press=OK [URL]
Referer field
Sometimes you need to provide a referer field in the http request header to indicate where you jumped.
curl –referer http://www.example.com http://www.example.com
9. User Agent Field
This field is used to represent the device information of the client. Servers sometimes return pages in different formats for different devices, such as mobile and desktop versions, according to this field.
The User Agent for the iPhone 4 is
Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
curl can be simulated as follows:
curl –user-agent "[User Agent]" [URL]
10. cookie
With the -- cookie parameter, curl can send cookies.
curl –cookie "name=xxx" www.example.com
As for the value of the specific cookie, it can be obtained from the Set-Cookie field of the http response header information.
- c cookie-file can save the cookie returned by the server to the file, and-b cookie-file can use this file as Cookie information for subsequent requests.
curl -c cookies http://example.com
curl -b cookies http://example.com
Increase header information
Sometimes you need to add a header to your http request. The header parameter can do this.
curl –header "Content-Type: application/json" http://example.com
perhaps
curl -H "Content-Type: application/json" http://example.com
12. HTTP Authentication
Some domains require HTTP authentication, and curl needs the -- user parameter.
curl –user name:password example.com