Chapter 20. Network Programming

PHP has a number of ways to work over a network: the most common protocols have special functions to make often-used functionality easy, but it is possible to use PHP to write any kind of data over any kind of protocol.

Sockets

While it is out of the scope of this book to go into detail about network infrastructure, you at least need to know what protocols, ports , and sockets are. Protocols are like languages, defining how two computers can talk to each other, and there are hundreds of protocols to perform all varieties of operations—there is a protocol for file transfer (File Transfer Protocol, or FTP), a protocol for transferring web pages (Hypertext Transfer Protocol, or HTTP), a protocol for network management (Simple Network Management Protocol, or SNMP), and many more.

Each protocol has a set of ports that it uses, which are theoretical openings in your computer's Internet connection that clients can connect to. They are numbered 1 to 65535, of which the first 1023 are considered reserved for administrative users. By default, your PC "listens" to no ports, meaning that it ignores all incoming connections. However, if you run a web server, it will open up port 80—this is the port for HTTP, where your web server will listen for requests for web pages. Many of the first 1023 ports are used already, which means if you want to use a port for a new service you have written, it is best that you use a number above 1024.

Sockets are the literal connectors ...

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.