Connect To SIP With PHP

Last time, we took a look at how to make sure your web server can see your SIP server. This time, we’re going to carry on with actually making the two talk to each other.

Conceptually, all that needs to happen, is we open a socket connection to SIP. This basically acts as a Telnet session, so we can send a message to the server, and it will reply back with a message. For this reason, we need to use a sleep command, to wait for the server to reply. Depending on your network setup, you may want to experiment with different sleep times to see how short you can go.

Basic Code

<?php
	$sip_server_address = 'MyServerName';
	$sip_server_port = 2023;

	$fp = fsockopen($sip_server_address,$sip_server_port,$errno,$errstr);
	stream_set_blocking($fp, 0);
	
	$check = 'Our_SIP_String_Will_Go_Here';

	fputs($fp, "$check\r");
	sleep(2);

	$response = fread($fp, 256);

	echo $response;
?>

By taking the above code, and swapping in your server’s name (or IP address will work as well), and port number you should receive back a response of 96. As stated before code 96 is SIP’s way of saying it doesn’t understand what you’re asking for. If you get that, you are connected to SIP, and everything is working as it should.

Troubleshooting

If your page is timing out, you likely have a problem with your server name or port. Double check the port, and in a pinch try connecting via IP address. I actually have my server name defined in a hosts file on my server. This may be overkill, but it may also be just the tip you need to get things working. Another idea is to check that you still have a connection to SIP, to rule out network issues.

Next time, we’ll take a look at how to create a real message to SIP, so that we can receive account information back. If you have any success or failures with connection, please let me know by using the comments section.