CURL 模拟post和get请求

<?php 

    
    class Curl
    {
        public static function get($url)
        {
            //创建一个新的CURL资源赋给变量$ch;
            $ch = curl_init();
            //设置URL 及其他选项
            curl_setopt($ch, CURLOPT_URL, $url);
            //设置获取的内容但不输出
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            //设置输出的头信息
            // curl_setopt($ch, CURLOPT_HEADER, 0);
            //执行 获取url内容并输出到浏览器
            $output = curl_exec($ch);
            //释放资源
            curl_close($ch);
            //返回获取的网页内容
            return $output;
        }

        public static function post($url, $data)
        {
            //创建一个新的CURL资源赋给变量$ch
            $ch = curl_init();

            if(class_exists('./CURLFile'))//php5.5跟php5.6中的CURLOPT_SAFE_UPLOAD的默认值不同
            {
                curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
            }else
            {
                if(defined('CURLOPT_SAFE_UPLOAD'))
                {
                    curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
                }
            }
            //设置要访问的url地址
            curl_setopt($ch, CURLOPT_URL, $url);
            //设置获取的内容但不输出
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            // 发送一个post的请求
            curl_setopt($ch, CURLOPT_POST, 1);
            // post提交的数据包
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            //执行操作
            $output = curl_exec($ch);
            //关闭curl
            curl_close($ch);
            //返回数据
            return $output;
        }
    }

猜你喜欢

转载自www.cnblogs.com/itsc/p/10468480.html