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)

Google瀏覽器提示此標籤頁的內容正在被分享怎麼辦?我們在使用Google瀏覽器開啟新標籤的時候有時會遇到提示此標籤頁的內容正在被分享,那麼這是怎麼回事?以下就讓本站來為使用者來仔細的介紹一下谷歌瀏覽器提示此標籤頁的內容正在被共享的問題解析吧。 Google瀏覽器提示此標籤頁的內容正在被共享解決方法 1、打開谷歌瀏覽器,在瀏覽器右上角可以看到三個點「自訂和控制Googlechrome」用滑鼠點擊圖示進行圖示。 2、點擊後,Google瀏覽器的選單視窗將彈出到下面,滑鼠將移動到「更多工具

暴雪戰網更新一直卡在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

微星顯示卡是市面上主流的顯示卡品牌,我們知道顯示卡都需要安裝驅動才能發揮效能,並保證相容性。那麼微星顯示卡驅動要怎麼更新到最新版本呢?一般微星顯卡驅動可以官網下載驅動安裝,以下就來了解一下。 顯卡驅動更新方法: 1.首先我們進入「微星官網」。 2.進入後點選右上角「搜尋」按鈕並輸入自己的顯示卡型號。 3.然後找到對應的顯示卡點開詳情頁。 4.隨後進入上方「技術支援」選項。 5.最後在“驅動器&下載”

Windows更新可能導致以下一些問題:1.相容性問題:某些應用程式、驅動程式或硬體裝置可能與新的Windows更新不相容,導致它們無法正常運作或崩潰。 2.效能問題:有時,Windows更新可能會導致系統變得更慢或出現效能下降的情況。這可能是由於新的功能或改進需要更多資源來運作。 3.系統穩定性問題:某些用戶報告稱,在安裝Windows更新後,系統可能會出現意外的崩潰或藍屏錯誤。 4.資料遺失:在罕見的情況下,Windows更新可能會導致資料遺失或檔案損壞。這是為什麼在進行任何重要的更新之前,請備份您
