我的Curl操作类,含发送浏览器标识,模拟客户端IP,Cookie
2012-6-3 8:40 Sunday  

分类: PHP 标签: function curl CURLOPT setopt cookie 评论(80) 浏览(119723)

<?php

/**
 * @desc curl 方法的实现
 * @author Aboc QQ:9986584
 * @version 1.0
 *
 */
class Curl {

    /**
     * url地址
     *
     * @var unknown_type
     */
    private $_url = '';
    private $_cookie = '';

    function __construct($url = '') {
        if (!function_exists('curl_init'))
            die('请开启Curl模块');
        $this->_url = $url;
    }

    /**
     * 私有变量赋值
     *
     * @param unknown_type $name
     * @param unknown_type $value
     */
    public function setUrl($value) {
        $this->_url = $value;
    }

    /**
     * 设置cookie的值
     * 形如:hello1=111;hello2=222;hello3=333;hello4=444
     * @param unknown_type $value
     */
    public function setCookie($value) {
        $this->_cookie = $value;
    }

    /**
     * 获取私有变量的值
     *
     * @param unknown_type $name
     * @return unknown
     */
    public function getUrl() {
        return $this->_url;
    }

    /**
     * 将data post到指定的url
     *
     * @param $data array/str都可以 str形如name=aboc&num=bbb
     * @return unknown
     */
    public function post($data, $user_agent = false) {
        if (is_array($data)) {
            $str = array();
            foreach ($data as $key => $value) {
                if (!is_array($value))
                    $str[] = $key . '=' . urlencode($value);
            }
            $str = join('&', $str);
        }
        else {
            $str = $data;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->_url);
        curl_setopt($ch, CURLOPT_POST, count($data));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
        if ($user_agent) {
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0");
        }
        if (!empty($this->_cookie))
            curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie);
        ob_start();
        if (curl_exec($ch)) {
            $result = ob_get_contents();
        } else {
            $result = false;
        }
        ob_end_clean();
        curl_close($ch);
        return $result;
    }

    public function get($user_agent = false,$sendip='') {
        $ch = curl_init($this->_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        //伪造客户端IP
         if($sendip!="")
         curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$sendip, 'CLIENT-IP:'.$sendip));

        if ($user_agent) {
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0");
        }
        if (!empty($this->_cookie))
            curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    function __destruct() {
        
    }

}

?>

至于使用方式,很简单,就不做说明了

 

 

 

 

+1 26

留下你的看法: