stream_context_set_params
스트림/래퍼/컨텍스트의 매개 변수를 설정하십시오
함수 이름 : stream_context_set_params ()
해당 버전 : PHP 5> = 5.3.0, PHP 7
함수 설명 : stream_context_set_params () 함수는 컨텍스트 매개 변수를 설정하는 데 사용됩니다.
구문 : bool stream_context_set_params (resource $ stream_or_context, 배열 $ 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 ()를 사용하여 추가 매개 변수 '알림'을 설정하십시오. 마지막으로 file_get_contents () 함수를 사용하여 요청을 보내고 응답을 얻습니다.
$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 이상에서만 사용할 수 있습니다.