curl

概述:
在设计之初,cURL(Client URL Library)是一种作为使用URL语法传输数据的命令行工具。通过cURL库,我们可以在PHP脚本中自由地使用某种协议来获取或者提交数据,比如获取HTTP请求数据。简单的来说。cURL是客户端向服务器请求资源的工具。

cURL的优势:
在PHP中,想要获取某个URL的内容其实很简单,有多重实现方法,比如使用file_get_contents()函数:
<?php
$content = file_get_contents("https://segmentfault.com");
var_dump($content);

虽然file_get_contents()函数使用起来很方便,但是不够灵活,也没法进行错误处理。在一些复杂的请求中,不能够设置请求头、Cookie、代理、认证等相关信息,更不能向某个服务器提价表单数据,上传文件。
cURL库不但支持丰富的网络协议,而且提供了设置各种URL请求参数的方法,功能强大。cURL的使用场景有很多,比如访问网页资源,获取WebService接口数据、下载FTP服务器文件。

cURL命令行常见用法
1.获取页面内容
当我们不加任何选项使用curl时,默认会发送GET请求来获取链接内容到标准输出。
curl http://www.baidu.com


2.显示HTTP头
1)如果我们只想要显示HTTP头,而不显示文件内容,可以使用 -I 选项:
curl -I http://www.baidu.com

输出为:
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: Keep-Alive
Content-Length: 277
Content-Type: text/html
Date: Tue, 06 Aug 2019 08:31:38 GMT
Etag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

2)也可以同时显示HTTP头和文件内容,使用 -i 选项:
curl -i http://www.baidu.com
输出为:
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: Keep-Alive
Content-Length: 2381
Content-Type: text/html
Date: Tue, 06 Aug 2019 08:34:12 GMT
Etag: "588604c8-94d"
Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class="fm"> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class="s_ipt" value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class="mnav">新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class="mnav">hao123</a> <a href=http://map.baidu.com name=tj_trmap class="mnav">地图</a> <a href=http://v.baidu.com name=tj_trvideo class="mnav">视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class="mnav">贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class="lb">登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class="bri" style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class="cp-feedback">意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

3、将链接保存到文件
1)我们可以使用 > 符号将输出重定向到本地文件中。
curl http://www.baicu.com > index.html
2)也可以通过curl自带的-o/-O选项将内容保存到文件中。
-o(小写的o):结果会被保存到命令行中提供的文件名
-O(大写的O):URL中的文件名会被用作保存输出的文件名
curl -o index.html http://www.codebelief.com
curl -O http://www.codebelief.com/page/2/

4、同时下载多个文件
我们可以使用-o 或 -O选项来同时制定多个链接,按照以下格式编写命令:
curl -o http://www.codebelief.com/page/2/ -O http://www.codebelief.com/page/3/
或者
curl -o page1.html http://www.codebelief.com/page/1/ -o page2.html http://www.codebelief.com/page/2/

5、使用-A自定义User-Agent
我们可以使用-A来自定义用户代理,例如下面的命令将伪装成安卓火狐浏览器对网页进行请求:
curl -A "Nozilla/5.0(Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" http://www.baidu.com

6、使用-H自定义header
当我们需要传递特定的header的时候,可以仿照以下命令来写:
curl -H "Referer: www.example.com" -H "User-Agent: custom-User-Agent" http://www.baicu.com
可以看到,当我们使用-H来自定义 User-Agent时,需要使用"User-Agent:xxx"的格式。
我们能够直接在header中传递Cookie,格式与上面的例子一样:
curl -H "Cookie: JSESSIONID=D0112A5063D938586B659EF8F939BE24" http://www.example.com

7、使用 -c 保存Cookie
当我们使用cURL访问页面的时候,默认是不会保存Cookie的。有些情况下我们希望保存Cookie以便下次访问时使用。例如登录某个网站,我们希望再次访问该网站时保持登录的状态,这时就可以先将登录时的Coolie保存起来,下次访问时再读取。
-c后面跟上要保存的文件名。
curl -c "cookie-example" http://www.baidu.com

8、使用-b读取Cookie
curl -b "cookie-example" http://www.example.com
即 -b 后面既可以是Cookie字符串,也可以是保存了Cookie的文件名

9、使用-d发送post请求
curl -d 'username=用户名&password=密码' -X POST http://www.example.com
-d 用于指定发送的数据,-X 用于指定发送数据的方式
如果省略-X,则默认使用POST方式

10、强制使用GET方式
发送数据时,不仅可以使用post方式,也可以使用GET方式,使用上面提到的-X
或者使用-G选项
curl -d "somedata" -G http://www.example.com/api

原文地址:https://www.cnblogs.com/lxhyty/p/11310425.html