我從http傳遞給https,因此我必須將StreamContext
添加到幾個read_file
和get_file_contents
調用中。何時StreamContext可重用?何時不應該重複使用?
我需要
$stream_context = stream_context_create([
/* some lenghty options array */
]);
read_file('https://'.$host.$uri, false, $stream_context);
現在我的問題,以取代
read_file('http://'.$host.$uri);
:是一個$ stream_context可重複使用的是這樣的:
$stream_context = stream_context_create([
/* some lenghty options array */
]);
read_file('https://'.$host.$uri, false, $stream_context);
get_file_contents($another_url, false, $stream_context);
read_file($even_another, false, $stream_context);
或者我需要重新創建一個新的每個網址有StreamContext
?
以不同方式提出問題:流上下文只是參數和選項的描述符,還是在使用它時綁定到資源?
編輯:從評論看來,人們可以經常使用StreamContext
,但並非總是如此。作爲答案,這並不令人滿意。
什麼時候可以或應該重用,何時不能重用?有人可以對StreamContext
的內部工作有所瞭解。 documentation看起來相當稀疏。
你可以試試看。我希望它是可重用的,因爲它是一種從未讀取但未寫入的資源。 – apokryfos
我會說它的目的是可重複使用;但它似乎可能會有意想不到的結果(如果我沒有正確使用,我認爲)在這裏評論:http://php.net/manual/en/function.stream-context-create.php#117361 – ilpaijin
@ilpaijin這一點很有趣。 –