加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

一个稍微整理过的curl函数

发布时间:2020-12-13 23:15:38 所属栏目:Linux 来源:网络整理
导读:if (! function_exists (‘curlRequest‘ )){ /* * * cURL请求 * @author ligeliang * @param [type] $url [description] * @param [type] $data 为空get请求,不为空post请求 * @return [type] [description] */ function curlRequest( $url , $data = null
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;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读