php的curl封装类

之前一直做爬虫相关的,每次自己去写一系列curl_setopt()函数太繁琐,我于是封装了如下curl请求类。

<?php
/**
 * @author freephp
 * @date 2015-11-13
 *
 */
class MyCurl {    
    private static  $url = ''; // 访问的url
    private static $oriUrl = ''; // referer url
    private static $data = array(); // 可能发出的数据 post,put
    private   static $method; // 访问方式,默认是GET请求
    
    public static function send($url, $data = array(), $method = 'get') {
        if (!$url) exit('url can not be null');
        self::$url = $url;
        self::$method = $method;
        $urlArr = parse_url($url);
        self::$oriUrl = $urlArr['scheme'] .'://'. $urlArr['host'];
        self::$data = $data;
        if ( !in_array(
                self::$method,
                array('get', 'post', 'put', 'delete')
             )
           ) {
                    exit('error request method type!');
             }
        
                $func = self::$method . 'Request';
                return self::$func(self::$url);
    }
    /**
     * 基础发起curl请求函数
     * @param int $is_post 是否是post请求
     */
    private  function doRequest($is_post = 0) {
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, self::$url);//抓取指定网页
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        // 来源一定要设置成来自本站
        curl_setopt($ch, CURLOPT_REFERER, self::$oriUrl);
        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        if($is_post == 1) curl_setopt($ch, CURLOPT_POST, $is_post);//post提交方式
        if (!empty(self::$data)) {
            self::$data = self::dealPostData(self::$data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, self::$data);
        }
        
        $data = curl_exec($ch);//运行curl    
        curl_close($ch);
        return $data;
    }
    /**
     * 发起get请求
     */
    public function getRequest() {
        return self::doRequest(0);
    }
    /**
     * 发起post请求
     */
    public function postRequest() {
        return self::doRequest(1);
    }
    /**
     * 处理发起非get请求的传输数据
     * 
     * @param array $postData
     */
    public function dealPostData($postData) {
        if (!is_array($postData)) exit('post data should be array');
        foreach ($postData as $k => $v) {
            $o .= "$k=" . urlencode($v) . "&";
        }
        $postData = substr($o, 0, -1);
        return $postData;
    }
    /**
     * 发起put请求
     */
    public function putRequest($param) {
        return self::doRequest(2);
    }
    
    /**
     * 发起delete请求
     */
    public function deleteRequest($param) {
        return self::doRequest(3);
    }
    
}
/* $curl = new MyCurl('http://www.jumei.com',array(), 'get');
$res = $curl->send(); */
$res = MyCurl::send('http://www.ipip.net/ip.html',array('ip' => '61.142.206.145'),'post');

var_dump($res);die();

考虑到可能会有循环调用的可能和高并发,为了减少内存堆的不必要消耗,我只对客户端调用提供静态方法。为了类的单一职责,MyCurl只做发请求和返回data的作用,对返回数据的处理交给其他代码。

posted @ 2015-11-13 16:52  freephp  阅读(9615)  评论(3编辑  收藏  举报