【捷哥浅谈PHP】第十六弹---文件传输工具cURL的高级应用

我们接着上文的内容来讲,上文给大家简单介绍了下使用curl的四个步骤,本文来给大家讲解下curl的一些高级应用:

获取请求的相关信息,我们可以在curl执行完成后利用curl_getinfo():
  1. <?php
  2. // 创建一个新cURL资源
  3. $ch = curl_init("http://www.lampbrother.net");
  4. // 设置URL和相应的选项
  5. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  6. // 检查是否有错误发生
  7. if(!curl_errno($ch))
  8. {
  9. $info = curl_getinfo($ch);
  10. var_dump($info);
  11. }
  12. // 抓取URL并把它传递给浏览器
  13. $html = curl_exec($ch);
  14. // 关闭cURL资源,并且释放系统资源
  15. curl_close($ch);
  16. ?>

    打印出来的内容为:

    array
    'url' => string 'http://www.lampbrother.net' (length=26)
    'content_type' => null
    'http_code' => int 0
    'header_size' => int 0
    'request_size' => int 0
    'filetime' => int 0
    'ssl_verify_result' => int 0
    'redirect_count' => int 0
    'total_time' => float 0
    'namelookup_time' => float 0
    'connect_time' => float 0
    'pretransfer_time' => float 0
    'size_upload' => float 0
    'size_download' => float 0
    'speed_download' => float 0
    'speed_upload' => float 0
    'download_content_length' => float -1
    'upload_content_length' => float -1
    'starttransfer_time' => float 0
    'redirect_time' => float 0
    'certinfo' =>array
    empty
    'redirect_url' => string '' (length=0)

    返回的数组中包括了以下信息:
    “url” //资源网络地址
    “content_type” //内容编码
    “http_code” //HTTP状态码
    “header_size” //header的大小
    “request_size” //请求的大小
    “filetime” //文件创建时间
    “ssl_verify_result” //SSL验证结果
    “redirect_count” //跳转技术
    “total_time” //总耗时
    “namelookup_time” //DNS查询耗时
    “connect_time” //等待连接耗时
    “pretransfer_time” //传输前准备耗时
    “size_upload” //上传数据的大小
    “size_download” //下载数据的大小
    “speed_download” //下载速度
    “speed_upload” //上传速度
    “download_content_length”//下载内容的长度
    “upload_content_length” //上传内容的长度
    “starttransfer_time” //开始传输的时间
    “redirect_time”//重定向耗时

    我们甚至可以通过curl来模拟浏览器用POST方式发送数据:

    我们先来建立一个可以打印POST数据的页面:
  1. <?php
  2. var_dump($_POST);
  3. ?>
再新建一个页面,用来模拟浏览器发送POST数据:
  1. <?php
  2. $url = "http://localhost/post.php";
  3. $post_data = array(
  4. "author"=>"李捷",
  5. "title"=>"捷哥浅谈PHP"
  6. );
  7. //初始化,创建一个新cURL资源
  8. $ch = curl_init();
  9. //设置URL和相应的选项
  10. curl_setopt($ch,CURLOPT_URL,$url);
  11. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  12. curl_setopt($ch,CURLOPT_POST,1);
  13. curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
  14. //抓取URL并把它传递给浏览器
  15. $out = curl_exec($ch);
  16. //关闭cURL资源,并且释放系统资源
  17. curl_close($ch);
  18. echo $output;
  19. ?>
    打印出来的结果:

    array
    'author' => string '李捷' (length=4)
    'title' => string '捷哥浅谈PHP' (length=11)


    我们可以看到强大的curl已经帮我们把post数据传递过来了,它是这样一个过程:

    1.把post数据传递给post.php页面
    2.post.php页面将post数据输出显示在页面上
    3.curl将post.php接收并打印出的post数据抓取回来,输出在页面上!

    我们不仅能使用post传递数据,我们还可以上传文件,方法基本相同:


    curl.php
  1. <?php
  2. $url = "http://localhost/upload.php";
  3. $post_data = array(
  4. "title"=>"惊艳!!!",
  5. "pic"=>"@d:\李文凯唯美艳照.jpg"
  6. );
  7. //初始化,创建一个新cURL资源
  8. $ch = curl_init();
  9. //设置URL和相应的选项
  10. curl_setopt($ch,CURLOPT_URL,$url);
  11. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  12. curl_setopt($ch,CURLOPT_POST,1);
  13. curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
  14. //抓取URL并把它传递给浏览器
  15. $out = curl_exec($ch);
  16. //关闭cURL资源,并且释放系统资源
  17. curl_close($ch);
  18. echo $output;
  19. ?>

upload.php
  1. <?php
  2. var_dump($_FILES);
  3. ?>

    传递回来的值:

    array
    'pic' =>array
    'name' => string '李文凯唯美艳照.jpg' (length=18)
    'type' => string 'application/octet-stream' (length=24)
    'tmp_name' => string 'F:\LAMPBrother\Environmental\wamp_32\tmp\php9A73.tmp' (length=52)
    'error' => int 0
    'size' => int 0
  1. $post_data = array(
  2. "title"=>"惊艳!!!",
  3. "pic"=>"@d:\李文凯唯美艳照.jpg"
  4. );

上传需要注意的是, 要上传的文件名之前要加上@符号!

cURL批处理:

cURL还有一个高级应用,批处理句柄,这个特性可以同步或异步地处理多个URL连接:
  1. <?php
  2. // 创建一对cURL资源
  3. $ch1 = curl_init();
  4. $ch2 = curl_init();
  5. // 设置URL和相应的选项
  6. curl_setopt($ch1, CURLOPT_URL, "http://www.li-jie.me/");
  7. curl_setopt($ch1, CURLOPT_HEADER, 0);
  8. curl_setopt($ch2, CURLOPT_URL, "http://www.lampbrother.net/");
  9. curl_setopt($ch2, CURLOPT_HEADER, 0);
  10. // 创建批处理cURL句柄
  11. $mh = curl_multi_init();
  12. // 增加2个句柄
  13. curl_multi_add_handle($mh,$ch1);
  14. curl_multi_add_handle($mh,$ch2);
  15. $running=null;
  16. // 执行批处理句柄
  17. do {
  18. usleep(10000);
  19. curl_multi_exec($mh,$running);
  20. } while ($running > 0);
  21. // 关闭全部句柄
  22. curl_multi_remove_handle($mh, $ch1);
  23. curl_multi_remove_handle($mh, $ch2);
  24. curl_multi_close($mh);
  25. ?>

$running会收集来自http://www.li-jie.mehttp://www.lampbrother.net的页面内容,实现多个URL的批量处理!

大家看到了吧,以后采集网站摒弃file_get_contents和fopen吧,把我们强大的cURL用起来,会帮你的web应用增色不少!

原文地址: http://bbs.lampbrother.net/read-htm-tid-121356.html
<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(47) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值