Socket connection and fetching CLI of incoming call


I want to fetch the callerid when and incoming call is send to a particular extension.

Suppose I have and extension and I wan to to fetch the callerid and display it, how can i do it from php, any ideas ?

<?php $socket = fsockopen("","5038", $errno, $errstr, $timeout); fputs($socket, "Action: Login\r\n"); fputs($socket, "UserName: user\r\n"); fputs($socket, "Secret: password\r\n\r\n"); fputs($socket, "Events: On\r\n\r\n"); fputs($socket, "Action: Command\r\n"); fputs($socket, "Command: Status [1001]\r\n\r\n"); $wrets=fgets($socket,128); ?>