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;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



What should I do if Google Chrome prompts that the content of this tab is being shared? When we use Google Chrome to open a new tab, we sometimes encounter a prompt that the content of this tab is being shared. So what is going on? Let this site provide users with a detailed introduction to the problem of Google Chrome prompting that the content of this tab is being shared. Google Chrome prompts that the content of this tab is being shared. Solution: 1. Open Google Chrome. You can see three dots in the upper right corner of the browser "Customize and control Google Chrome". Click the icon with the mouse to change the icon. 2. After clicking, the menu window of Google Chrome will pop up below, and the mouse will move to "More Tools"

Blizzard Battle.net update keeps stuck at 45%, how to solve it? Recently, many people have been stuck at the 45% progress bar when updating software. They will still get stuck after restarting multiple times. So how to solve this situation? We can reinstall the client, switch regions, and delete files. To deal with it, this software tutorial will share the operation steps, hoping to help more people. Blizzard Battle.net update keeps stuck at 45%, how to solve it? 1. Client 1. First, you need to confirm that your client is the official version downloaded from the official website. 2. If not, users can enter the Asian server website to download. 3. After entering, click Download in the upper right corner. Note: Be sure not to select Simplified Chinese when installing.

Epic Seven has been confirmed to be updated non-stop at 11 noon on February 22. This update will bring us a lot of new activities and content, including an increase in the limited summoning rate of Leia and Sweet Miracle, an update to the mysterious card pool, The second week of the special side story Miracle Maid Kingdom has begun. Let’s take a look at this update. Mobile game update schedule: The Seventh Epic will be updated on February 22nd: The Miracle Maid Kingdom will open for the second week ※The chance of limited summoning of "Leia" & "Sweet Miracle" is up! ■Limited Summoning Chance Up Time: -2024/02/22 (Thursday) 11:00 ~ 2024/03/07 (Thursday) 10:59 ■Character Attributes & Occupations: Natural Attributes, Warrior ■Character Introduction: Four-person Band The sub-vocalist of "Miracle Maid Kingdom" and Bei

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

Lantern and Dungeons has been confirmed to be updated on February 29th. After the update, the remastered version of Lantern and Dungeons will be launched, and the remastered version will also be linked to the Legend of Nezha. The remastered version will also bring a new profession, and players can directly Job changes, dungeon content will also be expanded, new dungeon areas will be opened, etc. Mobile game update schedule Lantern and Dungeon updated on February 29th: Remastered version ╳ "Legend of Nezha" linkage version key content New profession, why are you invited to change jobs? Lamplighters can actually change jobs? Such cool equipment is really It makes people greedy. I heard that after changing jobs, the lantern holder can also learn many cool skills. Goro exclaimed: Thai pants are hot! The Legend of Nezha is coming together! Stepping on the hot wheel, holding the circle of heaven and earth in hand ♫ ~ The little heroes with both wisdom and courage: Nezha and Little Dragon Girl are about to come

A friend's computer has such a fault. When opening "This PC" and the C drive file, it will prompt "Explorer.EXE Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access the project." Including folders, files, This computer, Recycle Bin, etc., double-clicking will pop up such a window, and right-clicking to open it is normal. This is caused by a system update. If you also encounter this situation, the editor below will teach you how to solve it. 1. Open the registry editor Win+R and enter regedit, or right-click the start menu to run and enter regedit; 2. Locate the registry "Computer\HKEY_CLASSES_ROOT\PackagedCom\ClassInd"

MSI graphics cards are the mainstream graphics card brand on the market. We know that graphics cards need to install drivers to achieve performance and ensure compatibility. So how to update the MSI graphics card driver to the latest version? Generally, MSI graphics card drivers can be downloaded and installed from the official website. Let’s find out more below. Graphics card driver update method: 1. First, we enter the "MSI official website". 2. After entering, click the "Search" button in the upper right corner and enter your graphics card model. 3. Then find the corresponding graphics card and click on the details page. 4. Then enter the "Technical Support" option above. 5.Finally go to “Driver & Download”

Windows updates may cause some of the following problems: 1. Compatibility issues: Some applications, drivers, or hardware devices may be incompatible with new Windows updates, causing them to not work properly or crash. 2. Performance issues: Sometimes, Windows updates may cause the system to become slower or experience performance degradation. This may be due to new features or improvements requiring more resources to run. 3. System stability issues: Some users reported that after installing Windows updates, the system may experience unexpected crashes or blue screen errors. 4. Data loss: In rare cases, Windows updates may cause data loss or file corruption. This is why before making any important updates, back up your
