<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## Socket 操作 ![網絡七層協議示意圖](https://box.kancloud.cn/e2e6beee0280c8aac08794e476f58c52_542x476.jpg) ### TCP/IP 是什么? * TCP/IP 是個協議組,分為三個層次:網絡層、傳輸層和應用層。 * 在網絡層有 IP 、ICMP 和 IGMP 協議。 * 在傳輸層中有 TCP 和 UDP 協議。 * 在應用層有FTP、HTTP、TELNET、SMTP、DNS 等協議。 ### Socket 是什么? Socket 是應用層與 TCP/IP 協議族通信的中間軟件抽象層,一組接口,把復雜的 TCP/IP 協議族隱藏在 Socket 接口后面。 ### Socket 原理 Socket 連接至少需要一對套接字,分為 clientSocket 和 serverSocket。 Socket連接分為3個步驟: * 服務器監聽:服務器并不定位具體客戶端的套接字,而是時刻處于監聽狀態。 * 客戶端請求:客戶端的套接字要描述它要連接的服務器的套接字,提供地址和端口號,然后向服務器套接字提出連接請求。 * 連接確認:當服務器套接字收到客戶端套接字發來的請求后,就響應客戶端套接字的請求,把服務器端的套接字的描述發給客戶端。一旦客戶端確認了此描述,就正式建立連接。而服務器套接字繼續處于監聽狀態,繼續接收其他客戶端套接字的連接請求。 長鏈接和短鏈接: * 短鏈接: 短連接是指 SOCKET 連接后,發送完數據后,馬上斷開連接。 * 長鏈接: 長連接是指 SOCKET 連接后,不管是否使用,繼續保持鏈接。 參考鏈接:[TCP/IP 和 Socket](http://blog.chinaunix.net/uid-9622484-id-3392992.html) ### Socket 發送 HTTP 請求 ``` <?php class HttpClient { const SEP = "\r\n"; // 分隔符 const VERSION = 'HTTP/1.1'; // 協議的版本 private $line = ''; // 請求行 private $body = ''; // 請求正文 private $headers = []; // 請求頭 private $uInfo = []; // url信息 public function __construct($url = null) { if ($url) { $this->setUrl($url); } } /** * 設置并解析URL * @param string $url */ public function setUrl($url) { $uInfo = parse_url($url); if (!isset($uInfo['port'])) { $uInfo['port'] = 80; } if (!isset($uInfo['path'])) { $uInfo['path'] = '/'; } $this->uInfo = $uInfo; } /** * 添加請求頭信息 * @param string $header */ public function setHeader($header) { $this->headers[] = $header; } /** * GET 請求 * @return array */ public function get() { $this->setLine('GET'); return $this->request(); } /** * POST 請求 * @param array * @return array */ public function post($data = []) { $this->setLine('POST'); $this->setBody($data); $this->setHeader('Content-Type: application/x-www-form-urlencoded'); $this->setHeader('Content-Length: ' . strlen($this->body)); return $this->request(); } /** * 構造請求行信息 * @param string $line GET|POST */ private function setLine($line) { $this->line .= ($line . ' ' . $this->uInfo['path']); if (isset($this->uInfo['query'])) { $this->line .= ('?' . $this->uInfo['query']); } $this->line .= (' ' . self::VERSION); } /** * 構造消息正文 * @param array $data */ private function setBody($data) { if ($data) { $this->body = http_build_query($data); } } /** * 發送數據 * @return array */ private function request() { $this->setHeader('Host: ' . $this->uInfo['host']); $this->setHeader('Connection: close'); $this->setHeader('User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0'); $handle = fsockopen($this->uInfo['host'], $this->uInfo['port']); $str = $this->line . self::SEP; $str .= implode(self::SEP, $this->headers); $str .= self::SEP . self::SEP; if (!empty($this->body)) { $str .= $this->body; } fwrite($handle, $str); $response = ''; while (!feof($handle)) { $response .= fgets($handle); } fclose($handle); $this->response = $response; list($headers, $content) = explode(self::SEP . self::SEP, $response); return [ 'headers' => $headers, 'content' => $content, ]; } } $http = new HttpClient('http://forum-api.local/test'); /* ------------- 發送 GET 請求 ------------ */ $response = $http->get(); var_dump($response['headers']); var_dump($response['content']); exit(); /* ------------- 發送 POST 請求 ------------ */ $data = [ 'name' => 'kate green', 'age' => 12, ]; $response = $http->post($data); var_dump($response['headers']); var_dump($response['content']); ?> ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看