• curl_get和curl_post,伪造请求头,绕过防盗链下载文件


    //curl-get
        function curl_get($url, $timeout = 10)
        {
            $ch = curl_init();//初始化
            curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //禁止服务器端的验证
            //伪装请求来源,绕过防盗
            curl_setopt($ch,CURLOPT_REFERER,"http://www.ding.com");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//服务器5秒内没有响应,脚本就会断开连接
            $file_contents = curl_exec($ch);//运行curl
            if (curl_errno($ch)) {
                return curl_error($ch);
            }
            curl_close($ch);
            return $file_contents;
        }
        //http://oss.houdunren.com/lesson_video/水印类/1,水印类简介.mp4
        $url = '要下载文件的地址';
        $filename = '要保存的下载文件名称';
        header("Content-Disposition:attachment;filename=".$filename);
        echo curl_get($url);
    
    function curl_post($url, array $params = array(), $timeout = 5)
    {
        $ch = curl_init();//初始化
        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return ($data);
    }
    

  • 相关阅读:
    JAVA基础——编程练习(二)
    JAVA基础——面向对象三大特性:封装、继承、多态
    JVM内存
    50. Pow(x, n) (JAVA)
    47. Permutations II (JAVA)
    46. Permutations (JAVA)
    45. Jump Game II (JAVA)
    43. Multiply Strings (JAVA)
    42. Trapping Rain Water (JAVA)
    41. First Missing Positive (JAVA)
  • 原文地址:https://www.cnblogs.com/idjl/p/9610557.html
Copyright © 2020-2023  润新知