Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP HTTP Context Options

บทนำ

รายการ context options สำหรับ transports https:// และ https:// ดังนี้

methodHTTP method ที่ remote server รองรับ ค่าเริ่มต้น GET
headerAdditional headers ที่จะส่งใน request
user_agentค่าที่ส่งกับ User-Agent: header โดยค่าเริ่มต้นใช้ user_agent php.ini setting
contentAdditional data ที่ส่งหลัง headers ใช้ทั่วไปกับ POST หรือ PUT requests
proxyURI ระบุที่อยู่ proxy server
request_fulluri booleanเมื่อ TRUE ใช้ URI ทั้งหมดเมื่อสร้าง request ค่าเริ่มต้น FALSE
follow_locationFollow Location header redirects ตั้งเป็น 0 เพื่อปิด ค่าเริ่มต้น 1
max_redirectsจำนวน redirects สูงสุดที่จะ follow
protocol_versionHTTP protocol version ค่าเริ่มต้น 1.0
timeoutRead timeout ในวินาที ระบุเป็น float (เช่น 10.5)
ignore_errorsดึง content แม้ failure status codes ค่าเริ่มต้น FALSE

ตัวอย่างต่อไปนี้ดึง headers และ contents จาก URL https://

ตัวอย่าง

<?php $url = "https://localhost/testscript.php"; $opts = array('http' => array( 'method' => 'GET', 'max_redirects' => '0', 'ignore_errors' => '1' ); $context = stream_context_create($opts); $stream = fopen($url, 'r', false, $context); var_dump(stream_get_meta_data($stream)); ?>

ผลลัพธ์

แสดง header information และ metadata ดังนี้

array(10) { ["timed_out"]=> bool(false) ["blocked"]=> bool(true) ["eof"]=> bool(false) ["wrapper_data"]=> array(7) { [0]=> string(15) "HTTP/1.1 200 OK" [1]=> string(35) "Date: Thu, 17 Sep 2020 07:04:47 GMT" [2]=> string(55) "Server: Apache/2.4.41 (Win64) OpenSSL/1.0.2s PHP/7.1.32" [3]=> string(24) "X-Powered-By: PHP/7.1.32" [4]=> string(17) "Content-Length: 0" [5]=> string(17) "Connection: close" [6]=> string(38) "Content-Type: text/html; charset=UTF-8" } ["wrapper_type"]=> string(4) "http" ["stream_type"]=> string(14) "tcp_socket/ssl" ["mode"]=> string(1) "r" ["unread_bytes"]=> int(0) ["seekable"]=> bool(false) ["uri"]=> string(31) "https://localhost/testscript.php" }