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中执行MYSQL事务解决数据写入不完整等情况
Jan 07 PHP
使用PHP函数scandir排除特定目录
Jun 12 PHP
php遍历数组的4种方法总结
Jul 05 PHP
PHP+FFMPEG实现将视频自动转码成H264标准Mp4文件
Sep 24 PHP
PHP中round()函数对浮点数进行四舍五入的方法
Nov 19 PHP
简单实用的PHP防注入类实例
Dec 05 PHP
PHP表单提交后引号前自动加反斜杠的原因及三种办法关闭php魔术引号
Sep 30 PHP
PHPWind9.0手动屏蔽验证码解决后台关闭验证码但是依然显示的问题
Aug 12 PHP
Zend Framework框架中实现Ajax的方法示例
Jun 27 PHP
PHP实现的链式队列结构示例
Sep 15 PHP
Yii框架使用PHPExcel导出Excel文件的方法分析【改进版】
Jul 24 PHP
php 实现银联商务H5支付的示例代码
Oct 12 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
ADODB的数据库封包程序库
2006/12/31 PHP
PHP 编写大型网站问题集
2010/05/07 PHP
php连接oracle数据库的核心步骤
2016/05/26 PHP
php中html_entity_decode实现HTML实体转义
2018/06/13 PHP
laravel-admin表单提交隐藏一些数据,回调时获取数据的方法
2019/10/08 PHP
解读JavaScript代码 var ie = !-[1,] 最短的IE判定代码
2011/05/28 Javascript
基于jquery的防止大图片撑破页面的实现代码(立即缩放)
2011/10/24 Javascript
20个最新的jQuery插件
2012/01/13 Javascript
JavaScript原生对象之String对象的属性和方法详解
2015/03/13 Javascript
JQuery zClip插件实现复制页面内容到剪贴板
2015/11/02 Javascript
基于JavaScript实现智能右键菜单
2016/03/02 Javascript
JS中的==运算: [''] == false —&gt;true
2016/07/24 Javascript
js从输入框读取内容,比较两个数字的大小方法
2017/03/13 Javascript
ionic2打包android时gradle无法下载的解决方法
2017/04/05 Javascript
js制作简单的音乐播放器的示例代码
2017/08/28 Javascript
layui关闭弹窗后刷新主页面和当前更改项的例子
2019/09/06 Javascript
[15:57]教你分分钟做大人:斧王
2014/10/30 DOTA
在python中的socket模块使用代理实例
2014/05/29 Python
举例讲解Python中字典的合并值相加与异或对比
2016/06/04 Python
Python使用迭代器捕获Generator返回值的方法
2017/04/05 Python
Python 的类、继承和多态详解
2017/07/16 Python
Python定义二叉树及4种遍历方法实例详解
2018/07/05 Python
Python实现全排列的打印
2018/08/18 Python
Python实现爬取亚马逊数据并打印出Excel文件操作示例
2019/05/16 Python
学习python分支结构
2019/05/17 Python
如何使用Python多线程测试并发漏洞
2019/12/18 Python
Python实现壁纸下载与轮换
2020/10/19 Python
英国最大线上综合鞋类商城:Office
2017/12/08 全球购物
JackJones官方旗舰店:杰克琼斯男装
2018/03/27 全球购物
马来西亚太阳镜、眼镜和隐形眼镜网上商店:Focus Point
2018/12/13 全球购物
计算机专业个人求职自荐信
2013/09/21 职场文书
《落花生》教学反思
2014/02/25 职场文书
2014年营业员工作总结
2014/11/18 职场文书
学校运动会简讯
2015/07/20 职场文书
Go中的条件语句Switch示例详解
2021/08/23 Golang
CSS link与@import的区别和用法解析
2023/05/07 HTML / CSS