php中http请求类的示例分析
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章将为大家详细讲解有关php中http请求类的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。代码:
千家信息网最后更新 2026年11月03日php中http请求类的示例分析
这篇文章将为大家详细讲解有关php中http请求类的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
代码:
]>* @property* 1、timeout 超时时间,默认5秒* 2、depth 重定向深度,默认3* 3、name 上传文件的名字,默认file* 4、cookie 模拟登录时cookie存储在本地的文件,默认cookie.txt* @method* 1、ssl 是否设置https true:是 false:否* 2、auth 启用验证 user:用户名 pass:密码* 3、login 模拟登录,获取cookie* 4、cookie 使用cookie登录* 5、header 设置请求头 data:请求头数组* 6、proxy 设置服务器代理 url:代理服务器url port:代理服务器端口* 7、agent 设置浏览器代理 browse:代理浏览器 默认:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)* 8、get 模拟get请求 data:传递的数据* 9、post 模拟post请求 data:传递的数据* 10、json 模拟json请求 data:传递的数据* 11、upload 模拟表单上传 files:上传的文件 array|string* 12、download 下载文件 dir:要下载的文件 格式:a/b* 13、run 执行 ret:返回的数据* 14、info 获取执行信息 ret:返回的信息*/class http{public $timeout = 5; # 超时时间,默认5秒public $depth = 3; # 重定向深度,默认3public $name = 'file'; # 上传文件的名字,默认filepublic $cookie = 'cookie.txt'; # 模拟登录时cookie存储在本地的文件,默认cookie_nprivate $scheme = '';private $host = '';private $path = '';private $query = '';private $options = array();private $ch;private $fp;private $progress = false;/* @desc:内部方法 设置get请求参数 @param data 请求数据 */private function setget($data){ $scheme = $this->scheme; $host = $this->host; $path = $this->path; $query = $this->query; $sep = ($query || !empty($data))?"?":""; $qurl = $scheme.'://'.$host.$path.$sep.$query.$data; $this->options[CURLOPT_URL] = $qurl; return $this;}/* @desc:内部方法 设置post请求参数 @param data 请求数据 */private function setpost($data){ $scheme = $this->scheme; $host = $this->host; $path = $this->path; $query = $this->query; $sep = $query?"?":""; $qurl = $scheme.'://'.$host.$path.$sep.$query; $this->options[CURLOPT_URL] = $qurl; $this->options[CURLOPT_POST] = 1; $this->options[CURLOPT_POSTFIELDS] = $data; return $this;}/* @desc:内部方法 设置最终请求参数 */private function setopt(){ $options = $this->options; curl_setopt_array( $this->ch, $options ); return $this;}/* @desc:构造方法 设置初始请求参数 @param url 请求地址 */public function __construct($url){ $info = parse_url($url); $this->scheme = $info['scheme']?:'http'; $this->host = $info['host']; $this->path = $info['path']; $this->query = $info['query']; $this->ch = curl_init(); $this->options[CURLOPT_CONNECTTIMEOUT] = $this->timeout; $this->options[CURLOPT_RETURNTRANSFER] = 1; $this->options[CURLOPT_FOLLOWLOCATION] = 1; $this->options[CURLINFO_HEADER_OUT] = true; $this->options[CURLOPT_ENCODING] = 'gzip'; $this->options[CURLOPT_MAXREDIRS] = $this->depth;}/* @desc:是否设置https请求 @param bool true:https请求 false:http请求 */public function ssl($bool = false){ if($bool){ $this->scheme = 'https'; $this->options[CURLOPT_SSL_VERIFYHOST] = 1; $this->options[CURLOPT_SSL_VERIFYPEER] = false; } return $this;}/* @desc:设置验证用户名、密码 @param user 用户名 @param pass 密码 */public function auth($user,$pass){ $this->options[CURLOPT_USERPWD] = $user.':'.$pass; return $this;}/* @desc:模拟登录 */public function login(){ $cookie = $this->cookie; $this->options[CURLOPT_COOKIEJAR] = $cookie; $this->options[CURLOPT_RETURNTRANSFER] = 0; return $this;}/* @desc:带cookie登录 */public function cookie(){ $cookie = $this->cookie; $this->options[CURLOPT_COOKIEFILE] = $cookie; return $this;}/* @desc:设置请求头信息 @param data 请求头 */public function header($data){ $this->options[CURLOPT_HTTPHEADER] = $this->options[CURLOPT_HTTPHEADER]?:array(); $this->options[CURLOPT_HTTPHEADER] = array_merge($this->options[CURLOPT_HTTPHEADER],$data); return $this;}/* @desc:设置代理服务器 @param url 代理服务器url @param port 代理服务器端口 */public function proxy($url,$port){ $info = parse_url($url); $scheme = $info['scheme']?:'http'; $host = $info['host']; $path = $info['path']; $purl = $scheme.'://'.$host.$path.':'.$port; $this->options[CURLOPT_PROXY] = $purl; return $this;}/* @desc:设置代理浏览器 @param browse 代理浏览器 */public function agent($browse = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)'){ $this->options[CURLOPT_USERAGENT] = $browse; return $this;}/* @desc:模拟get请求 @param data 请求数据 */public function get($data = array()){ $data = http_build_query($data); $this->setget($data); return $this;}/* @desc:模拟post请求 @param data 请求数据 */public function post($data = array()){ $this->setpost($data); return $this;}/* @desc:模拟json请求 @param data 请求数据 */public function json($data = array()){ $data = json_encode($data); $header = array( 'Content-Type: application/json', 'Content-Length:' . strlen($data) ); $this->header($header); $this->setpost($data); return $this;}/* @desc:模拟表单上传 @param files 文件路径 */public function upload($files){ $this->progress = true; $data = array(); $name = $this->name; if(is_array($files)){ foreach($files as $k=>$v){ $data["{$name}[{$k}]"]=new \CURLFile($v); } }else{ $data["{$name}"]=new \CURLFile($files); } ob_start(); echo ">>"; ob_flush(); flush(); $last = 0; $diff = 0; $this->options[CURLOPT_PROGRESSFUNCTION] = function($source,$dfilesize,$downsize,$upsize,$ufilesize){ global $last; global $diff; if($ufilesize > 0){ $percent = round($ufilesize/$upsize*100,2); $diff += $percent - $last; if($diff > 1){ echo "#"; $diff = 0; } $last = $percent; } ob_flush(); flush(); }; $this->options[CURLOPT_NOPROGRESS] = false; $this->setpost($data); return $this;}/* @desc:下载文件 @param dir 存储文件目录 */public function download($dir = ''){ $this->progress = true; $path = $this->path; if($dir && !is_dir($dir)){ mkdir($dir,0755,true); } $name = strrchr($path, '/'); $dsep = $dir?'/':''; ob_start(); echo ">>"; ob_flush(); flush(); $this->fp=fopen('.'.$dsep.$dir.$name, 'w'); $last = 0; $diff = 0; $this->options[CURLOPT_PROGRESSFUNCTION] = function($source,$dfilesize,$downsize,$upsize,$ufilesize){ global $last; global $diff; if($dfilesize > 0){ $percent = round($downsize/$dfilesize*100,2); $diff += $percent - $last; if($diff > 1){ echo "#"; $diff = 0; } $last = $percent; } ob_flush(); flush(); }; $this->options[CURLOPT_NOPROGRESS] = false; $this->options[CURLOPT_FILE] = $this->fp; $this->setget(''); return $this;}/* @desc:执行方法 @return ret 返回的数据 */public function run(){ $ch = $this->ch; $this->setopt(); $ret = curl_exec($ch); curl_close($ch); if($this->progress){ if($this->fp){ fclose($this->fp); } echo "#<测试:
$http = new http('www.baidu.com');$ret = $http->ssl(true)->get()->run();echo $ret;输出:
百度一下,你就知道
关于"php中http请求类的示例分析"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
数据
文件
代理
登录
服务器
服务
方法
浏览器
浏览
参数
更多
信息
密码
用户
用户名
篇文章
存储
示例
分析
主页
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网黑科技
华服互联网科技靠谱吗
西安纸贵互联网科技有限公司
中关村互联网 科技集团
河南真二互联网科技有限公司
科技互联网技术
上海互联网科技有限公司
远信互联网科技
唐信互联网科技 大连
兼蜜互联网科技
四川狮子座互联网科技
互联网金融监管科技
科技大学互联网专业好不好
互联网科技公司的市场部门
互联网科技有哪些公司
民营互联网公司国防科技
企晟互联网信息科技有限公司
伊宁互联网科技做什么的
互联网金融科技公司的优势
利用互联网等科技手段
科技与互联网发展
互联网科技集团
西安互联网科技
互联网科技上市公司
蓝色互联网科技
互联网科技公司招聘
互联网科技公司实习生
中国互联网有什么科技
有人说阿里系是互联网科技企业
互联网科技职业技能培训学校
