• 封装curl类,post get方法实现网站请求


    <?php
    class RamDemo
    {
        //get方法
        function RamGet($url,$arr)
        {
            if($arr!=''){
                $ar=array();
                foreach($arr as $k=>$v){
                    $ar[]=$k.'='.$v;
                   }
                $url=$url.'?'.implode('&',$ar);
            }
            //return $url;die;
            return $this->curl($url,$arr,'GET',false,false);
        }
        //post方法
        function RamPost($url,$arr)
        {
            return $this->curl($url,$arr,'POST',false,false);
        }
        //curl方法
        function  curl($url,$data,$method,$setcooke=false,$cookie_file=false){
            $ch = curl_init();     //1.初始化
            curl_setopt($ch,CURLOPT_URL, $url); //2.请求地址
            curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$method);//3.请求方式
            //4.参数如下    
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
            curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
            curl_setopt($ch,CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0'); //指定请求方式(浏览器)
            curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
            curl_setopt($ch,CURLOPT_AUTOREFERER,1);
            if($method=="POST"){//5.post方式的时候添加数据    
                curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
            }
            if($setcooke==true){
                 //把生成的cookie保存在指定的文件中
                curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie_file);
            }else{
                //直接从文件中读取cookie信息
                curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie_file);
            }
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            $output = curl_exec($ch);

            if (curl_errno($ch)) {
                return curl_error($ch);
            }
            curl_close($ch);
            return $output;
        }
    }
    ?>

  • 相关阅读:
    一些来不及整理的链接
    TensorFlow 入门 下(自用)
    TensorFlow 入门 上(自用)
    Tensorflow 深度学习简介(自用)
    解决flutter的image_cropper组件引入报错问题
    微信小程序自定义导航栏
    layui.table图片显示不全和404问题
    php设计模式2
    PHP常用设计模式讲解
    解决git pull出现: Your local changes to the following files would be overwritten by merge: ...的问题
  • 原文地址:https://www.cnblogs.com/nuanxin/p/5651536.html
Copyright © 2020-2023  润新知