HttpClient 增强版
更新提示: 1.增加301、302HTTP重定向 2.增加自定义发送头部 3.修复超时时间 4.增加HTTP_STATUS_CODE属性 5.支持HTTPS 来源地址:http://www.haowei.me/archives/1154.html 无 ?php class HttpClient { private static $context = null; // (resource) Socket
更新提示:
1. 增加 301、302 HTTP 重定向
2. 增加 自定义 发送头部
3. 修复 超时时间
4. 增加 HTTP_STATUS_CODE 属性
5. 支持 HTTPS
来源地址: http://www.haowei.me/archives/1154.html
<?php class HttpClient { private static $context = null; // (resource) Socket stream private $header = null; // (array) Request headers private $headerList = null; // (array) User custom request headers public $buffer = null; // (string) Call buffer public $response = null; // (array) Remote response headers public $request = null; // (string) Request headers private $args = null; // (array) User configure private $attachRedirect = null; // (bool) Tigger HTTP redirect public function __construct($args = null) { if(!is_array($args)) $args = array(); $this->args = $args; $charset = isset($this->args['charset']) ? $this->args['charset'] : 'UTF-8'; if(!empty($this->args['debugging'])) { set_time_limit(0); header('Content-Type: text/plain;charset='. $charset); }else{ header('Content-Type: text/html;charset='. $charset); } if(!isset($this->args['timeout'])) $this->args['timeout'] = 5; $this->args['timeout'] = intval($this->args['timeout']); if(!empty($this->args['redirect'])) $this->attachRedirect = true; $this->headerList = array(); } public static function init(& $instanceof, $args = null) { static $instance; if(!$instance) $instanceof = new self($args); return $instance = $instanceof; } public function setHeader($name, $value) { $this->headerList[$name] = $value; } private function build($args) { list($method, $url, $data, $cookie) = $args; $this->buffer = ''; $this->request = ''; $this->header = array(); $this->response = array(); $userAgent = isset($this->args['userAgent']) ? $this->args['userAgent'] : ( isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : __CLASS__ ); extract($parse = parse_url($url)); $path = isset($query) ? $path .'?'. $query : ( isset($path) ? $path : '/' ); $port = isset($port) ? $port : ( $scheme == 'https' ? 443 : 80 ); $protocol = $scheme == 'https' ? 'ssl://' : 'tcp://'; self::$context = fsockopen($protocol . $host, $port, $errno, $errstr, $this->args['timeout']); if($errno) trigger_error(iconv('GBK//IGNORE', 'UTF-8', $errstr), E_USER_ERROR); stream_set_blocking(self::$context, 1); stream_set_timeout(self::$context, $this->args['timeout']); $query = $data; if($data && is_array($data)) { $query = array(); foreach($data as $k => $value) array_push($query, $k .'='. $value); $query = implode('&', $query); } array_push($this->header, $method .' '. $path .' HTTP/1.1'); array_push($this->header, 'Host: '. $host); array_push($this->header, 'Accept: */*'); array_push($this->header, 'Content-type: application/x-www-form-urlencoded'); array_push($this->header, 'Connection: close'); array_push($this->header, 'User-Agent: '. $userAgent); if($this->headerList) foreach($this->headerList as $name => $value) array_push($this->header, $name .': '. $value); if($cookie) array_push($this->header, 'Cookie: '. $cookie ); if($data) array_push($this->header, 'Content-Length: '. strlen($query)); if($data) array_push($this->header, ''); if($data) array_push($this->header, $query); array_push($this->header, "\r\n"); $this->request = implode("\r\n", $this->header); fputs(self::$context, $this->request); $skipped = false; $this->HTTP_STATUS_CODE = 0; $this->HTTP_TRANSFER_CHUNKED = false; while(!feof(self::$context)) { if(($line = fgets(self::$context))) { if(preg_match('/HTTP\/\d\.\d\s*(\d+)/i', $line, $match)) $this->HTTP_STATUS_CODE = (int) array_pop($match); if(preg_match('/Location:\s*(.+)\s*?/i', $line, $match)) ( ($this->HTTP_REDIRECT_URL = trim(array_pop($match))) && $skipped = !$skipped ); if(preg_match('/Transfer\-Encoding:\s*chunked/i', $line, $match)) $this->HTTP_TRANSFER_CHUNKED = true; if(array_push($this->response, $line) && in_array($line, array("\n", "\r\n"))) break; } } if($this->attachRedirect && $skipped) { fclose(self::$context); $data ? call_user_func_array(array($this, $method), array($this->HTTP_REDIRECT_URL, $data, $cookie)): call_user_func_array(array($this, $method), array($this->HTTP_REDIRECT_URL, $cookie)); } if(!$skipped) { if($this->HTTP_STATUS_CODE === 200) { $this->buffer = ''; $chunksize = 0; $chunked = ''; while(!feof(self::$context)) { $line = fgets(self::$context); if($this->HTTP_TRANSFER_CHUNKED) { if(!$chunksize) { $chunksize = (int) hexdec(trim(ltrim($line, '0'))) + 2; }else{ if(strlen($chunked) < $chunksize){ $chunked .= $line; }else{ $this->buffer .= substr($chunked, 0, $chunksize - 2); $chunksize = (int) hexdec(trim(ltrim($line, '0'))) + 2; $chunked = ''; } } }else{ $this->buffer .= $line; } } } } return (string) $this->buffer; } public function get($url, $cookie = null) { return $this->build(array('GET', $url, null, $cookie)); } public function post($url, $data = null, $cookie = null) { return $this->build(array('POST', $url, $data, $cookie)); } public function __set($attr, $value) { $this->$attr = $value; } public function __destruct() { if(is_resource(self::$context)) fclose(self::$context); unset($this->headerList, $this->header, $this->response, $this->request, $this->args, $this->buffer); } } HttpClient::init($HttpClient, array('userAgent' => $_SERVER['HTTP_USER_AGENT'], 'redirect' => true)); $HttpClient->get('http://www.haowei.me'); echo $HttpClient->buffer;

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

谷歌浏览器提示此标签页的内容正在被共享怎么办?我们在使用谷歌浏览器打开新标签的时候有时候会遇到提示此标签页的内容正在被共享,那么这是怎么回事?下面就让本站来为用户们来仔细的介绍一下谷歌浏览器提示此标签页的内容正在被共享的问题解析吧。 谷歌浏览器提示此标签页的内容正在被共享解决方法 1、打开谷歌浏览器,在浏览器右上角可以看到三个点“自定义和控制Googlechrome”用鼠标点击图标进行图标。 2、点击后,谷歌浏览器的菜单窗口将弹出到下面,鼠标将移动到“更多工具

暴雪战网更新一直卡在45%怎么解决?近期有很多人在更新软件的时候,都是卡在45%的进度条,重启多次还是会卡住,那么这种情况应该要如何解决,我们可以通过重新安装客户端、切换地区、删除文件的方式来处理,本期软件教程就来分享操作步骤,希望能够给更多的人带来帮助。 暴雪战网更新一直卡在45%怎么解决 一、客户端 1、首先需要确认你的客户是官网下载的官方版本。 2、如果不是的话,用户可以进入亚服网址来进行下载。 3、进入以后点击右上角的下载就可以了。 注意:安装的时候一定不要选择简体中文。

第七史诗已经确定将于2月22日中午11点进行不停机更新,本次更新为我们带来超级多新的活动和内容,包括莱娅和甜蜜奇迹限定召唤几率UP、神秘卡池更新、特别支线故事奇迹女仆王国第二周开启等,一起来看看本次的更新吧。手游更新时间表第七史诗2月22日更新:奇迹女仆王国第二周开启※「莱娅」&「甜蜜奇迹」限定召唤几率UP!■限定召唤几率UP时间:-2024/02/22(周四)11:00~2024/03/07(周四)10:59■角色属性&职业:自然属性、战士■角色简介:四人乐队【奇迹女仆王国】的副主唱兼贝

Angular.js是一种可自由访问的JavaScript平台,用于创建动态应用程序。它允许您通过扩展HTML的语法作为模板语言,以快速、清晰地表示应用程序的各个方面。Angular.js提供了一系列工具,可帮助您编写、更新和测试代码。此外,它还提供了许多功能,如路由和表单管理。本指南将讨论在Ubuntu24上安装Angular的方法。首先,您需要安装Node.js。Node.js是一个基于ChromeV8引擎的JavaScript运行环境,可让您在服务器端运行JavaScript代码。要在Ub

提灯与地下城已经确定将于2月29日更新,更新之后会上线提灯与地下城重制版本,而且重制版本还会与哪吒传奇联动,重制版本还带来全新职业,玩家可以直接转职哦,地下城内容也将拓展,开放全新副本区域等。手游更新时间表提灯与地下城2月29日更新:重制版╳「哪吒传奇」联动版本重点内容全新职业,邀您转职什么?提灯者竟然可以转职了?如此炫酷的装备真是让人眼馋,听说,转职之后,提灯者还能学习很多超帅的技能,五郎直接惊呼:泰裤辣!哪吒传奇,联动来袭!踩着那风火轮,乾坤圈手中拿♫~智勇双全的小英雄:哪吒和小龙女,即将来

小伙伴电脑出现这样的故障,打开“此电脑”和C盘文件会提示“Explorer.EXEWindows无法访问指定设备、路径或文件。你可能没有适当的权限访问访问该项目。”包括文件夹、文件、此电脑、回收站等,双击都会弹出这样的窗口,右键打开又是正常的。这是系统更新导致,如果你也遇到这样的情况,下面小编教大家如何解决。一,打开注册表编辑器Win+R,输入regedit,或右键开始菜单运行输入regedit;二,定位注册表“计算机\HKEY_CLASSES_ROOT\PackagedCom\ClassInd

Windows更新可能导致以下一些问题:1.兼容性问题:某些应用程序、驱动程序或硬件设备可能与新的Windows更新不兼容,导致它们无法正常工作或崩溃。2.性能问题:有时,Windows更新可能会导致系统变得更慢或出现性能下降的情况。这可能是由于新的功能或改进需要更多资源来运行。3.系统稳定性问题:某些用户报告称,在安装Windows更新后,系统可能会出现意外的崩溃或蓝屏错误。4.数据丢失:在罕见的情况下,Windows更新可能会导致数据丢失或文件损坏。这是为什么在进行任何重要的更新之前,备份您

微星显卡是市面上主流的显卡品牌,我们知道显卡都需要安装驱动才能发挥性能,并保证兼容性。那么微星显卡驱动要怎么更新到最新版本呢?一般微星显卡驱动可以官网下载驱动安装,下面就来了解一下吧。 显卡驱动更新方法: 1.首先我们进入“微星官网”。 2.进入后点击右上角“搜索”按钮并输入自己的显卡型号。 3.然后找到对应的显卡点开详情页。 4.随后进入上方“技术支持”选项。 5.最后在“驱动&下载”
