Detailed Usage of cURL

    2014年09月23日 doc php 字数:543

curl命令使用

  • curl www.baidu.com 查看网页源码,前提是有权限
  • curl -o test www.baidu.com 查看网页代码,并且保存至test文件
  • curl example.com/form.cgi?data=xxx 发送GET类型的请求,会直接返回该url的返回值
  • curl –data “method=xxx” test/envup 发送POST类型的请求,会直接返回该url的返回值
  • –data-urlencode 该参数与–data的唯一区别是经过url编码
  • curl –user-agent “[User Agent]” [URL] 模拟以自己设定的useragent来访问url
  • curl –cookie “name=xxx” www.example.com curl发送cookie给对应的url
  • curl –header “Content-Type:application/json” http://example.com curl增加头信息
  • curl –user name:password example.com 对于一些需要用户名密码认证的url,可以通过该方式认证

python中的使用

cmd = 'curl --data "method=testService&test1=1&test2=testvalue" http://example.com' % testvalue
handle = subprocess.Popen(cmd,shell=TRUE,stdout=subprocess.PIPE)

在cmd中,定义了curl的POST命令。再利用python的创建子进程函数,将其当做sell字符串执行,获取返回值。