使用PHP提供的CURL模块采集任意网页 已经封装一个类超级好用 请拿走

本文介绍了一个使用PHP的CURL模块封装的采集类,支持GET和POST请求,适用于大部分网页采集需求。尽管批量采集可能不稳定,但适合内部接口并发处理。通过实例展示了如何抓取A5科技栏目中的文章列表,并存储到文件中,便于后续处理。
摘要由CSDN通过智能技术生成
<?php
/**
 * 类名称:curl
 * 1.支持单个get,post请求
 * 2.支持多个目标未登录get请求
 * 3.支持单个目标并行多个get,post请求
 * 4.支持ajax请求
 * 5.支持自定义header请求
 * 6.支持自定义编码数据请求(该情况比较特殊)
 * 7.支持代理登陆
 * 8.支持自定义来路
 * 9.支持自定义超时
 * 10.支持文件上传
 */

/**
 * demo1 get 请求
 * curl()->get("http://www.baidu.com")->body();
 */

/**
 * demo2 post请求 
 * curl()->post("http://www.xxx.com/say.php",array(
 *      'data' => array(
 *                  'title' => 'test title',
 *                  'content' => 'test content',
 *              ),
 * ))->body();
 */

/**
 * demo3 post请求 ajax请求 并设置cookie
 * curl()->post("http://www.xxx.com/save.php",array(
 *      'data' => array(
 *              'username' => 'test',
 *              'password' => 'test'
 *             ),
 *      'cookie_file' => '/tmp/cookie.txt',
 *      'ajax' => 1,
 * ))->body();
 */

/***
 * demo4 批处理get请求
 * curl()->get(array(
 *  'http://www.xxx.com/test1.php?aaa=111',
 *  'http://www.xxx.com/test2.php?aaa=222',
 *  'http://www.xxx.com/test3.php?aaa=333',
 * ))->body();
 */

/***
 * demo5 批处理post请求 post请求,目前只支持单个网站的批处理post请求
 * curl()->post(array(
 *  'http://www.xxx.com/test1.php',
 *  'http://www.xxx.com/test2.php',
 *  'http://www.xxx.com/test3.php',
 * ),array(
 *   'data' => array(
 *        array(
 *              'uid' => 'aabbccdd',
 *        ),
 *        array(
 *              'uid' => 'eeeeeeee',
 *        ),
 *        array(
 *              'uid' => 'ffffffff',
 *        ),
 *   ),
 *   'cookie_file' => '/tmp/cookie.txt'
 * ))->body();
 */

/**
 * demo6 文件上传
 * curl()->post('填写url地址',array(
 *       'files' => array(
 *          'pic' => '/tmp/a.gif' ,
 *       ),
 *   ))->body();
 */

/**
 * 其他方法未一一列举,可查看源码进行测试
 */

class Curl {
    
    //单例对象
    private static $ins = null;
    
    //请求结果
    private $body = null;
    
    //cookie文件
    private $cookieFile = null;
    
    //支持的请求方法
    private $method = array('get','post');
  
    //禁用初始化方法
    final private function __construct()
    {        
    }
    
    /**
     * 单例化对象
     */
    public static function exec()
    {
        if (self::$ins) {
            return self::$ins;
        }
        return self::$ins = new self();
    }
    
    /**
     * 禁止克隆对象
     */
    public function __clone()
    {
        throw new curlException('错误:不能克隆对象');
    }
    
    /**
     * 调用不存在的方法被调用
     */
    public function __call($method, $args)
    {
        if(!in_array($method,$this->method)) {
            throw new curlException("错误:不支持{$method}方法,支持的方法有" 
                . join(',',$this->method));
        }
        return $this->request($method, $args);
    }
    
    /**
     * 返回执行结果
     */
    public function body() 
    {
        return $this->body;
    }

    /**
     * 执行请求
     * @param type $method
     * @param type $args
     * @return 返回对象本身
     */
    priv
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值