Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP http://
The http:// and https:// wrappers in PHP enable read-only access to resources and files through the HTTP protocol. When handling virtual name-based hosts, the host: header is sent along with user_agent (if configured in php.ini).
The HTTP header information is stored in the $http_response_header variable. These headers help identify the URL of the resource where the document originates from using the from: header. HTTPS is supported only if the openssl extension is enabled in php.ini settings.
Syntax
The HTTP/HTTPS wrapper can be used in different formats −
http://localhost http://example.com http://localhost?name='Ram'&age=20 https://example.com http://username:password@abc.com
Key Features
HTTP and HTTPS wrappers have the following characteristics −
- Read-only access − Cannot write or copy files
- Metadata support − Access to headers and connection info
- SSL/TLS support − HTTPS requires openssl extension
- Authentication support − Basic HTTP authentication
Example
The following example demonstrates reading header metadata from an HTTPS URL −
<?php
$url = 'https://www.tutorialspoint.com/php7/php7_closure_call.htm';
if (!$fp = fopen($url, 'r')) {
trigger_error("Unable to open URL ($url)", E_USER_ERROR);
}
$meta = stream_get_meta_data($fp);
print_r($meta);
fclose($fp);
?>
The output shows detailed metadata including SSL information and HTTP headers −
Array(
[crypto] => Array(
[protocol] => TLSv1.2
[cipher_name] => ECDHE-RSA-AES128-GCM-SHA256
[cipher_bits] => 128
[cipher_version] => TLSv1/SSLv3
)
[timed_out] =>
[blocked] => 1
[eof] =>
[wrapper_data] => Array(
[0] => HTTP/1.0 200 OK
[1] => Age: 1310067
[2] => Cache-Control: max-age=2592000
[3] => Content-Type: text/html; charset=UTF-8
[4] => Date: Mon, 14 Sep 2020 17:15:36 GMT
[5] => Server: ECS (nag/99AA)
[6] => Content-Length: 24102
[7] => Connection: close
)
[wrapper_type] => http
[stream_type] => tcp_socket/ssl
[mode] => r
[uri] => https://www.tutorialspoint.com/php7/php7_closure_call.htm
)
Conclusion
PHP's HTTP/HTTPS wrappers provide a simple way to read remote content and access metadata. They're ideal for consuming web APIs, downloading files, or checking HTTP headers programmatically.
