First, PHP provides the [man]sockets[/man] extension for basic socket support. You'll want to ensure that it's installed/enabled on your system (e.g. by uncommenting the appropriate "extension=" line in your php.ini file).
Next, it's a simple matter of reading through the PHP manual and understanding how sockets work in PHP. If you have any experience with sockets at all (say in C/C++), you'll find that the operations are very similar; if you haven't, learning them isn't too hard (especially if you at least understand the basics of how sockets/communication works even in a broad/basic sense).
For example, your sockets-based PHP script will simply need to:
Create a listening socket on a given port number. Use [man]socket_create_listen/man for basic AF_INET sockets (e.g. IPv4), or the more comprehensive [man]socket_create/man otherwise.
If you used [man]socket_create/man above, you'll then need to bind the socket to a specific interface/port ([man]socket_bind/man) and then get ready to listen for connections on that socket ([man]socket_listen/man). Otherwise, continue on...
Call [man]socket_accept/man to accept the next connection on the port number bound to the socket you created. Note that this is a "blocking" function, meaning your PHP script will be suspended at this point until it receives a connection (at which time the execution will then continue).
If you're expecting multiple connections, you'll want to use [man]socket_accept/man in a loop (e.g. it will get called to "accept" or handle each incoming connection). Make sure you have a stopping condition, if necessary/applicable, so that the script knows when to break out of this loop (e.g. no longer expecting/accepting any further connections).
Call [man]socket_recv/man to receive data from the connected socket (note: use the new socket resource returned by [man]socket_accept/man - not the one that was originally created when calling the _create function from above).
Note that this page in the manual would appear to have a rather complete coding example illustrating the code structure for socket-based communication in a PHP script.
It would be nice if you gracefully ended the communication, e.g. by calling [man]socket_shutdown/man and then [man]socket_close/man (once all of the data from the socket's data buffer has been "read" and discarded).
If you've never dealt with socket-based programming before, it might seem a little daunting at first. Read through the examples in the manual (especially the one I noted above) and start piecing it together bit-by-bit (no pun intended). Start small - try to get a working PHP script that does nothing but sets up a listening socket, reads in data, echo's it out to you, and then shuts down the socket. Once you can get that simple example working, you can easily expand it to do what you're wanting to do above.
Even if you don't understand a certain piece of it, try doing your best at it (or just leave in some pseudo-code placeholder) and try to see how much you actually can do. Then you can post the code that you came up with here and we can see where you went astray and/or what you're missing.