(PHP 4 >= 4.3.0, PHP 5, PHP 7)
stream_context_create — 創建資源流上下文
說明 ¶
stream_context_create ([ array
$options
[, array $params
]] ) : resource
創建並返回一個資源流上下文,該資源流中包含了 options
中提前設定的所有參數的值。
參數 ¶
-
options
-
必須是一個二維關聯數組,格式如下:$arr['wrapper']['option'] = $value 。
默認是一個空數組。
-
params
-
必須是 $arr['parameter'] = $value 格式的關聯數組。 請參考 context parameters 里的標准資源流參數列表。
返回值 ¶
上下文資源流,類型為 resource 。
實例:PHP:stream_context_create函數模擬POST/GET請求
<?php $data = array( 'name' => 'zhangsan', 'gender' => 'male', 'age' => 25 ); $query_string = http_build_query($data); $option = array( 'http' => array( 'method' => 'POST', 'header' => array( "Content-type:application/x-www-form-urlencoded", "Contnet-length:".strlen($query_string) ), 'content'=> $query_string ) ); $context = stream_context_create($option); $url = 'http://localhost/test.php'; $content = file_get_contents($url,false,$context); echo $content;
test.php文件:
<?php print_r($_POST);
請求返回的結果:
Array ( [name] => zhangsan [gender] => male [age] => 25 )
注意:method中的方法名稱必須是大寫!