php实现httpclient类示例

2015-01-24信息快讯网

这篇文章主要介绍了php实现httpclient类示例,需要的朋友可以参考下

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('http://www.jb51.net/ServiceLogin/', 'hash='. $time, 'uid=1;users=admin;')

echo $httpClient->buffer;

Yii中render和renderPartial的区别
使用YUI+Ant 实现JS CSS压缩
在Ubuntu 14.04上部署 PHP 环境及 WordPress
浅析PHP中strlen和mb_strlen的区别
PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法
PHP中echo和print的区别
简单分析ucenter 会员同步登录通信原理
PHP函数http_build_query使用详解
zend framework框架中url大小写问题解决方法
PHP实现取得HTTP请求的原文
Zend Framework 2.0事件管理器(The EventManager)入门教程
PHP中模拟处理HTTP PUT请求的例子
ThinkPHP模板判断输出Present标签用法详解
编译PHP报错configure error Cannot find libmysqlclient under usr的解决方法
PHP使用DOMDocument类生成HTML实例(包含常见标签元素)
php下获取http状态的实现代码
php命令行使用方法和命令行参数说明
php使用json_encode对变量json编码
php检测useragent版本示例
xss防御之php利用httponly防xss攻击
PHP fopen()和 file_get_contents()应用与差异介绍
zf框架的db类select查询器join链表使用示例(zend框架)
zf框架的zend_cache缓存使用方法(zend框架)
php中调用其他系统http接口的方法说明
php中sprintf与printf函数用法区别解析
实现获取http内容的php函数分享
php的sprintf函数的用法 控制浮点数格式
分享下页面关键字抓取components.arrow.com站点代码
php使用curl访问https示例分享
解决file_get_contents无法请求https连接的方法
PHP PDOStatement:bindParam插入数据错误问题分析
©2014-2024 dbsqp.com