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

 


  

posted @ 2020-02-17 19:56  PHP小媛  阅读(183)  评论(0编辑  收藏  举报