For both clients, you would just use the PHP Socket Extension to perform the connection and send commands. I recommend the socket extension because it's far more robust than fsockopen() and it's counterparts.
I can't help you on the Yahoo! Messenger front, but I do know the YIM protocol is available on the internet somewhere.
On the MSN front, I can definitely help you. I wrote a PHP-powered MSN bot once, but unfortunately I lost interest in it so I ditched the code (stupid, I know).
Because the MSN Protocol was made publically known by Microsoft (surprise there), it meant people could write their own MSN clients. But I digress....here's some links:
http://www.evilwalrus.com/viewcode.php?codeEx=515
This is the framework for a basic MSN bot. It's what I based mine on.
http://www.hypothetic.org/docs/msn/index.php
This is by far the best outline of the MSN protocol I've read. I also used this resource to develop my old bot. It contains a complete reference to all the server messages and commands, as well as how to communicate with the server. has some research into MSNP8, the latest revision of the MSN protocol.
http://www.venkydude.com/articles/msn.htm
Another good overview.
http://www.cs.berkeley.edu/~mikechen/im/protocols/msn/draft-movva-msn-messenger-protocol-00.txt
The MSN Protocol Draft, as released by Microsoft.
Note: A quick Google search pulled up this resource for the Yahoo! protocol:
http://www.venkydude.com/articles/yahoo.htm
Hope that gave you something to work with 🙂