<?php
//自定义curl类,模拟各种请求
class MyCurl
{
    public static function get($url)
    {
        //1 初始curl
        $ch = curl_init($url);
        //2 设置参数
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//是否返回数据
        curl_setopt($ch, CURLOPT_HEADER, false);//是否显示请求头
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);//请求超时时间
        //3执行
        $content = curl_exec($ch);
        //4 关闭
        curl_close($ch);
        return $content;
    }


    public static function post($url,$data)
    {
        //1 初始curl
        $ch = curl_init($url);
        //2 设置参数
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//是否返回数据
        curl_setopt($ch, CURLOPT_HEADER, false);//是否显示请求头
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);//请求超时时间
        curl_setopt($ch, CURLOPT_POST, true);//使用post请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//使用post请求的参数
        //3执行
        $content = curl_exec($ch);
        //4 关闭
        curl_close($ch);
        return $content;
    }
}

$curl = new MyCurl();
$content = $curl->get('http://www.baidu.com');
var_dump($content);

如若时光萧瑟去丶
111 声望9 粉丝

weakChickenPeng.