Use cURL with SNI (Server Name Indication)
Server Name Indication (SNI) is an extension to the Transport Layer Security (TLS) protocol that allows a client to specify the hostname it is trying to connect to at the start of the handshaking process.
Server Name Indication (SNI) is an extension to the Transport Layer Security (TLS) protocol that allows a client to specify the hostname it is trying to connect to at the start of the handshaking process. This is useful when a server is hosting multiple websites on the same IP address, as it allows the server to present the correct SSL/TLS certificate for the hostname the client is trying to reach.
By default, modern cURL automatically handles SNI for standard HTTPS requests, so no special options are needed to enable it.
Here is an example of using cURL to make an HTTPS request to a website that uses SNI:
Example of using cURL to make an HTTPS request to a website that uses SNI
curl https://example.comYou could also use the -v option to get more detailed information about the request, which will show the SNI extension in the SSL/TLS handshake:
Example of using the -v option in cURL to get more detailed information about the request
curl -v https://example.comYou can also use the --resolve option to override DNS resolution while keeping the original hostname for the SNI extension:
Example of using the --resolve option to set the SNI hostname explicitly in cURL
curl --resolve example.com:443:192.0.2.1 https://example.comThis tells cURL to connect to the IP address 192.0.2.1 for example.com while still sending example.com in the SNI extension.
Since this snippet is categorized under PHP, here is how you would implement the same request in PHP. The PHP cURL extension handles SNI automatically by default:
$ch = curl_init('https://example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);