zoukankan      html  css  js  c++  java
  • 一个网页抓取的类支持get+post+cookie存储

    前段时间提取了一个工具类,分享给大家:

    <?php
    class httpconnector {
        private $curl;
        private $cookie;
        private $kv;
        function __construct(){
            $this->kv = new SaeKV();
            $this->kv->init();
            if($data=$this->kv->get("cookie"))
              $this->cookie=$data;
     
        }
        public function get($url) {
            $this->curl = curl_init();
            curl_setopt($this->curl, CURLOPT_URL, $url);
            curl_setopt($this->curl, CURLOPT_HEADER, 1);
            curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
            curl_setopt($this->curl, CURLOPT_COOKIE, $this->cookie);
            curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($this->curl);
            curl_close($this->curl);
            preg_match_all("/Set-Cookie:(.*?);/", $data, $match, PREG_SET_ORDER);
            foreach ($match as $r) {
                if ($this->cookie != '') {
                    $this->cookie = $this->cookie . ';';
                }
                if (isset($r[1])) {
                    $this->cookie .= trim(str_replace("
    ", "", $r[1]));
                }
            }
            $this->kv->set("cookie",$this->cookie);
            return $data;
     
        }
        public function post($url, $params) {
            $this->curl = curl_init();
            curl_setopt($this->curl, CURLOPT_URL, $url);
            curl_setopt($this->curl, CURLOPT_HEADER, 1);
            curl_setopt($this->curl, CURLOPT_COOKIE, $this->cookie);
            curl_setopt($this->curl, CURLOPT_POST, 1);
            curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params);
            curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($this->curl);
            curl_close($this->curl);
            preg_match_all("/Set-Cookie:(.*?);/", $data, $match, PREG_SET_ORDER);
            foreach ($match as $r) {
                if ($this->cookie != '') {
                    $this->cookie = $this->cookie . ';';
                }
                if (isset($r[1])) {
                    $this->cookie .= trim(str_replace("
    ", "", $r[1]));
                }
            }
            $this->kv->set("cookie",$this->cookie);
            return $data;
     
        }
    }
    ?>
    
     
    
  • 相关阅读:
    Android 8.0编译过程
    Ubuntu下映射网络驱动器
    Android 指定调用已安装的某个“相机”App
    sendMessage 与 obtainMessage (sendToTarget)比较
    Linux tee命令
    Android P(9.0) userdebug版本执行adb remount失败
    adb shell get/setprop, setenforce...
    Bluedroid: 蓝牙协议栈源码剖析
    android o logcat read: unexpected EOF!
    Winform 打包 混淆 自动更新
  • 原文地址:https://www.cnblogs.com/mengdejun/p/3375991.html
Copyright © 2011-2022 走看看