php基础九(通过cURL来做小偷程序)

更新时间:2020-05-11 编辑:未知 关注人次:0 云搜索


  1. <!-- 第十五章 通过cURL来做小偷程序 -->
  2.  
    <?php
  3.  
    //如何请求微信、地图等第三方API呢?
  4.  
    //这个时候就需要使用到——cURL。cURL中文翻译过来叫做基于URL的函数库。
  5.  
    //它的主要功能是:使用相关的函数模拟协议请求。
  6.  
    //例如:
  7.  
    //1.模拟表单向某个地址发送数据
  8.  
    //2.在无验证码的情况下模拟表单完成用户登录
  9.  
    //3.上传某个文件到远程服务器
  10.  
    //4.请求远程服务器提供的某些功能
  11.  
     
  12.  
    //curl支持dict,file,ftp,ftps,gopher,http,https,imap,imaps,idap,idaps,
  13.  
    //pop3,pop3s,rtmp,rtsp,smtp,smtps,teInet和tftp协议。
  14.  
    //curl同时也支持HTTPS认证、HTTP的POST、HTT和PPUT、FTP上传(这个
  15.  
    //也能通过PHP的FTP扩展完成)、HTTP基于表单的上传、代理、cookies和用户名+密码的认证。
  16.  
    ?>
  17.  
     
  18.  
    <!-- curl使用方法和步骤 -->
  19.  
    <?php
  20.  
    //1.初始化curl资源
  21.  
    //2.参数设置请求的协议地址
  22.  
    //3.设置是否返回请求结果
  23.  
    //4.设置发送数据(无发送数据可不设置)
  24.  
    //5.其他的参数信息设置(按实际工作需要决定)
  25.  
    //6.执行或执行得到返回结果
  26.  
    //7.关闭curl资源
  27.  
    $ch = curl_init();
  28.  
    curl_setopt($ch, CURLOPT_URL, "http://www.php.cn");
  29.  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  30.  
    curl_setopt($ch, CURLOPT_HEADER, 0);
  31.  
    $output = curl_exec($ch);
  32.  
    curl_close($ch);
  33.  
    print_r($output);
  34.  
    ?>
  35.  
     
  36.  
    <!-- curl 自定义get方法抓取网页 -->
  37.  
    <?php
  38.  
    $content = get('http://www.xmtnews.com/events');
  39.  
    preg_match('/<section class="ov">(.*?)<div class="hr-10"><\/div>/mis', $content,$match);
  40.  
    $area = $match[1];
  41.  
    preg_match_all('/<h3><a href="(.*?)" title=".*?"
  42.  
    class="headers" target="_blank">(.*?)<\/a><\/h3/>', $area, $find);
  43.  
    var_dump($find);
  44.  
    function get($find){
  45.  
    $ch = curl_init();
  46.  
    curl_setopt($ch, CURLOPT_URL, $url);
  47.  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  48.  
    curl_setopt($ch, CURLOPT_HEADER, 0);
  49.  
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  50.  
    $output = curl_exec($ch);
  51.  
    curl_close($ch);
  52.  
    }
  53.  
    ?>
  54.  
     
  55.  
    <!-- curl使用post发送数据 -->
  56.  
    <?php
  57.  
    function post($url, $data){
  58.  
    //初始化init方法
  59.  
    $ch = curl_init();
  60.  
    //指定URL
  61.  
    curl_setopt($ch, CURLOPT_URL, $url);
  62.  
    //设置请求后返回结果
  63.  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  64.  
    //声明使用POST方式来发送
  65.  
    curl_setopt($ch, CURLOPT_POST, 1);
  66.  
    //发送什么数据呢
  67.  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  68.  
    //忽略证书
  69.  
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  70.  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  71.  
    //忽略header头信息
  72.  
    curl_setopt($ch, CURLOPT_HEADER, 0);
  73.  
    //设置超时时间
  74.  
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  75.  
    //发送请求
  76.  
    $output = curl_exec($ch);
  77.  
    //关闭curl
  78.  
    curl_close($ch);
  79.  
    //返回数据
  80.  
    return $output;
  81.  
    }
  82.  
    ?>
  83.  
     
  84.  
本文地址: http://www.4435.cn/a/xinwenguandian/xinwenzixun/2020/0511/32367.html ,转载请注明出处。

服务支持

我们珍惜您每一次在线询盘,有问必答,用专业的态度,贴心的服务。

让您真正感受到我们的与众不同!