<?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
使用PHP提供的CURL模块采集任意网页 已经封装一个类超级好用 请拿走
最新推荐文章于 2022-04-25 00:13:45 发布
本文介绍了一个使用PHP的CURL模块封装的采集类,支持GET和POST请求,适用于大部分网页采集需求。尽管批量采集可能不稳定,但适合内部接口并发处理。通过实例展示了如何抓取A5科技栏目中的文章列表,并存储到文件中,便于后续处理。
摘要由CSDN通过智能技术生成