发布一个http请求封装类
调试时经常要模拟提交,在抓取别人页面时也经常要去请求别人的页面,于是就写了下面这个类。封装了三种post提交方法和一个request请求方法,
- <?php
- /**
- * HTTP常用请求封装
- *
- * Copyright(c) 2012 by ustb80. All rights reserved
- *
- * To contact the author write to {@link mailto:ustb80@163.com}
- *
- * @author ustb80
- * @version $Id: HttpHelper.php,v 1.0 2012-8-9
- * @package library
- */
- // ------------------------------------------------------------------------
- /**
- * http请求处理
- *
- * 开发中经常需要模拟提交请求,本类封装了常用的post方法
- *
- * @author ustb80
- *
- */
- class HttpHelper
- {
- // 当前的user-agent字符串
- public $ua_string= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1";
- // 支持的提交方式
- public $post_type_list = array("curl", "socket", "stream");
- // 本地cookie文件
- private $cookie_file;
- // --------------------------------------------------------------------
- /**
- * 构造函数
- *
- * @param array $params 初始化参数
- */
- public function __construct($params = array())
- {
- if(count($params) > 0)
- {
- $this->init($params);
- }
- }
- // --------------------------------------------------------------------
- /**
- * 参数初始化
- *
- * @param array $params
- */
- public function init($params)
- {
- if(count($params) > 0)
- {
- foreach($params as $key => $val)
- {
- if(isset($this->$key))
- {
- $this->$key = $val;
- }
- }
- }
- }
- // --------------------------------------------------------------------
- /**
- * 提交请求
- *
- * @param string $url 请求地址
- * @param mixed $data 提交的数据
- * @param string $type 提交类型,curl,socket,stream可选
- */
- public function post($url, $data, $type = "socket")
- {
- if(!in_array($type, $this->post_type_list))
- {
- die("undefined post type");
- }
- $function_name = $type . "Post";
- return call_user_func_array(array($this, $function_name), array($url, $data));
- }
- // --------------------------------------------------------------------
- /**
- * 更改默认的ua信息
- *
- * 本方法常用于模拟各种浏览器
- *
- * @param string $ua_string UA字符串
- */
- public function setUA($user_agent)
- {
- $this->ua_string = $user_agent;
- return $this;
- }
- // --------------------------------------------------------------------
- /**
- * 设置本地cookie文件
- *
- * 在用curl来模拟时常需要设置此项
- *
- * @param string $cookie_file 文件路径
- */
- public function setCookieFile($cookie_file)
- {
- $this->cookie_file = $cookie_file;
- return $this;
- }
- // --------------------------------------------------------------------
- /**
- * curl方式提交
- *
- * @param string $url 请求地址
- * @param mixed $data 提交的数据
- * @param string $user_agent 自定义的UA
- * @return mixed
- */
- public function curlPost($url, $data, $user_agent = '')
- {
- if($user_agent == '')
- {
- $user_agent = $this->ua_string;
- }
- if (!is_array($data))
- {
- $data = array($data);
- }
- $data = http_build_query($data);
- if (!function_exists("curl_init"))
- {
- die('undefined function curl_init');
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
- $rs = curl_exec($ch);
- curl_close($ch);
- return $rs;
- }
- // --------------------------------------------------------------------
- /**
- * 套接字提交
- *
- * @param string $url 请求地址
- * @param mixed $data 提交的数据
- * @param string $user_agent 自定义的UA
- * @param int $port 端口
- * @param int $timeout 超时限制
- * @return mixed
- */
- public function socketPost($url, $data, $user_agent = '', $port = 80, $timeout = 30)
- {
- $url_info = parse_url($url);
- $remote_server = $url_info['host'];
- $remote_path = $url_info['path'];
- $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
- if(!$socket)
- {
- die("$errstr($errno)");
- }
- if($user_agent == '')
- {
- $user_agent = $this->ua_string;
- }
- if (!is_array($data))
- {
- $data = array($data);
- }
- $data = http_build_query($data);
- fwrite($socket, "POST {$remote_path} HTTP/1.0\r\n");
- fwrite($socket, "User-Agent: {$user_agent}\r\n");
- fwrite($socket, "HOST: {$remote_server}\r\n");
- fwrite($socket, "Content-type: application/x-www-form-urlencoded\r\n");
- fwrite($socket, "Content-length: " . strlen($data) . "\r\n");
- fwrite($socket, "Accept:*/*\r\n");
- fwrite($socket, "\r\n");
- fwrite($socket, "{$data}\r\n");
- fwrite($socket, "\r\n");
- $header = "";
- while($str = trim(fgets($socket, 4096)))
- {
- $header .= $str;
- }
- $data = "";
- while(!feof($socket))
- {
- $data .= fgets($socket, 4096);
- }
- return $data;
- }
- // --------------------------------------------------------------------
- /**
- * 文件流提交
- *
- * @param string $url 提交地址
- * @param string $data 数据
- * @param string $user_agent 自定义的UA
- * @return mixed
- */
- public function streamPost($url, $data, $user_agent = '')
- {
- if($user_agent == '')
- {
- $user_agent = $this->ua_string;
- }
- if (!is_array($data))
- {
- $data = array($data);
- }
- $data = http_build_query($data);
- $context = array(
- 'http' => array(
- 'method' => 'POST',
- 'header' => 'Content-type: application/x-www-form-urlencoded' . "\r\n" . 'User-Agent : ' . $user_agent . "\r\n" . 'Content-length: ' . strlen($data),
- 'content' => $data
- )
- );
- $stream_context = stream_context_create($context);
- $data = file_get_contents($url, FALSE, $stream_context);
- return $data;
- }
- // --------------------------------------------------------------------
- /**
- * 发送请求
- *
- * 本方法通过curl函数向目标服务器发送请求
- *
- * @param string $url 请求地址
- * @return mixed
- */
- public function request($url)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_USERAGENT, !empty($this->ua_string)? $this->ua_string : $_SERVER['HTTP_USER_AGENT']);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- if (isset($this->cookie_file))
- {
- curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file);
- }
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- }
例子:
- <?php
- require_once 'HttpHelper.php';
- $HttpHelper = new HttpHelper();
- $url = "http://localhost/post.php";
- $data = array("name"=>"socket");
- $rs[] = $HttpHelper->post($url, $data);
- $data = array("name"=>"curl");
- $rs[] = $HttpHelper->post($url, $data, "curl");
- $data = array("name"=>"stream");
- $rs[] = $HttpHelper->post($url, $data, "stream");
- $rs[] = $HttpHelper->request($url);
- print_r($rs);
post.php代码如下:
- <?php
- echo 'test request:';
- print_r($_REQUEST);
输出结果:
- Array
- (
- [0] => test request:Array
- (
- [name] => socket
- )
- [1] => test request:Array
- (
- [name] => curl
- )
- [2] => test request:Array
- (
- [name] => stream
- )
- [3] => test request:Array
- (
- )
- )
转载于:https://blog.51cto.com/ustb80/1043797
发布一个http请求封装类相关推荐
- php http 类,php写的http请求封装类
本文介绍一个php实现的http请求封装类,封装了三种post提交方法和一个request请求方法,并提供了调用示例,供大家学习参考. 在php编程调试时,经常需要模拟提交. 另外,在抓取一些页面时, ...
- 【区块链】发布一个纯Python实现的EOSIO WAX SDK
回顾 <[区块链]Python开发WAX链游脚本常用工具> 在上一篇文章中,我们介绍了Python与EOS或WAX网络交互的两个第三方开源库[eospy]和[pyeoskit]. 不过经过 ...
- 【WAX链游】发布一个免费开源的Alien Worlds【外星世界】合约脚本TLM
前言 <链游Farmers World[农民世界]爆火,发布一个免费开源的脚本> 在之前的文章中,我们分享了一个开源的农民世界(Farmers World)脚本 [OpenFarmer]: ...
- 使用 Go 从零开发并发布一个 Kubectl 插件
作者:KaliArch(薛磊),某 Cloud MSP 服务商产品负责人,熟悉企业级高可用/高并发架构,包括混合云架构.异地灾,熟练企业 DevOPS 改造优化,熟悉 Shell/Python/Go ...
- 如何在Mendix中发布一个REST Service
目录 介绍(Introduction) REST是什么? Published REST Service 一般属性 Service Name(服务名称) Version (版本) Location 位置 ...
- 一文读懂一个URL请求的过程是怎样的
前言 当我们在浏览器中输入一个URL访问地址,然后浏览器返回给我们一个响应页面,这内部过程到底是怎样的呢?下面我将从以下几个方面阐述一个 WEB请求过程到底是怎样: 浏览器缓存 DNS域名解析 TCP ...
- C#实现http的GET/POST请求封装类-HttpRequest
C#实现http的GET/POST请求封装类-HttpRequest 背景 代码实现 背景 在开发微信应用时,会经常在C#服务端以Http的GET或POST方式请求钉钉平台或微信平台的接口,为了方便发 ...
- 给小孩发布一个有趣的网站 在线动物园
给小孩发布一个有趣的网站 在线动物园,可以实时看到动物园里的动物实时摄像头. 非常有意思,大热天也不用到动物园里看了. http://zoo.baidu.com/video.html
- 一个HTTP请求的曲折经历
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来源:8rr.co/6usv 从一个经典的面试题说起,输入URL到 ...
最新文章
- android点滴(25)之 original-package
- java基础 知识点
- 英语与计算机的整合,浅谈计算机应用与英语教学的整合
- 【学习笔记】抽象队列同步器AQS应用之BlockingQueue详解
- 详解分布式协调服务 ZooKeeper
- 计算机控制学什么,计算机控制技术专业介绍
- php正文重复度,百度如何判断网页文章的重复度?两个页面相似度确认方法介绍...
- OpenBSD基金会收到锤子科技约140万捐赠款
- [Unity] ACT 战斗系统学习 2:从 Buff 系统开始的思考
- Windows2008R2 --Active Directory回收站
- Kali渗透测试工具库(二)beef--Web浏览器攻击框架
- QByteArray与char、int、float(及其数组)之间的互相转化
- 电路原理解析_接近开关检测旋转设备的工作原理
- mysql语句优化 explai_mysql使用explain优化sql语句
- 在cs页面获取input的值方法
- 机器视觉算法与应用总结
- C语言:使用函数统计指定数字的个数
- www 53ff com劫持IE,广告网页图标常驻桌面,删了又来
- docker 安装linux镜像制作,制作ubuntu完整版docker镜像
- js将阿拉伯数字转换成中文的大写数字