PHP Streams API
If we want to work with sockets in PHP, we're offered two sets of methods, starting with one of these two prefixes:
socket_*
: Low-level API to the socket communication available since PHP 4.1. This extension needs to be enabled when compiling PHP with the--enable-sockets
option. You can check whether your PHP supports this API by runningphp -i
in the console and watching for--enable-sockets
under theConfigure Command
option.stream_*
: API introduced in PHP 4.3 that generalizes usage of file, network, and other operations under a unified set of functions. Streams in the sense of this API are resource objects that share some common behavior. This extension is part of PHP and doesn't require any extra steps to be enabled. More stream functions were added in PHP 5, such asstream_socket_server()
, which we'll use in a moment.
In general, we'll always want to use the newer stream_*
API because it's a built-in part of PHP and offers better functionality.
The core feature is that...