This article will introduce the use of curl. According to the common scenarios, it will provide the demo code and server code for calling curl to realize the request, which is convenient for everyone to learn and use.
1. View the Web Source Code
The curl command is followed by a web address, so you can see the source code of the web page.
curl www.csdn.net
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
2. Automatic jump
In the example above, because the web site has been jumped, curl defaults to not follow the jump, so only the source code of the first page is obtained. If we want to follow the jump, we can use the - L parameter.
curl -L www.csdn.net
After adding the - L parameter, curl jumps to the new page and returns the source code of the new page.
3. View the source code of the web page and download it.
Save the source code of the page to the file, and use the - o parameter with the save path.
curl -o csdn.viewcode www.csdn.net
After execution, the source code of the web page is saved in the csdn.viewcode file.
4. Display header information
curl -i www.csdn.net
HTTP/1.1 302 Moved Temporarily
Server: openresty
Date: Sun, 17 Dec 2017 09:18:46 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Keep-Alive: timeout=20
Location: https://www.csdn.net
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
If you only need to display the header information of http response, use -I
curl -I www.csdn.net
HTTP/1.1 302 Moved Temporarily
Server: openresty
Date: Sun, 17 Dec 2017 09:20:25 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Keep-Alive: timeout=20
Location: https://www.csdn.net
5. Display the communication process
curl -v www.csdn.net
* Rebuilt URL to: www.csdn.net/
* Trying 101.201.172.229...
* Connected to www.csdn.net (101.201.172.229) port 80 (#0)
> GET / HTTP/1.1
> Host: www.csdn.net
> User-Agent: curl/7.49.1
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Server: openresty
< Date: Sun, 17 Dec 2017 09:23:06 GMT
< Content-Type: text/html
< Content-Length: 154
< Connection: keep-alive
< Keep-Alive: timeout=20
< Location: https://www.csdn.net
<
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host www.csdn.net left intact
6. Display detailed communication process
If you want to see a more detailed communication process, you can use the following two parameters.
curl --trace trace.log www.csdn.com
curl --trace-ascii trace.log www.csdn.com
After execution, open trace.log to view.
7. Submit forms
get mode
Data is spliced directly behind the address
curl "localhost/server.php?a=1&b=2&c=3"
Server-side code:
<?php
$param = array('a','b','c');
foreach($param as $v){
echo $v.'='.(isset($_GET[$v])? $_GET[$v] : '').PHP_EOL;
}
?>
post mode
Data should be separated from address by using the data parameter
curl -X POST --data "a=1&b=2&c=3" "localhost/server.php"
It can also be simplified to
curl -d "a=1&b=2&c=3" "localhost/server.php"
Form coding
If the data is not encoded, the data-urlencode parameter is used to execute the encoding, but a separate parameter is needed to encode it.
curl -X POST --data-urlencode "a=Ha-ha" --data-urlencode "b=Hey" --data-urlencode "c=Ha-ha" "localhost/server.php"
Server-side code:
<?php
$param = array('a','b','c');
foreach($param as $v){
echo $v.'='.(isset($_POST[$v])? $_POST[$v] : '').PHP_EOL;
}
?>
8. Upload files
For example, the file upload form is as follows:
<form name="form1" method="post" action="test.php" enctype="multipart/form-data">
<p>file:<input type="file" name="file"></p>
<p><input type="submit" value="submit"></p>
</form>
Use the - form or - F parameter
Only upload pictures
curl --form file=@/tmp/photo.jpg localhost/server.php
Upload pictures and submit other parameters
curl -F upload=@/tmp/photo.jpg -F upload_field=upload -F file_path=abc.jpg localhost/server.php
Server-side code:
<?php
$upload_field = isset($_POST['upload_field'])? $_POST['upload_field'] : 'file';
$name = explode('.', $_FILES[$upload_field]['name']);
$file_ext = array_pop($name);
$file_path = isset($_POST['file_path'])? $_POST['file_path'] : time().'.'.$file_ext;
$upload_flag = move_uploaded_file($_FILES[$upload_field]['tmp_name'], dirname(__FILE__).'/'.$file_path);
echo ($upload_flag==1? 'true' : 'false').PHP_EOL;
?>
9. Simulated sources
curl --referer 'www.csdn.net' localhost/server.php
Server-side code:
<?php
echo 'REFERER:'.(isset($_SERVER['HTTP_REFERER'])? $_SERVER['HTTP_REFERER'] : '').PHP_EOL;
?>
10. Analog Client Device
Simulated iPhone Access
curl --user-agent "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25" localhost/server.php
Simulate chrome access
curl --user-agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" localhost/server.php
Server-side code:
<?php
function isMobile() {
$agent = $_SERVER['HTTP_USER_AGENT'];
return stripos($agent, 'iPod;') !== false || stripos($agent, 'iPhone') !== false || stripos($agent, 'Android') !== false || stripos($agent, 'iPad') !== false;
}
echo (isMobile()? 'true' : 'false').PHP_EOL;
?>
Simulated iPhone access returns true, simulated chrome access returns false.
11. Send and receive cookie s
Get the returned cookie
curl -c cookiefile url
Cookies passed on request
curl -b cookiefile url
Server-side code:
<?php
if(isset($_COOKIE['username'])){
echo $_COOKIE['username'].' is login'.PHP_EOL;
}else{
$username = isset($_POST['username'])? $_POST['username'] : '';
$password = isset($_POST['password'])? $_POST['password'] : '';
if($username=='fdipzone' && $password=='654321'){
setcookie('username', $username, time()+3600);
echo 'login success'.PHP_EOL;
}else{
echo 'login fail'.PHP_EOL;
}
}
?>
Access without cookie s
curl localhost/server.php
Post-execution output: login fail
Log in to get cookie s
curl -c /tmp/cookie -d "username=fdipzone&password=654321" localhost/server.php
Output after execution: login success
The obtained cookie is saved in the / tmp/cookie file.
Using the acquired cookie access
curl -b /tmp/cookie localhost/server.php
Post-execution output: fdipzone is login
Represents a cookie that can be retrieved for the delivery of the request.
12.header
Bring a custom header when requesting.
curl --header "access-token:RGV2cXgxWHFVbEdVN1o2NFVkSG40Zmo1aXE" --header "lang:zh_CN" localhost/server.php
Server-side code:
<?php
/**
* Get custom header data
*/
function get_all_headers(){
// Ignore captured header data
$ignore = array('host','accept','content-length','content-type');
$headers = array();
foreach($_SERVER as $key=>$value){
if(substr($key, 0, 5)==='HTTP_'){
$key = substr($key, 5);
$key = str_replace('_', ' ', $key);
$key = str_replace(' ', '-', $key);
$key = strtolower($key);
if(!in_array($key, $ignore)){
$headers[$key] = $value;
}
}
}
return $headers;
}
print_r(get_all_headers());
?>
Output after execution:
Array
(
[lang] => zh_CN
[access-token] => RGV2cXgxWHFVbEdVN1o2NFVkSG40Zmo1aXE
[user-agent] => curl/7.49.1
)
13.HTTP authentication
Some addresses require HTTP authentication, and authentication information can be passed using the user parameter.
curl --user "user:pass" url
Server-side code:
<?php
if(!isset($_SERVER['PHP_AUTH_USER']))
{
header('WWW-Authenticate: Basic realm="localhost"');
header("HTTP/1.0 401 Unauthorized");
exit('Unauthorized'.PHP_EOL);
}else{
if (($_SERVER['PHP_AUTH_USER']!= "fdipzone" || $_SERVER['PHP_AUTH_PW']!="654321")) {
header('WWW-Authenticate: Basic realm="localhost"');
header("HTTP/1.0 401 Unauthorized");
exit('Unauthorized'.PHP_EOL);
}
}
$content = isset($_POST['content'])? $_POST['content'] : '';
header('content-type:application/json');
echo $content.PHP_EOL;
?>
Request without authentication information
curl -d "content=abc123" localhost/server.php
Post-execution output: Unauthorized
Request with authentication information
curl -d "content=abc123" --user "fdipzone:654321" localhost/server.php
Post-execution output: abc123