• -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的更多相关文章

  1. 在PowerShell中使用curl(Invoke-WebRequest)

    前言 习惯了windows的界面模式就很难转去命令行,甚至以命令行发家的git也涌现出各种界面tool.然而命令行真的会比界面快的多,如果你是一个码农. situation:接到需求分析bug,需要访 ...

  2. php中实现的一个curl批处理的实例

    curl是利用URL语法在命令行方式下工作的开源文件传输工具 本文在php中实现了的一个curl批处理的实例. 代码如下: header("Content-Type:text/html;ch ...

  3. PHP curl 函数

    转载http://sunking.sinaapp.com/archives/111 最近使用curl的时候,发现了一个比较好用的函数,当然是初级者适用的一个函数,就是curl_getinfo(), 在 ...

  4. curl -w,–write-out参数详解

    顾名思义,write-out的作用就是输出点什么.curl的-w参数用于在一次完整且成功的操作后输出指定格式的内容到标准输出. 输出格式由普通字符串和任意数量的变量组成,输出变量需要按照%{varia ...

  5. CURL HELP

    CURL下载 在windows的系统环境变量中,将CURL的路径(curl.exe存放的路径)复制到"Path"变量的结尾 Usage: curl [options...] < ...

  6. curl的登录总结

    demo1 <?php $curl=curl_init('http://www.baidu.com'); curl_exec($curl); curl_close($curl); ?> c ...

  7. php curl获取的数据不直接输出

    curl获取页面内容,不直接输出到页面 必需设置curl的CURLOPT_RETURNTRANSFER选项为1或true curl_setopt($ch, CURLOPT_RETURNTRANSFER ...

  8. PHP的CURL

    使用CURL完成一个请求: 初始化连接句柄 设置CURL选项 执行并获取结果 释放CURL连接句柄 发送GET请求 function doGetRequest($url,$data,$timeout ...

  9. PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案

    $s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...

  10. 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. Maven的HTTP代理设置

    http://blog.sina.com.cn/s/blog_4f925fc30102ed3y.html   第一.检测本地网络是否不能直接访问Maven的远程仓库,命令为ping repo1.mav ...

  2. php文件hash算法,秒传原理

    header('Content-type:text/html;Charset=UTF-8'); define('blockSize', 4*1024*1024); var_dump(fileHash( ...

  3. android 的通知管理

    1在context里定义通知管理器(NotificationManager) NotificationManager notificationManager = (NotificationManage ...

  4. java枚举类型使用笔记

    1.values()方法返回枚举所有实例的一个数组,调用这个数组的length方法,可以得到这个枚举对象中实例的个数 2.枚举类的每个实例,其实都是static的,可以通过static方法直接调用,而 ...

  5. [terry笔记]dblink

    dblink在分布式数据库集群中很常见,今天简单的总结一下. db link 的命名和global_names有关,global_name是数据库全局名称,global_name在你所管理的数据库中要 ...

  6. 开始认真学计算机网络----computer network学习笔记(一)

    什么是计算机网络,就是连一堆计算机,计算机不单单指pc,还包括打印机啦,手机啦巴拉巴拉一堆 为什么要连,share data共享数据 数据? 文档,图片,视频,巴拉巴拉 网络有什么类型? LAN--- ...

  7. Linux学习-0627

    1.文件处理命令ls愿意:list权限:所有用户ls -a all 所有文件ls -l long 详细信息ls -d directory 查看目录,目录自己详细信息选项可以组合使用,  ls -ld ...

  8. Excle中LOOKUP经典用法

    在Excle中我们经常会遇到需要求根据某个区间的判断然后获取到对应的结果,下面是一个具体的实现例子: 例如: 现在需要实现,当输入0到25以内的任何数字时,会自动获取相应的英文字母 =IFERROR( ...

  9. Environment 类

    提供有关当前环境和平台的信息以及操作它们的方法. 此类不能被继承. using System; using System.Collections; using System.Collections.G ...

  10. 模糊查询&&日期时间操作

    一.模糊查询 1.采用“_”.“%”通配符进行查询 select * from Students where stu_name like '张_';--一个‘_’表示一个字符 select * fro ...