Giới thiệu
Danh sách tùy chọn ngữ cảnh cho transport https:// và https://:
| method | Phương thức HTTP server từ xa hỗ trợ. Mặc định GET. |
| header | Header bổ sung gửi trong yêu cầu. |
| user_agent | Giá trị gửi với header User-Agent. Mặc định dùng thiết lập user_agent php.ini. |
| content | Dữ liệu bổ sung gửi sau header. Thường dùng với POST/PUT. |
| proxy | URI chỉ định địa chỉ proxy server. |
| request_fulluri boolean | Khi TRUE, toàn bộ URI dùng khi xây dựng yêu cầu. Mặc định FALSE. |
| follow_location | Theo dõi redirect header Location. Đặt 0 để tắt. Mặc định 1. |
| max_redirects | Số redirect tối đa theo dõi. |
| protocol_version | Phiên bản HTTP protocol. Mặc định 1.0. |
| timeout | Timeout đọc tính bằng giây, float (ví dụ 10.5). |
| ignore_errors | Lấy nội dung ngay cả khi mã trạng thái thất bại. Mặc định FALSE. |
Ví dụ lấy header và nội dung từ URL https://:
Ví dụ
<?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)); ?>Kết quả
Hiển thị header và 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" }