zoukankan      html  css  js  c++  java
  • CURL PHP模拟浏览器get和post

    模拟浏览器get和post数据需要经常用到的类,

    在这里收藏了几个不错的方法

    方法一

    <?php
    define ( 'IS_PROXY', true ); //是否启用代理
    /* cookie文件 */
    $cookie_file = dirname ( __FILE__ ) . "/cookie_" . md5 ( basename ( __FILE__ ) ) . ".txt"; // 设置Cookie文件保存路径及文件名
    /*模拟浏览器*/
    $user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
    
    function vlogin($url, $data) { // 模拟登录获取Cookie函数
        $curl = curl_init (); // 启动一个CURL会话
        if (IS_PROXY) {
            //以下代码设置代理服务器
            //代理服务器地址
            curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS ['proxy'] );
        }
        curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 ); // 从证书中检查SSL加密算法是否存在
        curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS ['user_agent'] ); // 模拟用户使用的浏览器
        @curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
        curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
        curl_setopt ( $curl, CURLOPT_POST, 1 ); // 发送一个常规的Post请求
        curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); // Post提交的数据包
        curl_setopt ( $curl, CURLOPT_COOKIEJAR, $GLOBALS ['cookie_file'] ); // 存放Cookie信息的文件名称
        curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file'] ); // 读取上面所储存的Cookie信息
        curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 设置超时限制防止死循环
        curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec ( $curl ); // 执行操作
        if (curl_errno ( $curl )) {
            echo 'Errno' . curl_error ( $curl );
        }
        curl_close ( $curl ); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }
    
    function vget($url) { // 模拟获取内容函数
        $curl = curl_init (); // 启动一个CURL会话
        if (IS_PROXY) {
            //以下代码设置代理服务器
            //代理服务器地址
            curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS ['proxy'] );
        }
        curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 ); // 从证书中检查SSL加密算法是否存在
        curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS ['user_agent'] ); // 模拟用户使用的浏览器
        @curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
        curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
        curl_setopt ( $curl, CURLOPT_HTTPGET, 1 ); // 发送一个常规的Post请求
        curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file'] ); // 读取上面所储存的Cookie信息
        curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 设置超时限制防止死循环
        curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec ( $curl ); // 执行操作
        if (curl_errno ( $curl )) {
            echo 'Errno' . curl_error ( $curl );
        }
        curl_close ( $curl ); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }
    
    function vpost($url, $data) { // 模拟提交数据函数
        $curl = curl_init (); // 启动一个CURL会话
        if (IS_PROXY) {
            //以下代码设置代理服务器
            //代理服务器地址
            curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS ['proxy'] );
        }
        curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 ); // 从证书中检查SSL加密算法是否存在
        curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS ['user_agent'] ); // 模拟用户使用的浏览器
        @curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
        curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
        curl_setopt ( $curl, CURLOPT_POST, 1 ); // 发送一个常规的Post请求
        curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); // Post提交的数据包
        curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file'] ); // 读取上面所储存的Cookie信息
        curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 设置超时限制防止死循环
        curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec ( $curl ); // 执行操作
        if (curl_errno ( $curl )) {
            echo 'Errno' . curl_error ( $curl );
        }
        curl_close ( $curl ); // 关键CURL会话
        return $tmpInfo; // 返回数据
    }
    
    function delcookie($cookie_file) { // 删除Cookie函数
        unlink ( $cookie_file ); // 执行删除
    }
    ?>
    

    方法二

    <?php
    /**
    *File:curl.class.php
    *CURL封装类,本类大部分操作均支持链式操作
    *
    *example:
    *
    *$curl=newCurl($url);
    *$curl->exec();
    *//发送post数据
    *$curl->post(array('username'=>'用户名'))->exec();
    */
    
    
    classCurl{
     private$ch;
     private$flag_if_have_run=false;
     private$has_cloase=true;
    
     publicfunction__construct($url='',$forgeIP=false){
     $this->init($url,$forgeIP);
     }
    
     /**
     *初始化CURL。如果CURL未被关闭,则先关闭
     *
     *@paramtype$url
     *@return\Common\Library\Curl
     */
     publicfunctioninit($url='',$forgeIP=false){
     if(!$this->has_cloase){//如果上一次连接尚未结束,则先关闭
     $this->close();
     }
    
     if($forgeIP){//伪造IP,将IP伪造为访问者的IP
     if(Validate::isIPAddress($forgeIP)){
     $ip=$forgeIP;
     }else{
     $ip=$_SERVER['SERVER_ADDR'];
     }
     $this->set_ip($ip);
     }
    
     $this->ch=curl_init($url);
     curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1);
     $this->has_cloase=false;
    
     return$this;
     }
    
    
     publicfunctionsetUrl($url){
     curl_setopt($this->ch,CURLOPT_URL,$url);
    
     return$this;
     }
    
     publicfunctionclose(){
     if(!$this->has_close){
     curl_close($this->ch);
     $this->has_cloase=true;
     }
     }
    
     publicfunction__destruct(){
     $this->close();
     }
    
     /**
     *设置页面超时时间,支持链式操作
     *
     *@paramtype$timeout
     *@return\Common\Library\Curl
     */
     publicfunctionset_time_out($timeout){
     curl_setopt($this->ch,CURLOPT_TIMEOUT,intval($timeout));
     return$this;
     }
    
     /**
     *伪造来源路径
     *
     *@paramtype$referer
     *@return\Common\Library\Curl
     */
     publicfunctionset_referer($referer){
     if(!empty($referer)){
     curl_setopt($this->ch,CURLOPT_REFERER,$referer);
     }
     return$this;
     }
    
     /**
     *设置cookie
     *本方法仅发送cookie信息到远端,不保存远端返回的cookie信息
     *
     *@paramtype$cookie_file cookie文件的存储路径
     *@return\Common\Library\Curl
     */
     publicfunctionload_cookie($cookie_file){
     $this->_checkCookie($cookie_file);
     curl_setopt($this->ch,CURLOPT_COOKIEFILE,$cookie_file);
     return$this;
     }
    
     /**
     *设置cookie
     *发送cookie到远端,并保存远端返回的cookie
     *
     *@paramtype$cookie_file
     *@return\Common\Library\Curl
     */
     publicfunctioncookie($cookie_file){
     $this->_checkCookie($cookie_file);
     curl_setopt($this->ch,CURLOPT_COOKIEFILE,$cookie_file);
     curl_setopt($this->ch,CURLOPT_COOKIEJAR,$cookie_file);
     return$this;
     }
    
     /**
     *设置cookie
     *本方法将不发送cookie信息,仅接收返回的cookie并保存
     *
     *@paramtype$cookie_file
     *@return\Common\Library\Curl
     */
     publicfunctionsave_cookie($cookie_file=""){
     //设置缓存文件,例如a.txt
     if(empty($cookie_file)){
     $cookie_file=tempnam('./','cookie');
     }
     $this->_checkCookie($cookie_file);
     curl_setopt($this->ch,CURLOPT_COOKIEJAR,$cookie_file);
     return$this;
     }
    
     privatefunction_checkCookie($cookie_file){
     if(!\Think\Storage::has($cookie_file)){
     \Think\Storage::put($cookie_file,'');
     }
     }
    
     /**
     *执行curl请求
     *
     *@returntype
     */
     publicfunctionexec(){
     $str=curl_exec($this->ch);
     $this->flag_if_have_run=true;
     return$str;
     }
    
     /**
     *设置发送POST请求。没有调用过该方法默认使用GET方法提交
     *
     *@paramtype$postData post的数据,支持数组和“xxx=1&x=2”两种格式
     *@return\Common\Library\Curl
     */
     publicfunctionpost($postData){
     curl_setopt($this->ch,CURLOPT_POST,1);
    //echo($postQuery);die;
     curl_setopt($this->ch,CURLOPT_POSTFIELDS,$postData);
     return$this;
     }
    
     /**
     *获取curl的信息
     *
     *@returntype
     *@throwsException
     */
     publicfunctionget_info(){
     if($this->flag_if_have_run==true){
     returncurl_getinfo($this->ch);
     }else{
     thrownewException("<h1>需先运行(执行exec),再获取信息</h1>");
     }
     }
    
     /**
     *设置代理
     *
     *@paramtype$proxy
     *@return\Common\Library\Curl
     */
     publicfunctionset_proxy($proxy){
     //设置代理,例如'68.119.83.81:27977'
     curl_setopt($this->ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
     curl_setopt($this->ch,CURLOPT_PROXY,$proxy);
     return$this;
     }
    
     /**
     *设置请求的IP
     *
     *@paramtype$ip
     *@returntype
     */
     publicfunctionset_ip($ip=''){
     if(!empty($ip)){
     curl_setopt($this->ch,CURLOPT_HTTPHEADER,array("X-FORWARDED-FOR:$ip","CLIENT-IP:$ip"));
     }
     return$ip;
     }
    
    }
    
  • 相关阅读:
    python 扁平列表转树状字典
    在Windows Server2012中通过DockerToolbox 一步一步搭建Mysql 数据库存运行环境
    腾讯云ubuntu服务器安装图像化界面并实现远程登陆
    IIS、apache、tomcat服务器虚拟主机配置
    微信商家二维码到底是什么
    线程与线程锁---python版本(附带线程锁实例)
    pip更新后仍旧是使用的旧版本
    pip更新后仍旧是使用的旧版本
    H5-LocalStorage
    Python摄像头抓拍的彩色图像转为灰度图、二值化和调整图片尺寸(实例)
  • 原文地址:https://www.cnblogs.com/aibk/p/7215462.html
Copyright © 2011-2022 走看看