一个网页抓取的类支持get+post+cookie存储
2013-10-18 14:16
323 查看
前段时间提取了一个工具类,分享给大家:
<?php class httpconnector { private $curl; private $cookie; private $kv; function __construct(){ $this->kv = new SaeKV(); $this->kv->init(); if($data=$this->kv->get("cookie")) $this->cookie=$data; } public function get($url) { $this->curl = curl_init(); curl_setopt($this->curl, CURLOPT_URL, $url); curl_setopt($this->curl, CURLOPT_HEADER, 1); curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); curl_setopt($this->curl, CURLOPT_COOKIE, $this->cookie); curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($this->curl); curl_close($this->curl); preg_match_all("/Set-Cookie:(.*?);/", $data, $match, PREG_SET_ORDER); foreach ($match as $r) { if ($this->cookie != '') { $this->cookie = $this->cookie . ';'; } if (isset($r[1])) { $this->cookie .= trim(str_replace("\r\n", "", $r[1])); } } $this->kv->set("cookie",$this->cookie); return $data; } public function post($url, $params) { $this->curl = curl_init(); curl_setopt($this->curl, CURLOPT_URL, $url); curl_setopt($this->curl, CURLOPT_HEADER, 1); curl_setopt($this->curl, CURLOPT_COOKIE, $this->cookie); curl_setopt($this->curl, CURLOPT_POST, 1); curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params); curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($this->curl); curl_close($this->curl); preg_match_all("/Set-Cookie:(.*?);/", $data, $match, PREG_SET_ORDER); foreach ($match as $r) { if ($this->cookie != '') { $this->cookie = $this->cookie . ';'; } if (isset($r[1])) { $this->cookie .= trim(str_replace("\r\n", "", $r[1])); } } $this->kv->set("cookie",$this->cookie); return $data; } } ?>
相关文章推荐
- flex时间创建和比较总结
- 泛型与非泛型
- nginx配置安装
- 提升代码内外部质量的22条经验
- Android:Service非绑定
- 提升代码内外部质量的22条经验
- 【unity3d】实现物体双击。
- iOS学习:UILabel和sizeWithFont方法,可用于动态调整UILabel大小
- js 使FORM表单的所有元素不可编辑的示例代码
- iOS学习:UILabel和sizeWithFont方法,可用于动态调整UILabel大小
- 发现未授权登录用户怎么办
- ICCV 2013的人脸特征点检评测及代码
- ant 执行修改testng报告样式
- mysql innodb 所有的库都在一个文件里
- NGUI系列教程四(自定义Atlas,Font)
- SQL注入攻击原理及防范
- 很不错的Utility库,C#4扩展 各种功能齐全,两行代码搞定图片转字符
- 分享 day1
- ArcGIS Viewer for Flex 学会使用flex做webGIS widget控件
- javascript中负数算术右移、逻辑右移的奥秘探索