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程序?
Dec 08 PHP
个人站长制做网页常用的php代码
Mar 03 PHP
有关phpmailer的详细介绍及使用方法
Jan 28 PHP
PHP5中Cookie与 Session使用详解
Apr 30 PHP
PHP中执行cmd命令的方法
Oct 11 PHP
thinkphp特殊标签用法概述
Nov 24 PHP
PHP基于MySQL数据库实现对象持久层的方法
Jun 17 PHP
Zend Framework教程之视图组件Zend_View用法详解
Mar 05 PHP
php中的登陆login实例代码
Jun 20 PHP
PHP 5.6.11中CURL模块问题的解决方法
Aug 08 PHP
PHP简单实现冒泡排序的方法
Dec 26 PHP
Ajax+Jpgraph实现的动态折线图功能示例
Feb 11 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
PHP全概率运算函数(优化版) Webgame开发必备
2011/07/04 PHP
php处理单文件、多文件上传代码分享
2016/08/24 PHP
PHP微信模板消息操作示例
2017/06/29 PHP
php readfile()修改文件上传大小设置
2017/08/11 PHP
YII框架常用技巧总结
2019/04/27 PHP
php5.6.x到php7.0.x特性小结
2019/08/17 PHP
在Laravel中实现使用AJAX动态刷新部分页面
2019/10/15 PHP
PHP7新特性
2021/03/09 PHP
JQuery 风格的HTML文本转义
2009/07/01 Javascript
js实现跟随鼠标移动且带关闭功能的图片广告实例
2015/02/26 Javascript
Angular Js文件上传之form-data
2015/08/28 Javascript
KnockoutJS 3.X API 第四章之click绑定
2016/10/10 Javascript
javascript 动态样式添加的简单实现
2016/10/11 Javascript
微信小程序 input输入框详解及简单实例
2017/01/10 Javascript
Javascript中for循环语句的几种写法总结对比
2017/01/23 Javascript
jquery手机触屏滑动拼音字母城市选择器的实例代码
2017/12/11 jQuery
layui select 禁止点击的实现方法
2019/09/05 Javascript
IDEA安装vue插件图文详解
2019/09/26 Javascript
jQuery实现的分页插件完整示例
2020/05/26 jQuery
python写的ARP攻击代码实例
2014/06/04 Python
Python 机器学习库 NumPy入门教程
2018/04/19 Python
在Pycharm terminal中字体大小设置的方法
2019/01/16 Python
pycharm配置当鼠标悬停时快速提示方法参数
2019/07/31 Python
Python创建空列表的字典2种方法详解
2020/02/13 Python
详解用selenium来下载小姐姐图片并保存
2021/01/26 Python
有机婴儿毛毯和衣服:Monica + Andy
2020/03/01 全球购物
某个公司的Java笔面试题
2016/03/11 面试题
体育教育专业自荐信范文
2013/12/20 职场文书
理工大学毕业生自荐信范文
2014/02/22 职场文书
合伙协议书范本
2014/04/21 职场文书
单位绩效考核方案
2014/05/11 职场文书
中层干部竞聘演讲稿
2014/05/15 职场文书
工作失职自我检讨书
2015/05/05 职场文书
2015年物资管理工作总结
2015/05/20 职场文书
千与千寻观后感
2015/06/04 职场文书
Win10鼠标宏怎么设置?win10系统鼠标宏的设置方法
2022/08/14 数码科技