其他分享
首页 > 其他分享> > 一个稍微整理过的curl函数

一个稍微整理过的curl函数

作者:互联网

if (!function_exists('curlRequest'))
{
    /**
    * cURL请求
    * @author ligeliang
    * @param  [type] $url  [description]
    * @param  [type] $data 为空get请求,不为空post请求
    * @return [type]       [description]
    */
    function curlRequest($url, $data = null)
    {
        // 初始化
        $ch = curl_init();
        // 设置抓取的url
        curl_setopt($ch, CURLOPT_URL, $url);

        // 设置头文件的信息作为数据流输出
        curl_setopt($ch, CURLOPT_HEADER, 0);
        // 设置获取的信息以文件流的形式返回,而不是直接输出。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // 取全等于0,避免url参数中含有https的特殊情况
        if (strpos($url, 'https://') === 0) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // https请求 不验证hosts
        }

        if (!empty($data)) {
            // 设置post方式提交
            curl_setopt($ch, CURLOPT_POST, 1);
            // 设置post数据
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        }

        // 执行命令
        $data = curl_exec($ch);
        // 关闭URL请求
        curl_close($ch);
        return $data;
    }
}

 

标签:ch,函数,url,稍微,curl,data,CURLOPT,setopt
来源: https://www.cnblogs.com/songkaixin/p/11126840.html