curl get started
- -v 显示整个通信过程
- -L 自动处理目标网址的自动跳转如301跳转
- -i 显示头部信息
- -d 发送表单信息
- -X HTTP动词
- -F 文件上传
- -A User Agent字段
- -b cookie
- -H 增加头信息
- -U HTTP认证
-v 显示整个通信过程
C:\Documents and Settings\ganiks.liu\Desktop>curl -v http://yii2back/users
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to yii2back (127.0.0.1) port 80 (#0)
> GET /users HTTP/1.1
> User-Agent: curl/7.37.0
> Host: yii2back
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Thu, 24 Jul 2014 06:04:02 GMT
* Server Apache/2.2.9 (Win32) PHP/5.4.30 mod_fcgid/2.3.6 is not blacklisted
< Server: Apache/2.2.9 (Win32) PHP/5.4.30 mod_fcgid/2.3.6
< X-Powered-By: PHP/5.4.30
< X-Pagination-Total-Count: 1
< X-Pagination-Page-Count: 1
< X-Pagination-Current-Page: 1
< X-Pagination-Per-Page: 20
< Link: <http://yii2back/users?page=1>; rel=self
< Set-Cookie: PHPSESSID=5070unbmku73s1kubincgfku91; path=/; HttpOnly
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< Content-Length: 277
< Content-Type: application/json; charset=UTF-8
<
[{"id":1,"username":"ganiks","auth_key":"iuMVI8xFbvrcdpZrDQbVhdmcLmIt5WgI","password_hash":"$2y$13$06XKlh09ZObd5cOX6pcqAu3swIdPPDlRyJQp8z19CIl6xeQvgD3wu","passwo
rd_reset_token":null,"email":"ganiks@qq.com","role":10,"status":10,"created_at":1406096791,"updated_at":1406096791}]* Connection #0 to host yii2back left intact
C:\Documents and Settings\ganiks.liu\Desktop>
如果你觉得上面的信息还不够,那么下面的命令可以查看更详细的通信过程。
curl --trace output.txt www.sina.com
curl --trace-ascii output.txt www.sina.com
-L 自动处理目标网址的自动跳转如301跳转
为了了解详细的处理过程,这里用到了上一个命令 -v
C:\Documents and Settings\ganiks.liu\Desktop>curl -v -L www.sina.com
* Rebuilt URL to: www.sina.com/
* Hostname was NOT found in DNS cache
* Trying 202.108.33.60...
* Connected to www.sina.com (202.108.33.60) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.37.0
> Host: www.sina.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
* Server nginx is not blacklisted
< Server: nginx
< Date: Thu, 24 Jul 2014 06:10:21 GMT
< Content-Type: text/html
< Location: http://www.sina.com.cn/
< Expires: Thu, 24 Jul 2014 06:12:21 GMT
< Cache-Control: max-age=120
< X-Cache: MISS from xd33-95.sina.com.cn
< Transfer-Encoding: chunked
<
* Ignoring the response-body
* Connection #0 to host www.sina.com left intact
* Issue another request to this URL: 'http://www.sina.com.cn/'
* Hostname was NOT found in DNS cache
* Trying 202.108.33.60...
* Connected to www.sina.com.cn (202.108.33.60) port 80 (#1)
> GET / HTTP/1.1
> User-Agent: curl/7.37.0
> Host: www.sina.com.cn
> Accept: */*
>
< HTTP/1.1 200 OK
* Server nginx is not blacklisted
< Server: nginx
< Date: Thu, 24 Jul 2014 06:09:37 GMT
< Content-Type: text/html
< Last-Modified: Thu, 24 Jul 2014 06:09:19 GMT
< Vary: Accept-Encoding
< Expires: Thu, 24 Jul 2014 06:10:37 GMT
< Cache-Control: max-age=60
< X-Powered-By: shci_v1.03
< Age: 48
< Content-Length: 542068
< X-Cache: HIT from xd33-70.sina.com.cn
<
<!DOCTYPE html>
<!--[30,131,1] published at 2014-07-24 14:09:18 from #153 by 10617-->
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=gb2312" />
<title>新浪首页</title>
<meta name="keywords" content="新浪,新浪网,SINA,sina,sina.com.cn,新浪首页,门户,资讯" />
<meta name="description" content="新浪网为全球用户24小时提供全面及时的中文资讯,内容覆盖国内外突发新闻事件、体坛赛事、娱乐时尚、产业资讯、实用信息等,设
有新闻、体育、娱乐、财经、科技、房产、汽车等30多个内容频道,同时开设博客、视频、论坛等自由互动交流空间。" />
<meta name="stencil" content="PGLS000022" />
<meta name="publishid" content="30,131,1" />
<meta name="verify-v1" content="6HtwmypggdgP1NLw7NOuQBI2TW8+CfkYCoyeB8IDbn8=" />
<meta name="360-site-verification" content="63349a2167ca11f4b9bd9a8d48354541" />
<meta name="application-name" content="新浪首页"/>
<meta name ="msapplication-TileImage" content="http://i1.sinaimg.cn/dy/deco/2013/0312/logo.png"/>
<meta name="msapplication-TileColor" content="#ffbf27"/>
<meta name="sogou_site_verification" content="BVIdHxKGrl"/>
<link rel="apple-touch-icon" href="http://i3.sinaimg.cn/home/2013/0331/U586P30DT20130331093840.png" />
<script type="text/javascript">
//^C
C:\Documents and Settings\ganiks.liu\Desktop>
-i 显示头部信息
C:\Documents and Settings\ganiks.liu\Desktop>curl -i www.sina.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 24 Jul 2014 06:18:09 GMT
Content-Type: text/html
Location: http://www.sina.com.cn/
Expires: Thu, 24 Jul 2014 06:20:09 GMT
Cache-Control: max-age=120
Age: 7
Content-Length: 178
X-Cache: HIT from xd33-88.sina.com.cn
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
C:\Documents and Settings\ganiks.liu\Desktop>
看这里,新浪网址因为 301 了, 所以如果没有用
-L的话, 是得不到内容的
-d 发送表单信息
发送表单信息有GET和POST两种方法。
GET方法相对简单,只要把数据附在网址后面就行。
curl example.com/form.cgi?data=xxx
POST方法必须把数据和网址分开,curl就要用到--data参数。
curl --data "data=xxx" example.com/form.cgi
如果你的数据没有经过表单编码,还可以让curl为你编码,参数是--data-urlencode。
curl --data-urlencode "date=April 1" example.com/form.cgi
-X HTTP动词
curl默认的HTTP动词是GET,使用-X参数可以支持其他动词。
curl -X POST www.example.com
curl -X DELETE www.example.com
-F 文件上传
假定文件上传的表单是下面这样:
<form method="POST" enctype='multipart/form-data' action="upload.cgi">
<input type=file name=upload>
<input type=submit name=press value="OK">
</form>
你可以用curl这样上传文件:
curl --form upload=@localfilename --form press=OK [URL]
八、Referer字段
有时你需要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的。
curl --referer http://www.example.com http://www.example.com
-A User Agent字段
这个字段是用来表示客户端的设备信息。服务器有时会根据这个字段,针对不同设备,返回不同格式的网页,比如手机版和桌面版。
iPhone4的User Agent是
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可以这样模拟:
curl --user-agent "[User Agent]" [URL]
-b cookie
使用--cookie参数,可以让curl发送cookie。
curl --cookie "name=xxx" www.example.com
至于具体的cookie的值,可以从http response头信息的Set-Cookie字段中得到。
-H 增加头信息
有时需要在http request之中,自行增加一个头信息。--header参数就可以起到这个作用。
curl --header "xxx: xxxxxx" http://example.com
-U HTTP认证
有些网域需要HTTP认证,这时curl需要用到--user参数。
curl --user name:password example.com
参考文章:
http://www.ruanyifeng.com/blog/2011/09/curl.html
curl get started的更多相关文章
- 在PowerShell中使用curl(Invoke-WebRequest)
前言 习惯了windows的界面模式就很难转去命令行,甚至以命令行发家的git也涌现出各种界面tool.然而命令行真的会比界面快的多,如果你是一个码农. situation:接到需求分析bug,需要访 ...
- php中实现的一个curl批处理的实例
curl是利用URL语法在命令行方式下工作的开源文件传输工具 本文在php中实现了的一个curl批处理的实例. 代码如下: header("Content-Type:text/html;ch ...
- PHP curl 函数
转载http://sunking.sinaapp.com/archives/111 最近使用curl的时候,发现了一个比较好用的函数,当然是初级者适用的一个函数,就是curl_getinfo(), 在 ...
- curl -w,–write-out参数详解
顾名思义,write-out的作用就是输出点什么.curl的-w参数用于在一次完整且成功的操作后输出指定格式的内容到标准输出. 输出格式由普通字符串和任意数量的变量组成,输出变量需要按照%{varia ...
- CURL HELP
CURL下载 在windows的系统环境变量中,将CURL的路径(curl.exe存放的路径)复制到"Path"变量的结尾 Usage: curl [options...] < ...
- curl的登录总结
demo1 <?php $curl=curl_init('http://www.baidu.com'); curl_exec($curl); curl_close($curl); ?> c ...
- php curl获取的数据不直接输出
curl获取页面内容,不直接输出到页面 必需设置curl的CURLOPT_RETURNTRANSFER选项为1或true curl_setopt($ch, CURLOPT_RETURNTRANSFER ...
- PHP的CURL
使用CURL完成一个请求: 初始化连接句柄 设置CURL选项 执行并获取结果 释放CURL连接句柄 发送GET请求 function doGetRequest($url,$data,$timeout ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...
- php获得远程信息到本地使用的3个函数:file_get_contents和curl函数和stream_get_contents
1:file_get_contents echo file_get_contents("http://www.php.com/index.php"); 2:curl funct ...
随机推荐
- 第一章:1-06、 试将TCP/IP和OSI的体系结构进行比较。讨论其异同之处?
<计算机网络>谢希仁著第四版课后习题答案答:(1)OSI和TCP/IP的相同点是二者均采用层次结构,而且都是按功能分层.(2)OSI和TCP/IP的不同点:①OSI分七层,自下而上分为物理 ...
- 大数据实践:ODI 和 Twitter (一)
本文利用twitter做为数据源,介绍使用Oracle大数据平台及Oralce Data Integrator工具,完成从twitter抽取数据,在hadoop平台上处理数据,并最终加载到oracle ...
- js日期格式化方法 dateFormatFn
var dateFormatFn=function(val,fmt){ var _this = new Date(val); console.log(_this,_this.getFullYear() ...
- Laravel 5 基础(八)- 模型、控制器、视图基础流程
添加路由 Route::get('artiles', 'ArticlesController@index'); 创建控制器 php artisan make:controller ArticlesCo ...
- PHP实例 表单数据插入数据库及数据提取 用户注册验证
网站在进行新用户注册时,都会将用户的注册信息存入数据库中,需要的时候再进行提取.今天写了一个简单的实例. 主要完成以下几点功能: (1)用户进行注册,实现密码重复确认,验证码校对功能. (2)注册成功 ...
- How to display SSRS report based on customer/Vendor specific language [AX2012]
Common requirement is to show the reports in customer’s language. [example : Quotations, sales confi ...
- MHA 日常管理
一.MHA的主要脚本 1.manager端 masterha_check_ssh 检查MHA的SSH配置状况 masterha_check_repl 检查MySQL复制状况 masterha_mang ...
- mono的https使用使用事项
private static void SetCertificatePolicy() { if( ServicePointManager.ServerCertificateValidationCall ...
- pure css兼容IE
<!--[if lte IE 8]> <link rel="stylesheet" href="pure/0.5.0/grids-responsive- ...
- 在MAC OS 下配置python + Flask ,并支持pyCharm编辑器
原创咯- flask是一个micro framework ,伸缩性很强.可以部署到openshift 的PAAS里.这个框架上手非常快.喜欢的可以试试. 若实在MAC里,python已经默认安装了.1 ...