php实现httpclient类示例


Posted in PHP onApril 08, 2014
httpClient::init($httpClient, $args = null);
$httpClient->get($url, $data = null, $cookie = null);
var_dump($httpClient->buffer);
<?php
class httpClient {
 public $buffer = null;  // buffer 获取返回的字符串
 public $referer = null;  // referer 设置 HTTP_REFERER 的网址
 public $response = null; // response 服务器响应的 header 信息
 public $request = null;  // request 发送到服务器的 header 信息
 private $args = null;
 public static function init(&$instanceof, $args = array()) {
  return $instanceof = new self($args);
 }
 private function __construct($args = array()) {
  if(!is_array($args)) $args = array();
  $this->args = $args;
  if(!empty($this->args['debugging'])) {
   ob_end_clean();
   set_time_limit(0);
   header('Content-Type: text/plain; charset=utf-8');
  }
 }
 public function get($url, $data = null, $cookie = null) {
  $parse = parse_url($url);
  $url .= isset($parse['query']) ? '&'. $data : ( $data ? '?'. $data : '' );
  $host = $parse['host'];
  $header  = 'Host: '. $host. "\r\n";
  $header .= 'Connection: close'. "\r\n";
  $header .= 'Accept: */*'. "\r\n";
  $header .= 'User-Agent: '. ( isset($this->args['userAgent']) ? $this->args['userAgent'] : $_SERVER['HTTP_USER_AGENT'] ). "\r\n";
  $header .= 'DNT: 1'. "\r\n";
  if($cookie) $header .= 'Cookie: '. $cookie. "\r\n";
  if($this->referer) $header .= 'Referer: '. $this->referer. "\r\n";
  $options = array();
  $options['http']['method'] = 'GET';
  $options['http']['header'] = $header;
  $response = get_headers($url);
  $this->request = $header;
  $this->response = implode("\r\n", $response);
  $context = stream_context_create($options);
  return $this->buffer = file_get_contents($url, false, $context);
 }
 public function post($url, $data = null, $cookie = null) {
  $parse = parse_url($url);
  $host = $parse['host'];
  $header  = 'Host: '. $host. "\r\n";
  $header .= 'Connection: close'. "\r\n";
  $header .= 'Accept: */*'. "\r\n";
  $header .= 'User-Agent: '. ( isset($this->args['userAgent']) ? $this->args['userAgent'] : $_SERVER['HTTP_USER_AGENT'] ). "\r\n";
  $header .= 'Content-Type: application/x-www-form-urlencoded'. "\r\n";
  $header .= 'DNT: 1'. "\r\n";
  if($cookie) $header .= 'Cookie: '. $cookie. "\r\n";
  if($this->referer) $header .= 'Referer: '. $this->referer. "\r\n";
  if($data) $header .= 'Content-Length: '. strlen($data). "\r\n";
  $options = array();
  $options['http']['method'] = 'POST';
  $options['http']['header'] = $header;
  if($data) $options['http']['content'] = $data;
  $response = get_headers($url);
  $this->request = $header;
  $this->response = implode("\r\n", $response);
  $context = stream_context_create($options);
  return $this->buffer = file_get_contents($url, false, $context);
 }
}
httpClient::init($httpClient, array( 'debugging' => true , 'userAgent' => 'MSIE 15.0' ));
$httpClient->get('http://www.baidu.com', 'name=haowei');
echo $httpClient->request; // 获取 请求头部信息
echo $httpClient->response; // 获取 响应的头部信息
echo $httpClient->buffer; // 获取 网页内容
$httpClient->get('https://3water.com/ServiceLogin/', 'hash='. $time, 'uid=1;users=admin;')
echo $httpClient->buffer;
PHP 相关文章推荐
php magic_quotes_gpc的一点认识与分析
Aug 18 PHP
PHP项目开发中最常用的自定义函数整理
Dec 02 PHP
PHP游戏编程25个脚本代码
Feb 08 PHP
php天翼开放平台短信发送接口实现方法
Dec 22 PHP
PHP概率计算函数汇总
Sep 13 PHP
微信利用PHP创建自定义菜单的方法
Aug 01 PHP
详解ThinkPHP3.2.3验证码显示、刷新、校验
Dec 29 PHP
php处理多图上传压缩代码功能
Jun 13 PHP
laravel框架实现去掉URL中index.php的方法
Oct 12 PHP
实例讲解PHP表单
Jun 10 PHP
50个优秀经典PHP算法大集合 附源码
Aug 26 PHP
PHP dirname(__FILE__)原理及用法解析
Oct 28 PHP
php使用json_encode对变量json编码
Apr 07 #PHP
php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符串
Apr 05 #PHP
PHP中的Memcache详解
Apr 05 #PHP
PHP中使用memcache存储session的三种配置方法
Apr 05 #PHP
PHP包含文件函数include、include_once、require、require_once区别总结
Apr 05 #PHP
PHP6 中可能会出现的新特性预览
Apr 04 #PHP
php实现水仙花数示例分享
Apr 03 #PHP
You might like
暴雪前总裁遗憾:没尽早追赶Dota 取消星际争霸幽灵
2020/03/08 星际争霸
使用php实现从身份证中提取生日
2016/05/09 PHP
PHP中使用foreach()遍历二维数组的简单实例
2016/06/13 PHP
原生JS实现Ajax通过POST方式与PHP进行交互的方法示例
2018/05/12 PHP
php设计模式之模板模式实例分析【星际争霸游戏案例】
2020/03/24 PHP
动态获取复选框checkbox选中个数的jquery代码
2013/06/25 Javascript
jquery等宽输出文字插件使用介绍
2013/09/18 Javascript
基于jquery实现的文字向上跑动类似跑马灯的效果
2014/06/17 Javascript
Javascript常用字符串判断函数代码分享
2014/12/08 Javascript
javascript实现点击后变换按钮显示文字的方法
2015/05/13 Javascript
AngularJs中route的使用方法和配置
2016/02/04 Javascript
Javascript日期格式化format函数的使用方法
2016/08/30 Javascript
JS中append字符串包含onclick无效传递参数失败的解决方案
2016/12/26 Javascript
Bootstrap3 图片(响应式图片&amp;图片形状)
2017/01/04 Javascript
详解闭包解决jQuery中AJAX的外部变量问题
2017/02/22 Javascript
详解nodejs微信公众号开发——3.封装消息响应模块
2017/04/10 NodeJs
简单谈谈CommonsChunkPlugin抽取公共模块
2017/12/31 Javascript
jQuery封装animate.css的实例
2018/01/04 jQuery
JavaScript实现好看的跟随彩色气泡效果
2020/02/06 Javascript
python启动办公软件进程(word、excel、ppt、以及wps的et、wps、wpp)
2009/04/09 Python
详解设计模式中的工厂方法模式在Python程序中的运用
2016/03/02 Python
python 垃圾收集机制的实例详解
2017/08/20 Python
python中实现指定时间调用函数示例代码
2017/09/08 Python
python利用多种方式来统计词频(单词个数)
2019/05/27 Python
Python for循环通过序列索引迭代过程解析
2020/02/07 Python
Python稀疏矩阵及参数保存代码实现
2020/04/18 Python
使用sklearn对多分类的每个类别进行指标评价操作
2020/06/11 Python
Django通过设置CORS解决跨域问题
2020/11/26 Python
Mio Skincare美国官网:身体紧致及孕期身体护理
2017/03/05 全球购物
Ibatis中如何提高SQL Map的性能
2013/05/11 面试题
成人大专生实习期的自我评价
2013/10/02 职场文书
教育专业个人求职信
2013/12/02 职场文书
火锅店营销方案
2014/02/26 职场文书
综合测评自我评价
2015/03/06 职场文书
【D4DJ】美少女DJ企划 动画将于明年冬季开播第2季
2022/04/11 日漫
python中Pyqt5使用Qlabel标签播放视频
2022/04/22 Python