stream_context_create解析


(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中的方法名稱必須是大寫!

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM