fastadmin的Curl类

分类首页日期2年前访问1232评论0

引用

use app\common\library\Curl;


使用


$data = Curl::curl_request($url);


类方法(在common\library下新建一个Curl.php文件)

<?php
namespace app\common\library;
class Curl
{
    //可以发送get和post的请求方式
    public static function curl_request($url, $data = null, $method = 'post', $https = true)
    {
        //1.初识化curl
        $ch = curl_init($url);

        //2.根据实际请求需求进行参数封装
        //返回数据不直接输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //如果是https请求
        if ($https === true) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        //如果是post请求
        if ($method === 'post') {
            //开启发送post请求选项
            curl_setopt($ch, CURLOPT_POST, true);
            //发送post的数据
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //3.发送请求
        $result = curl_exec($ch);
        //4.返回返回值,关闭连接
        curl_close($ch);
        return $result;
    }

    public static function http_request($url, $param = array())
    {
        $httph = curl_init($url);
        curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($httph, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
        curl_setopt($httph, CURLOPT_POST, 1);//设置为POST方式
        curl_setopt($httph, CURLOPT_POSTFIELDS, json_encode($param, true));
        curl_setopt($httph, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($httph, CURLOPT_HEADER, FALSE);
        $rst = curl_exec($httph);
        curl_close($httph);
        return $rst;
    }

    public static function downFile($url, $savePath = './uploads/Excel/')
    {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);  //需要response header
        curl_setopt($ch, CURLOPT_NOBODY, FALSE);  //需要response body
        $response = curl_exec($ch);
        //分离header与body
        $header = '';
        $body = '';
        if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
            $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); //头信息size
            $header = substr($response, 0, $headerSize);

            $body = substr($response, $headerSize);
        }
        curl_close($ch);
        //文件名
        $arr = array();
//        if (preg_match('/filename="*"/', $header, $arr)) {
        if (preg_match('/filename=([0-9A-Z_.]+)/i', $header, $arr)) {

            $file = date('Ym') . '/' . $arr[1];

            $fullName = rtrim($savePath, '/') . '/' . $file;
            //创建目录并设置权限
            $basePath = dirname($fullName);
            if (!file_exists($basePath)) {
                @mkdir($basePath, 0777, true);
                @chmod($basePath, 0777);
            }
            if (file_put_contents($fullName, $body)) {
                return $fullName;
            }
        }
        return false;
    }


}