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;
        }

      

  • 相关阅读:
    GET与POST类型接口
    sql查询优化 索引优化
    临时表操作
    sqlserver group by 分组使用详解
    js调用正则表达式
    后台对象转JSON字符串传到前台,前台JSON字符串转对象绑定标签赋值
    string 数组转 int 数组
    巧用XML格式数据传入存储过程转成表数据格式
    存储过程规范写法
    WebApi
  • 原文地址:https://www.cnblogs.com/stj123/p/12323278.html
Copyright © 2011-2022 走看看