PHP 理解和使用curl
来源:互联网 发布:软件著作权和发明专利 编辑:程序博客网 时间:2024/06/06 08:48
curl概念
Client URL Library Functions , 用URL语法传输数据的命令行工具
curl 使用场景
获取网页资源 - 网页爬虫WebService数据接口资源 - 动态获取接口数据FTP服务器里面的文件资源 - 下载与上传文件其他资源 - 所有网络上的资源都可以用curl访问和下载到
在PHP 中使用 curl
1、初始化 curl_init();2、参数设置 curl_setopt($handle , CURLOPT_*** , value);3、执行并返回结果 $result = curl_exec($handle);4、获取返回结果的相关参数 curl_getinfo($handle , CURLINFO_***);5、关闭curl curl_close();
curl实战之一 - GET
<?php $handle = curl_init(); curl_setopt($handle, CURLOPT_URL, "http://www.baidu.com"); // 为了让curl_exec 执行后不打印出来;因为它默认执行后打印获取到的内容 curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($handle); var_dump($result); curl_close($handle);
curl实战之二 - POST
<?php $params = "data1=1&data2=2"; $handle = curl_init(); curl_setopt($handle, CURLOPT_URL, "http://www.baidu.com"); curl_setopt($handle, CURLOPT_HEADER, 0); // 为了让curl_exec 执行后不打印出来;因为它默认执行后打印获取到的内容 curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); // POST 方式 url_setopt($handle, CURLOPT_POST , 1); url_setopt($handle, CURLOPT_POSTFIELDS , $params); url_setopt($handle, CURLOPT_HTTPHEADER , array("application/x-www-form-urlencoded; charset=utf-8", "Content-length: ".strlen($params) )); $result = curl_exec($handle); if(!curl_errno($handle)){ var_dump($result); } else { echo 'curl error : '. curl_error($handle); } curl_close($handle);
curl实战之三 - 需要登陆
<?php $params = "username=data1&password=2&remember=1"; $handle = curl_init(); // 1、打开登陆网页 curl_setopt($handle, CURLOPT_URL, "http://xxx/login"); // 为了让curl_exec 执行后不打印出来;因为它默认执行后打印获取到的内容 curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); // 2、COOKIE 相关设置,需要在所有绘画开始之前设置 date_default_timezone_set('PRC'); // 使用Cookie前,必须先设置时区 curl_setopt($handle, CURLOPT_COOKIESESSION, TRUE); curl_setopt($handle, CURLOPT_COOKIEFILE, 'cookiefile'); curl_setopt($handle, CURLOPT_COOKIEJAR, 'cookiefile'); curl_setopt($handle, CURLOPT_COOKIE, session_name().'='.session_id()); curl_setopt($handle, CURLOPT_HEADER, 0); curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 1); // 让curl支持页面链接跳转 // 3、登陆 url_setopt($handle, CURLOPT_POST , 1); url_setopt($handle, CURLOPT_POSTFIELDS , $params); url_setopt($handle, CURLOPT_HTTPHEADER , array("application/x-www-form-urlencoded; charset=utf-8", "Content-length: ".strlen($params) )); $result = curl_exec($handle); // 4、登陆过后要做的内容 curl_setopt($handle, CURLOPT_URL, "http://xxx/index"); url_setopt($handle, CURLOPT_POST , 0); url_setopt($handle, CURLOPT_HTTPHEADER , array("Content-type: text/xml")); $result = curl_exec($handle); if(!curl_errno($handle)){ var_dump($result); } else { echo 'curl error : '. curl_error($handle); } curl_close($handle);
curl实战之四 - FTP上传
$fp = fopen("file" , "a+"); url_setopt($handle, CURLOPT_UPLOAD , 1); url_setopt($handle, CURLOPT_INFILE , $fp); url_setopt($handle, CURLOPT_INFILESIZE , filesize($fp));
curl实战之五 - HTTPS 下载文件
<?php $handle = curl_init(); curl_setopt($handle, CURLOPT_URL, "https://xxx"); // 为了让curl_exec 执行后不打印出来;因为它默认执行后打印获取到的内容 curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); // 设置HTPPS支持 date_default_timezone_set('PRC'); // 终止从服务端进行验证ssl curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0); $result = curl_exec($handle); curl_close($handle); echo $result;
阅读全文