<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [stream擴展php官方文檔](https://www.php.net/manual/zh/book.stream.php) 流(Streams)它用于統一文件、網絡、數據壓縮等類文件操作方式,并為這些類文件操作提供一組通用的函數接口。在最簡單的定義中,一個stream就是一個具有流式行為的資源對象。也就是說,它可以線性方式對stream進行讀取或寫入,并且可以用[fseek()](https://www.php.net/manual/en/function.fseek.php)跳轉到stream內的任意位置 流有點類似數據庫抽象層,在數據庫抽象層方面,不管使用何種數據庫,在抽象層之上都使用相同的方式操作數據, 而流是對數據的抽象,它不管是本地文件還是遠程文件還是壓縮文件等等,只要來的是流式數據(是一組順序、大量、快速、連續到達的數據序列,像流水一樣來一點數據,處理一點,流式數據被封裝成了byte流(其實也是二進制的)如果是全部收到數據以后再處理,那么延遲會很大,而且在很多場合會消耗大量內存),那么操作方式就是一樣的 有了流這個概念就引申出了包裝器wrapper這個概念 每一種流都實現了一個包裝器(wrapper)類【即Stream 可以通過 **\<scheme\>:\/\/\<target\>** 方式來引用。其中\<scheme\>是包裝類的名字,\<target\>中的內容是由包裝類的語法指定,不同的包裝類的語法會有所不同。】,包裝器類包含一些額外的代碼用來處理**特殊的協議**和**編碼**。PHP提供了一些內置的包裝器類,我們也可以很輕松的創建和注冊自定義的包裝器類。我們甚至容可以使用上下文(contexts)和過濾器來改變和增強包裝器 PHP默認的包裝類是file://,也就是說我們在訪問文件系統的時候,其實就是在使用一個stream。我們可以通過下面兩種方式來讀取文件中的內容,readfile(‘/path/to/somefile.txt')或者readfile(‘file:///path/to/somefile.txt'),這兩種方式是等效的。如果你是使用readfile(‘http://google.com/'),那么PHP會選取HTTP stream包裝類來進行操作。 >注意區分PHP流包裝器和基于流的套接字函數的套接字傳輸器(套接字傳輸協議名稱) **stream_get_transports返回一個套接字傳輸協議名稱的索引數組** ``` print_r(stream_get_transports()); Array ( [0] => tcp [1] => udp [2] => unix [3] => udg [4] => ssl [5] => sslv3 [6] => sslv2 [7] => tls ) ``` **來看看PHP 默認有哪些內置的包裝類:** ``` print_r(stream_get_wrappers()); /* Array( [0] => php [1] => file [2] => glob [3] => data [4] => http [5] => ftp [6] => zip [7] => compress.zlib [8] => https [9] => ftps [10] => phar ) */ ``` 可以使用stream_register_wrapper()注冊自己的包裝器 流的使用方式詳情看[php網絡相關章節](http://www.hmoore.net/a173512/php_note/1708928) ``` /* 從 /home/bar 讀取本地文件*/ $localfile = file_get_contents ( "/home/bar/foo.txt" ); /*與上述相同,php默認就是file:// */ $localfile = file_get_contents ( "file:///home/bar/foo.txt" ); /* 使用 HTTP從www.example.com讀取遠程文件 */ $httpfile = file_get_contents ( "http://www.example.com/foo.txt" ); /*使用 HTTPS從www.example.com讀取遠程文件 */ $httpsfile = file_get_contents ( "https://www.example.com/foo.txt" ); /* 使用 FTP從www.example.com讀取遠程文件 */ $ftpfile = file_get_contents ( "ftp://user:pass@ftp.example.com/foo.txt" ); /* 使用 FTPS從www.example.com讀取遠程文件 */ $ftpsfile = file_get_contents ( "ftps://user:pass@ftp.example.com/foo.txt" ); ``` PHP還可以通過context和filter對包裝類進行修飾和增強 (1)關于context,如PHP通過stream_context_create()來設置獲取文件超時時間,這段代碼大家肯定用過: ``` $opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>60, ) ); $context = stream_context_create($opts); $html =file_get_contents('http://www.jb51.net', false, $context); ``` (2)關于filter過濾器,首先來看看PHP有哪些內置的過濾器: ``` print_r(stream_get_filters()); /* Array( [0] => convert.iconv.* [1] => mcrypt.* [2] => mdecrypt.* [3] => string.rot13 [4] => string.toupper [5] => string.tolower [6] => string.strip_tags [7] => convert.* [8] => consumed [9] => dechunk [10] => zlib.* ) */ ``` 通過stream\_filter\_register()和內置的php\_user\_filter可創建自定義的過濾器,如下: ``` /* Define our filter class */ class strtoupper_filter extends php_user_filter { function filter ( $in , $out , & $consumed , $closing ) { while ( $bucket = stream_bucket_make_writeable ( $in )) { $bucket -> data = strtoupper ( $bucket -> data ); $consumed += $bucket -> datalen ; stream_bucket_append ( $out , $bucket ); } return PSFS_PASS_ON ; } } /* Register our filter with PHP */ stream_filter_register ( "strtoupper" , "strtoupper_filter" ) or die( "Failed to register filter" ); $fp = fopen ( "foo-bar.txt" , "w" ); /* Attach the registered filter to the stream just opened */ stream_filter_append ( $fp , "strtoupper" ); fwrite ( $fp , "Line1\n" ); fwrite ( $fp , "Word - 2\n" ); fwrite ( $fp , "Easy As 123\n" ); fclose ( $fp ); readfile ( "foo-bar.txt" ); /* 結果如下: LINE1 WORD - 2 EASY AS 123 */ ``` context是一組stream相關的參數或選項,使用context可以修改或增強包裝類的行為。例如使用context來修改HTTP包裝器是一個常用到的使用場景。 這樣我們就可以不使用cURL工具,就能完成一些簡單的網絡操作。下面是一個例子: ``` $opts = array( 'http'=>array( 'method'=>"POST", 'header'=> "Auth: SecretAuthTokenrn" . "Content-type: application/x-www-form-urlencodedrn" . "Content-length: " . strlen("Hello World"), 'content' => 'Hello World' ) ); $default = stream_context_get_default($opts); readfile('http://localhost/dev/streams/php_input.php'); //在上面是修改默認context的參數,當然我們也可以創建一個新的context,進行交替使用。 $other_opts=array(...); $alternative = stream_context_create($other_opts); readfile('http://localhost/dev/streams/php_input.php', false, $alternative); ``` 在上面的例子中,內容被嵌入到request的body中,這樣遠端的腳本就可以使用php://input來讀取這些內容。同時,我們還能使用apache_request_headers()來獲取request的header ~~~ Array ( [Host] => localhost [Auth] => SecretAuthToken [Content-type] => application/x-www-form-urlencoded [Content-length] => 11 ) ~~~
                  <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>

                              哎呀哎呀视频在线观看