curl基本使用

curl简介

linux curl是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载。
curl可以使用URL的语法模拟浏览器来传输数据,因为它是模拟浏览器,因此它同样支持多种协议,FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP等协议都可以很好的支持,包括一些:HTTPS认证,HTTP POST方法,HTTP PUT方法,FTP上传,keyberos认证,HTTP上传,代理服务器,cookies,用户名/密码认证,下载文件断点续传,上传文件断点续传,http代理服务器管道,甚至它还支持IPv6,scoket5代理服务器,通过http代理服务器上传文件到FTP服务器等等。

curl 常用命令

http://www.cnblogs.com/gbyukg/p/3326825.html

curl 在php中的应用

php建立curl请求的基本步骤:

//1.初始化句柄
$ch = curl_init();
//2.设置选项,包括设置url
curl_setopt($ch, CURLOPT_URL, 'http://baidu.com');
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); 
//3.执行并获取url对应的文档内容
$content = curl_exec($ch);
//4.释放curl句柄
curl_close($ch);

其中,第2步是核心,有很多curl参数可供设置,它们能指定URL请求的各个细节。

curl实现get/post请求

  1. get请求
    参考楼上的例子

  2. post请求

$url = "http://localhost/post_output.php";
$post_data = array (
    "foo" => "bar",
    "query" => "Nettuts",
    "action" => "Submit"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);//post参数
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post的内容
$output = curl_exec($ch);
curl_close($ch);
echo $output;

curl并行处理

http://www.nowamagic.net/librarys/veda/detail/124
基于PHP的cURL快速入门
http://www.chinaz.com/program/2010/0119/104346_6.shtml
PHP的curl实现get,post 和 cookie(几个实例)
http://justcoding.iteye.com/blog/842371

posted @ 2015-08-17 21:18  fly1988happy  阅读(823)  评论(0编辑  收藏  举报