In PHP, stream_socket_enable_crypto() is useful when your application needs a specific operation rather than manually reproducing it. Starts or stops transport-level encryption on a compatible connected stream socket.
Stream functions work with PHP stream resources, wrappers, contexts or custom filters. Not every wrapper supports every operation.
What Is stream_socket_enable_crypto() in PHP?
Starts or stops transport-level encryption on a compatible connected stream socket.
Signature:
stream_socket_enable_crypto($stream, bool $enable, ?int $crypto_method = NULL, $session_stream = NULL): int|bool
Parameters:
$stream— An already opened stream resource, not a filesystem path string.$enable— Boolean selecting whether the mode or transport setting is enabled.$crypto_method— Constant selecting the supported TLS mode and protocol family.$session_stream— Connected stream resource to enable or disable TLS on.
Return value: int|bool.
Example 1: Basic Usage
This first example shows the method or function in a minimal context. For environment-dependent operations, adapt the required path, stream or connection before running it.
<?php
$context = stream_context_create(['ssl' => ['verify_peer' => true, 'verify_peer_name' => true]]);
$socket = stream_socket_client('tcp://example.test:443', $errorCode, $errorMessage, 5, STREAM_CLIENT_CONNECT, $context);
if ($socket === false) { throw new RuntimeException($errorMessage); }
$result = stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
if ($result !== true) { fclose($socket); throw new RuntimeException("TLS handshake incomplete or failed"); }
fclose($socket);
The important operation is stream_socket_enable_crypto(). Starts or stops transport-level encryption on a compatible connected stream socket.
Example 2: Another Input or Scenario
The next example changes the input or surrounding context to show how the operation behaves in a second situation.
<?php
$context = stream_context_create(["ssl" => ["verify_peer" => true, "verify_peer_name" => true]]);
$connectedSocket = stream_socket_client("tcp://example.test:443", $errorCode, $errorMessage, 5, STREAM_CLIENT_CONNECT, $context);
if (!$connectedSocket) { throw new RuntimeException($errorMessage); }
$result = stream_socket_enable_crypto($connectedSocket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
if ($result !== true) { throw new RuntimeException("TLS negotiation failed"); }
fclose($connectedSocket);
Compare the inputs and the handling of the return value with the first example. Avoid assuming that filesystem paths, resource states or binary bytes are interchangeable.
Practical Example: Applying the Operation
This example illustrates how the operation fits into a small application workflow. Paths, streams and secrets must be created or provided securely by the calling application.
<?php
$context = stream_context_create(["ssl" => ["verify_peer" => true, "verify_peer_name" => true]]);
$nonblockingSocket = stream_socket_client("tcp://example.test:443", $errorCode, $errorMessage, 5, STREAM_CLIENT_CONNECT, $context);
if (!$nonblockingSocket) { throw new RuntimeException($errorMessage); }
stream_set_blocking($nonblockingSocket, false);
$result = stream_socket_enable_crypto($nonblockingSocket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
if ($result === 0) { /* Wait for stream readiness before retrying. */ }
if ($result === false) { throw new RuntimeException("TLS handshake failed"); }
fclose($nonblockingSocket);
Practical note: Starts or stops transport-level encryption on a compatible connected stream socket.
Important Behavior and Edge Cases
- Starts or stops transport-level encryption on a compatible connected stream socket.
- Check failure values and exceptions; do not assume every environment supports the same stream wrapper, extension or filesystem capability.
- When handling paths, streams or external input, validate inputs before using results and release resources after use.
Comparison With Related Functions
| Function or Method | Behavior |
|---|---|
stream_socket_enable_crypto() | starts TLS negotiation over an established compatible socket. |
stream_socket_client() | creates an outgoing connection, optionally directly over TLS. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Assuming stream_socket_enable_crypto() can be used without its required setup | Provide the correct file, stream, state, key or object before calling it; Starts or stops transport-level encryption on a compatible connected stream socket. |
| Assuming one socket read returns the whole message | Treat socket input as a stream and frame messages explicitly; read until a known boundary or length. |
| Omitting timeouts and failure handling | Check false results, timeouts, and partial writes; close sockets on every error path. |
Key Takeaways
stream_socket_enable_crypto()starts or stops transport-level encryption on a compatible connected stream socket.- Use appropriate input types and verify results before continuing.
- Prefer specialized, safer APIs when the operation concerns untrusted input, encryption or network traffic.
In summary: Starts or stops transport-level encryption on a compatible connected stream socket. Build in error handling and use this function where its specific behavior matches the application requirements.