zoukankan      html  css  js  c++  java
  • curl

    <?php
    /**
     * Created by PhpStorm.
     * User: s
     * Date: 2018/11/20
     * Time: 10:07
     */
    class CurlClass
    {
        protected $_pdo;
        public function __construct()
        {
            $this->_pdo = new PDO("mysql:host=127.0.0.1;dbname=student","root","root");
        }
    
        //
        public function get($url)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "$url");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,10);
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
            $output = curl_exec($ch);
            curl_close($ch);
            return $output;
        }
        //正则
        public function name($url)
        {
            $data = $this->get($url);
            $zheng_ze='#<div class="right">s*<a.*?href="(.*?)".*?">(.*?)</a>s*<a.*?<span class="cboy">(.*?)</span>#';
            preg_match_all($zheng_ze,$data,$arr);
            $arr=array_slice($arr,1);
            //var_dump($arr);die;
            foreach ($arr[0] as $k=>$v)
            {
                $ww=[
                    'url'=>$arr[0][$k],
                    'title'=>$arr[1][$k],
                    'img'=>$arr[2][$k]
                ];
                $sql="insert into news(id,title,tu,jian) VALUES (NULL ,'{$ww['title']}','{$ww['url']}','{$ww['img']}')";
                $aa=$this->_pdo->exec($sql);
            }
            if($aa)
            {
                return "添加成功";
            }
            else
            {
                return "添加失败";
            }
        }
    }


    public function getCurl($url,$type='get',$data,$https='true')
        {
            $ch= curl_init($url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
            if($type=='post')
            {
                curl_setopt($ch,CURLOPT_POST,1);
                curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
            }
            if($https=='false')
            {
                curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
                curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
            }
            $data=curl_exec($ch);
            curl_close($ch);
            return $data;
        }

      

  • 相关阅读:
    day18:正则表达式和re模块
    finance1:专业词汇
    Iterator & Iterable 和 Comparable&Comparator
    pandas中三种索引方式(.loc.iloc.ix)
    XML序列化与反序列化
    控件蓝图里控件绑定问题
    Mouse Properties(鼠标属性)
    cSV
    从父控件移除控件
    是否显示鼠标指针
  • 原文地址:https://www.cnblogs.com/stj123/p/12323278.html
Copyright © 2011-2022 走看看