當前位置: 首頁> 函數類別大全> stream_context_set_params

stream_context_set_params

設置流/包裝器/上下文的參數
名稱:stream_context_set_params
分類:溪流
所屬語言:php
一句話介紹:設置上下文參數

函數名稱:stream_context_set_params()

適用版本:PHP 5 >= 5.3.0, PHP 7

函數描述:stream_context_set_params() 函數用於設置上下文參數。

語法:bool stream_context_set_params ( resource $stream_or_context , array $params )

參數:

  • $stream_or_context:必需,一個資源流或者是一個已經創建的上下文資源。
  • $params:必需,一個關聯數組,包含要設置的參數。

返回值:成功時返回true,失敗時返回false。

示例:

  1. 使用stream_context_create() 創建上下文資源,並使用stream_context_set_params() 設置上下文參數:
 $opts = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => http_build_query(array('key1' => 'value1', 'key2' => 'value2')) ) ); $context = stream_context_create($opts); $params = array( 'notification' => 'on' ); if (stream_context_set_params($context, $params)) { $file = file_get_contents('http://example.com', false, $context); echo $file; } else { echo "设置上下文参数失败!"; }

上述示例中,首先使用stream_context_create() 創建了一個上下文資源,並將一些HTTP 請求參數設置在上下文中。然後使用stream_context_set_params() 設置了一個額外的參數'notification'。最後,使用file_get_contents() 函數發送請求並獲取響應。

  1. 使用一個已經創建的上下文資源進行參數設置:
 $context = stream_context_create(); $params = array( 'ssl' => array( 'verify_peer' => true, 'verify_peer_name' => true, 'allow_self_signed' => false ) ); if (stream_context_set_params($context, $params)) { $file = file_get_contents('https://example.com', false, $context); echo $file; } else { echo "设置上下文参数失败!"; }

上述示例中,首先使用stream_context_create() 創建了一個空的上下文資源。然後使用stream_context_set_params() 設置了一個SSL 相關的參數。最後,使用file_get_contents() 函數發送HTTPS 請求並獲取響應。

注意:stream_context_set_params() 函數只能在PHP 5.3.0 及以上版本中使用。

同類函數
熱門文章