码迷,mamicode.com
首页 > Web开发 > 详细

CURL 模拟post和get请求

时间:2019-03-03 23:47:27      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:ati   public   UNC   url   file   一个   code   访问   set   

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

 

CURL 模拟post和get请求

标签:ati   public   UNC   url   file   一个   code   访问   set   

原文地址:https://www.cnblogs.com/itsc/p/10468480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!