Click to Call with PHP-SIP

posted by Chris on 2009-10-07 22:29:27

In this tutorial we will show how to implement "click to call" functionality in a web page using PHP-SIP class, free SIP registrar service and Twinkle softphone.

The same principle can be used with any RFC compliant SIP service or your own PBX such as Asterisk or OpenSIPs.


In order to accomplish scenario shown in a diagram below, you will need the following:

  • Basic knowledge of SIP protocol.
  • PHP-SIP class
  • Free SIP account from - this will be our SIP Proxy and sip:user1@sip as shown in the diagram below. Alternatively any SIP compliant SIP service or your own SIP Proxy can be used instead.
  • PHP (version >= 5) enabled web server.
  • Twinkle softphone (on Ubuntu linux can be installed with the following command: apt-get install twinkle) or any other SIP softphone or normal phone. However we provide detailed instructions for Twinkle only.

SIP Flow diagram

Click to Call SIP flow diagram

  1. User submits a form with calling (sip:user1@sip) and called (sip:user2@sip) parties SIP URIs.
  2. Web Server sends INVITE to sip:user1@sip.
  3. Once INVITE is accepted by user1, web server immediately sends REFER with sip:user2@sip in "Refer-to" header.
  4. Web Server terminates "call" by sending BYE to user1.
  5. As instructed in REFER request sent by a web server, user1 sends INVITE to sip:user2@sip.

1. Create Click to Call web page

Go to your web server root directory and create c2c.php file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="" lang="en" xml:lang="en">

<head><title>PHP-SIP Click to Call</title></head>


<?php if (isset($_POST['from']) && isset($_POST['to'])) : ?>

  <?php require_once('php-sip/PhpSIP.class.php') ?>

  <?php $from = $_POST['from']; $to = $_POST['to'] ?>

  Trying call from <?php echo $from ?> to <?php echo $to ?> ...<br />

  <?php flush() ?>



      $api = new PhpSIP();
      // if you get "Failed to obtain IP address to bind. Please set bind address manualy."
      // error, use the line below instead
      // $api = new PhpSIP('you_server_IP_address');


      // if your SIP service doesn't accept anonymous inbound calls uncomment two lines below

      $api->addHeader('Subject: click2call');

      $res = $api->send();

      if ($res == 200)
        $api->addHeader('Refer-to: '.$to);
        $api->addHeader('Referred-By: sip:c2c@'.$api->getSrcIp());


        $api->reply(481,'Call Leg/Transaction Does Not Exist');

      if ($res == 'No final response in 5 seconds.')
        $res = $api->send();

      echo $res;

    } catch (Exception $e) {

      echo "Opps... Caught exception:";
      echo $e;

  <hr />

  <a href="<?php echo $_SERVER['PHP_SELF']; ?>">Back</a>

<?php else : ?>

  <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
      From: <input type="text" name="from" size="25" value="" />
      To: <input type="text" name="to" size="25" value="" />
      <input type="submit" value="Call" />

<?php endif ?>


Next we need to download PHP-SIP class from and extract it into your web server root directory, so it looks as shown below:


Now open the web browser and navigate to http://you_web_server/c2c.php which should show a form similar to:

Click to Call form

2. Register SIP Phone

For the purpose of this tutorial we assume our test account at is jsmith and softphone used for our test will be Twinkle. However you can use any other SIP proxy and software or normal SIP phone.

Once your SIP account at is set up, start Twinkle, select Edit => User profile... and enter your SIP account details as shown below:

Twinkle settings

  • enter your username into fields (1) (2) and (5)
  • enter into fields (3) and (4)
  • enter your password into field (6)
  • select SIP server tab (7) and enter into field (8)
  • select SIP protocol tab (9) and deselect Ask user permission for transfer check box

...finally click OK button and if everything went well your Twinkle will display:, registration succeeded (expires = 3600 secods)

3. Testing

Open a web browser and navigate to our "Click to Call" page. Enter into From field, leave default in To field and click Call button. Twinkle should indicate incoming call...

Twinkle - incoming call Answer and your phone will be automatically connected to which is a public ENUM test service provided by group.

Congratulations! Your Click to Call web page works!


The above illustrates the simplest possible scenario. In a real life applications you may want to use fancy Ajax instead of standard HTML form. Fetch From and/or To SIP URIs from database or do number of even more exciting things.


Dmitry 2009-10-16 15:17:02

How can I place an outbound call and then connect it with my extension?

I try to do as following:


where 91112233 is number 111-2233 called via 9-trunk
and 1000 - my extension.

but it does not call anywhere...
can somebody tell me if my task is reachable with phpsip ?

thanx for help!

Alan 2009-10-16 19:47:57

Hi Dmitry, thanks for your interest in PHP-SIP project.

Yes, your scenario is possible. You will need to download the latest version (>= 0.3.1) of php-sip and make modifications to c2c.php as shown below:


and replace auth_password as defined in your sip.conf for user 1000.


$api->addHeader('Referred-By: sip:c2c@'.$api->getSrcIp());
$api->addHeader('Referred-By: sip:1000@asterisk_ip');


Mehul 2009-11-13 11:54:51

Thanks for useful information. I am making outbound call thru c2c.php. For that i m putting
From : 19782232254@
To: 1000@

I did below changes in c2c.php:


$api->addHeader('Referred-By: sip:1000@');

After doing this when i m clicking on call it is giving me below error.

Trying call from 19782232254@ to sip:1000@ ...

Fatal error: Call to undefined function sys_get_temp_dir() in /var/www/html/php-sip/PhpSIP.class.php on line 247


What could be the issue...


Chris 2009-11-13 12:06:44

Hi Mehul,

It seems to me you are running PHP version 4.x, and it is missing sys_get_temp_dir() function.

You will need to use PHP version >= 5.2 with PhpSIP.

Kathy 2009-11-18 18:27:12


This is a great script. I have a small problem that maybe you could help me with. I am trying to use the class to make an outgoing call from a webpage. I have done all the things above you stated to do. No matter what configuration or port options I set, I always get No Final Response in 5 seconds.

My sip listens on port 5090
My client softphone listens on port 5060

Call from:
Call to:

I have put in my user and password in the c2c.php file.

Any help would be great, Thanks

Chris 2009-11-21 09:24:16

Hi Kathy, I will try to help, but can you please let me know the following:

1. What OS do you use? Window/Linux
2. What version of PHP do you use?
3. What is your network layout? Is the web server hosting c2c.php on
the same network as your softphone? Is it a private LAN or public

Could you please email the above info to chris [at-sign-here] level7systems [dot] co [dot] uk

Franck Chionna 2010-01-06 03:02:20

hello Level7,

is php-sip can be used to as user management (create, modify, credits etc..) ? also is it possible to use pstn phone number format ?
sorry for my newbie question, I'm fairly new in SIP world


Chris 2010-01-06 08:36:53

Hi Franck,

No, unfortunately you can't manage user accounts with php-sip. You would need SIP registrar/proxy server and database back end for this.

I can recommend openSIPs software:

kloula moez 2010-02-14 21:51:29

hello level7
I use ubuntu 9.01 and i have some problem withe the installation and i have this message that i don't resolve please help me to understand :

Trying call from to ...

Opps... Caught exception:exception 'Exception' with message 'Invalid argument' in /opt/lampp/htdocs/php-sip/PhpSIP.class.php:730
Stack trace:
#0 /opt/lampp/htdocs/php-sip/PhpSIP.class.php(664): PhpSIP->sendData('INVITE sip:klou...')
#1 /opt/lampp/htdocs/c2c.php(39): PhpSIP->send()
#2 {main

Chris 2010-02-14 22:15:33


Try replacing:

$api = new PhpSIP();


$api = new PhpSIP('you_server_IP_address');

Pablo 2010-02-20 21:18:15

it worked very well... mmm... i tried this one many time without any success, until i tried a different PC... i'm still wondering why it didn't work in my laptop but my desktop PC. Important: i tried using the X-Lite and it worked as well as in a linux based PC using Twinkle. Thanks bro...

Peter 2010-02-23 16:06:26

I think this is just what I was looking for!

I'm just reading through the class now, any chance that I could read a hint status i.e. BLF button to show presence on the web page?

Chris 2010-02-23 21:17:50

Hi Peter. I am afraid you will not be able read BLF status with PHP-SIP. What you need is to SUBSCRIBE to phone BLF events sent via NOTIFY packets. To do this you will need a SIP server listening all the time in the background. This is currently not supported by PHP-SIP.

Tigran 2010-02-27 10:23:56

How can I make external calls betwwen two external phone numbers?
I try to use my SIP server, but:
Opps... Caught exception:exception 'PhpSIPException' with message 'Failed to bind Invalid argument' in /www/php-sip/PhpSIP.class.php:1311
Stack trace:
#0 /www/php-sip/PhpSIP.class.php(256): PhpSIP->createSocket()
#1 /www/php-sip/c2c.php(23): PhpSIP->__construct('')
#2 {main}

Chris 2010-02-27 16:17:39

Hi Tigran, try using your server IP address, instead of domain name. For example:

$api = new PhpSIP('');

instead of:

$api = new PhpSIP('');

RobiGo 2010-03-29 08:55:32

I can modify this script to call 2 phone numbers? (not 2 sip users)

Chris 2010-03-29 08:59:26

@RobiGo - If your SIP server is a PSTN gateway at the same time, you can call phone numbers with this script.

Ricardo 2010-03-30 02:57:46

my php is 5
I try with other sip, and then with and I get the message below.
Whats the problem?

Trying call from to ...

Opps... Caught exception:exception 'PhpSIPException' with message 'socket_create() function missing.' in /home/www/----/php-sip/PhpSIP.class.php:228
Stack trace:
#0 /home/www/----/c2c.php(23): PhpSIP->__construct()
#1 {main}

Chris 2010-03-30 19:20:54

@Ricardo - Your PHP does not support "sockets".

On Linux platform you need to
compile PHP with "--enable-sockets"

On Windows add
"extension=php_sockets.dll" to your php.ini

Ricardo 2010-04-02 06:40:29

I again, I am trying in another host now, I get this message?

Trying call from to ...

Opps... Caught exception:exception 'PhpSIPException' with message 'Failed to send data. Operation not permitted' in /home/injernet/www/c2c/php-sip/PhpSIP.class.php:737
Stack trace:
#0 /home/injernet/www/c2c/php-sip/PhpSIP.class.php(671): PhpSIP->sendData('INVITE sip:---...')
#1 /home/injernet/www/c2c/c2c.php(39): PhpSIP->send()
#2 {main}

Balfi 2010-04-04 01:52:29


I like, if my server call me on events. I have more voip numbers and access in my sip provider.
Try the c2c.php (PHP5, linux), and output:

Opps... Caught exception:exception 'PhpSIPException' with message 'Failed to bind Cannot assign requested address' in /var/www/php-sip/PhpSIP.class.php:1327
Stack trace:
#0 /var/www/php-sip/PhpSIP.class.php(272): PhpSIP->createSocket()
#1 /var/www/php-sip/c2c.php(23): PhpSIP->__construct('')
#2 {main}
What is the problem? :-)

Chris 2010-04-06 07:52:58

@Balfi - you need to use:

$api = new PhpSIP('you_server_IP_address');

for example: $api = new PhpSIP('');

and NOT:

$api = new PhpSIP('');

prox 2010-04-15 12:35:06

Hi I'm getting the following error:

Trying call from to ...

--> INVITE SIP/2.0
No final response in fr_timer seconds.

any idea why ?


Carel 2010-04-18 22:19:32


I am getting the following error:

Trying call from to ...

Opps... Caught exception:exception 'PhpSIPException' with message 'Failed to bind Permission denied' in /home/novtel/public_html/test/php-sip/PhpSIP.class.php:1311
Stack trace:

Can you help in this regard?

#0 /home/novtel10/public_html/test/php-sip/PhpSIP.class.php(256): PhpSIP->createSocket()
#1 /home/novtel10/public_html/test/c2c.php(23): PhpSIP->__construct()
#2 {main}

Christoph 2010-04-25 14:11:35

My Problem:

Trying call from sip:10001@ to sip:10002@ ...

Opps... Caught exception:exception 'PhpSIPException' with message 'Failed to open lock file /tmp/phpSIP.lock' in /var/www/web1/html/kastius/sip/php-sip/PhpSIP.class.php:303
Stack trace:
#0 /var/www/web1/html/kastius/sip/php-sip/PhpSIP.class.php(1295): PhpSIP->getPort()
#1 /var/www/web1/html/kastius/sip/php-sip/PhpSIP.class.php(256): PhpSIP->createSocket()
#2 /var/www/web1/html/kastius/sip/c2c.php(23): PhpSIP->__construct('')
#3 {main}

Alan 2010-04-25 14:15:28

@Christoph - looks like your PHP process is not allowed to write to /tmp directory.

You can change location of "phpSIP.lock" file in PhpSIP.class.php - line 58.

Christoph 2010-04-25 14:19:05

Line 58: private $lock_file;

What write i here?

Alan 2010-04-25 14:23:27

@Christoph - a path to any directory that is writeable by your web server + "phpSIP.lock". So for example if your web server can write to "/var/www/rw" directory, your Line 58 will look like:

private $lock_file = "/var/www/rw/phpSIP.lock";

Richa 2010-05-17 08:39:28

Hi Chris,
This script works like magic. Great work!! Thank you. I managed to call my mobile from my asterisk box using this script.

Could we also send/add an automated message to the receiver (after the system calls the given number)? Basically auto-dialing a number and playing a pre-recorded message to the receiver.

Is it possible?
Thanking you in advance.

Chris 2010-05-17 11:50:47

@Richa - Thanks for your interest in our project.

Unfortunately it is not possible to send any Audio streams with PHP-SIP at the moment.

Only SIP signaling is implemented - no RTP (Real Time Protocol) stack is present at this time.

We are planning to add Audio (RTP) support (possibly with an external library) in the future.

Richa 2010-05-18 02:00:01

Thank you for your quick reply.
I appreciate the thought of your script supporting audio as well.
As a workaround, I was thinking whether I could do it by setting "exten" parameters in the extensions.conf file. So internally, when an extension is called it can trigger the voice message. Any ideas?

It would be good to see your script working with some few tweaking in the config files for RTP as well.

Alan 2010-05-20 11:29:34

SIP server : with php 5.2.10
Register 1001 and 1002 with X-lite on two XP system .
After I copy the source code from this page.
It shows like below and stop.
Trying call from sip:1001@ to sip:1002@ ...
The two X-lite system seems like nothing happened.
Is there something wrong ?

omar zorrilla 2010-06-09 17:47:33

Hello, great library, it is working very well for me, only a couple of questions, how can in make it rings more than once? because it rings once, if you dont answer it can be ringing more but when you answer it doesnt connect you to the other party, you are only able to connect to other person in the first ringing, 2nd question, once it is ringing res:180 or call connected res:200 , how can i hang up the call from the page, i created a button with the following code


but it doesn work, thanks in advance for your help

Chris 2010-06-10 14:49:41

@omar - to make it ring longer you will need to edit "Final Response Timer", around line 53:

private $fr_timer = 10;

Default is 10 seconds only.

To terminate unanswered call you can do:


Terminating already established calls with BYE is more complicated. The reason for that is $api object exists only until call is set up (or terminated with CANCEL). You will need to use database (or other persistent storage) to save Call-Id of call you want to terminate, and use this in your BYE request.

omar zorrilla 2010-06-10 20:21:06

Hi thanks for your answer, before you answered me i was testing some changes in the script, at the begining i was using
$api = new PhpSIP();
only and it was working very well, after that i commented these 2 lines
and then this error shows up

Opps... Caught exception:exception 'PhpSIPException' with message 'Failed to bind Permission denied' in /var/www/html/php-sip/PhpSIP.class.php:1311
Stack trace:
#0 /var/www/html/php-sip/PhpSIP.class.php(256): PhpSIP->createSocket()
#1 /var/www/html/c2cold.php(21): PhpSIP->__construct()

i changed $api = new PhpSIP('myip');and all changs you can guess and it is not working anymore , im running this page using https because it is working in the same box as asterisk

any idea?

WitchDoc 2010-07-19 14:56:43

Hi there,

I´m glad to find PHP-SIP but can´t get along yet.
Maybe someone wants to help and give me a hand.

I just want to check the status of several PBX´s in a PHP script like this:

$url_array[0] = "";

foreach $url_array as $i => $url
$api = new PhpSIP();

$res = $api->send();
echo "res1: $res\n";

catch (Exception $e)
echo $e->getMessage()."\n";

It works once, and then ERROR: ...failed to bind ,[...], socket used twice (german: Normalerweise darf jede Socketadresse (Protokoll, Netzwerkadresse oder Anschluss) nur jeweils einmal verwendet werden.)

Any idea?

Cosmin 2010-08-01 01:07:36

I`m trying to call from registered sip to phone number ( no sip ).. I didn`t saw any comment about this ..

it is possible ? how ?

i`m using XP pro with WAMP (php 5+)

thank u in advance

Chris 2010-08-01 11:33:57

@Cosmin, you can't call any PSTN number directly using SIP protocol (with this script or any other way). You will need SIP => PSTN gateway which will translate SIP to old PSTN protocol.

ondercsn 2010-08-30 14:52:33

This library is really great and useful. But it would be better if we can execute any extension with playback options etc.. For ex. before bridging two calls, the first side hears a voice file and then after the answer of other side, it plays another voice file. after end of the voices, the calls are bridged...

gReg 2010-08-31 15:03:51

Any idea of a PHP class that can implement RTP? From here to a predictive dialer is not that long way and a simple predictive dialer is what is currently missing to all of us. Any thoughts, please?

Alan 2010-08-31 17:15:41

@ondercsn and @gReg - it would be extremely difficult to implement RTP stack in a pure PHP. This class aims at providing SIP signalling only, leaving media (RTP streams) to a software which is much better suited for the job (eg. Asterisk).

Tony Nelson 2010-08-31 22:35:05

We have SIP a hosted SIP server. I use Blink on my Mac and it works fine. Adding:


Get's me close, but I think I need to login, I get the following error:

Trying call from to ...

--> INVITE SIP/2.0
<-- SIP/2.0 100 Trying
<-- SIP/2.0 487 LR2 - User not registered on this client
--> ACK SIP/2.0

Thanks for any help in advance.

gReg 2010-09-01 12:26:22

In the manner of a predictive dialer that sends an audio file it should not be difficult even by means of pure PHP.

Also the opposite should be all right, meaning a list of numbers sending an audio file to one number, that will make happy many.

As it is your class is very basic, at most it can be used for a scanner or a basic brute force script. I don't want to minimize your work, but as it is it's not more than a skype call me button. Serious stuff is ahead and the two options above can be implemented and I will probably do it.

Anyway, I didn't meant it without external support. (Your favorite SIP SDK here) is a good example of SDK and it does a lot. The UI and related are javascript, but the core is indeed in some serious program language.

alirsm 2010-09-09 15:54:47


I am trying to use php-sip class to create a 2-way-sms app using sip messages. I can send sms message and am wondering if php-sip class has the function to listen for incoming sip messages. Can I use $api->listen('MESSAGE')? If yes, how can I parser the content of the message?


Chris 2010-09-09 16:03:01

@alirsm, yes, you can use $api->listen('MESSAGE'). This method will set the following properties:

private $req_method;
private $req_cseq_method;
private $req_cseq_number;
private $req_contact;

See - this is where you need to add more parsing functions, if you want to extract more data from your incoming requests.

alirsm 2010-09-09 16:24:15


Thanks for the response.
I wrote a simple php to listen for any incoming sip MESSAGE:


$api = new PhpSIP(''); // IP we will bind to
$res = $api->listen('MESSAGE');
echo "res1: $res\n";

} catch (Exception $e) {

echo $e->getMessage()."\n";

However, when receiving a sip MESSAGE, I am getting:

~/php-sip$ php -f message.php
Unexpected request received.

alirsm 2010-09-15 19:20:50

found my issue:


Patricio 2010-09-22 15:56:22

My Problem:

Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 81264639 bytes).

My php.ini have memory_limit with 128M..

Thanks for any help in advance.

Christoph 2010-10-04 23:45:29

My Problem:

Fatal error: Call to undefined function sys_get_temp_dir() in /var/www/html/c2k/php-sip/PhpSIP.class.php on line 256

I have my Line 58 to:
private $lock_file = "/var/www/rw/phpSIP.lock";

chmod 777

have any idea?

Zorro 2010-10-05 01:08:33


Trying call from to ...

Opps... Caught exception:exception 'PhpSIPException' with message 'Failed to bind Permission denied' in /var/www/web0/html/
Stack trace:
#0 /var/www/web0/html/ PhpSIP->createSocket()
#1 /var/www/web0/html/ PhpSIP->__construct('')
#2 {main}

Chris 2010-10-06 10:44:31

@Christoph Hi, can you please try the latest code from the SVN repository

svn checkout php-sip

This issue has been fixed now.

Christoph 2010-10-06 23:09:35


Thanks for Support. Script worked with this new Files from Google.

Cade 2010-10-08 06:05:40

This sounds like a great package.

What I would like to do is the following:
1) Register a SIP phone
2) Dial a number to do call diverting for this phone. This is just a normal number that the PABX uses to initiate call forwarding, etc.
3) Unregister the phone

I've been playing around but I seem to get:
Failed to bind Permission denied

This is the IP of the unix server I'm using rather than the SIP server.

I've used the SIP IP in:
$api = new PhpSIP

but it doesn't seem to work

Is what I'm trying to do possible?

Nek 2010-11-02 11:33:34


Is it easy to write a method for REGISTER with your script?

I need to be able to register a user from my site so that he can take calls. Do you believe with your script that this can be done?

Thank you.

Chris 2010-11-02 11:45:07

@Nek, Hi, yes it should be very easy to implement REGISTER method. In fact all you need change is $allowed_methods variable (line 63) and add "REGISTER" there. Also don't forget to add Expires header:

$api->addHeader('Expires: 3600');

Good luck!

Nek 2010-11-02 11:58:19

Thank u very much for the quick response and your excellent job.

I will try it as soon as I get home and tell you the results ...

YhwhDesign 2010-11-02 19:36:58

I am new to the hole click 2 call interface, and was wondering if what I am wanting to do is doable... for a lack of better words.

I want to add a button on my site to allow people to call my offices directly from the site. I already have a sip account but have no clue what to do with it from there.

Any help would be great

Davey 2010-12-15 00:05:42

Your class works like a charm for connecting calls. I successfully have a page making an Ajax call to connect a specified agent to a selected phone number. I am also recording the value of
To a database table, so I can retrieve it and use it for a hang-up. I'm having a bit of trouble correctly referencing the unique ID for the correct session, so that it can request the BYE

I changed the class variable $call_id to be public, rather than private, so I can set it. On the call, it's very similar to your example, with some additional code to save the value of $call_id

$data = array('call_start' => date('Y-m-d H:i:s'), 'call_id' => $api->call_id);
if (($call_id = User::preLogCall($data)) === false) {
$err .= 'Failed to create lead_call record';

On the hang-up request, it retrieves the call_id value, which looking through the code, should only set if it doesn't already have a value. So immediately after the class initiation, I set it to the value recorded.
$api = new PhpSIP($_SERVER['SERVER_ADDR']);
$api->call_id = $call_id;


I just can't seem to get the BYE request to work. I've tried adding (in varying combinations) the settings for:
as well as tried setting the value with direct calls like this:
$api->addHeader('CALL-ID: '.$call_id);
$api->addHeader('SIPCALLID: '.$call_id);

Still no success. I did notice that when viewing the asterisk -vr that the value for Call-ID did not match what I was recording from the $api->call_id variable. Can you point me in the right direction? Much appreciation for any help you can be.

Chris 2010-12-16 00:19:40

@Davey - Hi, you would need to rewrite 'setCallId' method. See:

to something like:

public function setCallId($v)
$this->call_id = $v.'@'.$this->src_ip;

and call this method just before ->send(), for example:

$api = new PhpSIP();

Good luck!

Davey 2010-12-17 18:39:08

Thanks for your quick response and feedback. The trouble I'm having is that it more or less was doing things as you specified, although it was in a slightly different order. By making the class variable public rather than private for $call_id, I'm able to pull that value to store, which as I understand it, is the unique identifier for that session. The value being saved from that variable is like this:


(The IP is that of the server, hosting the web page GUI. is the IP of the client making the request)

When I do the hangup request, I'm querying the DB for that unique identifier and assigning it to the class variable $call_id before making the BYE method call and send action. While it doesn't error or fail or spit out any debug information, it doesn't actually do the hangup, even though the request completed without an issues.

I believe the answer to my dilemma, surrounds my lack of understanding as to the settings for the functions setFrom and setUri. The setFrom should always be the server IP address, correct? Is the setUri set to the server IP as well or the client IP address?

I just can't seem to get the previous session referenced so that the hangup request is sent to the correct client. Any ideas where I'm going horribly wrong?

Chris 2010-12-17 18:48:18

@Davey, actually my previous reply wasn't complete. Apart from call_id, you need to ensure From/To tags are the same as in your original request. To do it, you will need to make $to_tag and $from_tag variables public and set them accordingly in your BYE request. See RFC 3261 Dialogs -

Junior 2011-02-24 12:36:46

Hello, I'm having the following error:

Opps... Caught exception:exception 'PhpSIPException' with message 'Failed to bind Cannot assign requested address' in /mnt/hgfs/www/webphone/classes/php-sip/PhpSIP.class.php:1311
Stack trace:
#0 /mnt/hgfs/www/webphone/classes/php-sip/PhpSIP.class.php(256): PhpSIP->createSocket()
#1 /mnt/hgfs/www/webphone/teste.php(18): PhpSIP->__construct('')
#2 {main}

Chris 2011-02-25 09:27:45

Hi Junior, the IP address you try to bind has to be available on the machine which you use to run PHP-SIP script. In your case it seems is not a valid address.

Junior 2011-02-25 17:48:18

Hello Chris,

Thank's your help, and some doubts more.

When I have my softphone open all the calls are redirected to my number, no matter the sip address I fill on the boxes, but If I close my softphone I receive the message: "No final response in fr_timer seconds".

Some questions:
- Must I have my softphone open while I do the tests?
- Can I call a phone number inside my network (phone extension)? (8025, 8024)
- Can I call a phone numer outside my network? (+554839538024)
- How could I call another phone extension using this script, like I do with my phone calling any extension inside the company.

Sorry If I asked any bullshit, and thank you the help.

Chris 2011-02-26 08:23:55

Hi Junior,

You need to remember PHP-SIP Class is NOT a softphone. It can't handle any audio (RTP streams). All it can do is SIP signalling. Therefore yes, if you want to "Call" any number you will need your softphone running to pickup redirected call.

Good luck!

Kirk 2011-03-23 16:26:02

Ok this is exactly what is was looking for, i will be developing a web based call centre software and this is a good place to start.

BUT and here is the BUT:

i have tested this and have the following issues.

note: using x-lite on windows and twinkle on ubuntu yeilds the same result....

when making a "call" to from (myuserid) my softphone rings as expected i answer and nothing, call looks connected but nothing, twinkle states "establishing call, please wait" and after some seconds responds with "Line 1: no ACK received, call will be terminated."

now if i manually enter in the softphone it calls and i hear what i am supposed to hear so thats ok.

output in browser all looks ok no issues.
Trying call from sip:8******* to ...

--> INVITE sip:8******* SIP/2.0
<-- SIP/2.0 100 Trying
<-- SIP/2.0 100 Giving a try
<-- SIP/2.0 180 Ringing
<-- SIP/2.0 180 Ringing
<-- SIP/2.0 180 Ringing


I develop with php quite a lot so know my stuff there but new to sip so this may be an easy solution but it has me stumped!

Please help and when i have finished the software i will be happy to share, (mysql database with numbers, multiple agents, performance reports, all with nice fancy jquery interface)

kirk 2011-03-23 16:40:53

hummmmm interesting i just created a opensips account to test with that an low and behold it works!!!

i also tried it with the 3cx server i have here but nothing, doesnt work

is there a sip setting that has to be enabled for this to work and answer will be a life saver!!

Chris 2011-03-23 16:55:48

Hi Kirk, I am only guessing your web server where PHP-SIP is hosted doesn't get "200 OK" reply from (that is why you see continuous "180 Ringing" even after you answered the initial call). It is most likely something to do with NAT, but I would need to know more about your environment to be able to suggest any solution. Is your server hosting PHP-SIP script on a public IP, or is it behind some NAT device?

kir 2011-03-24 10:57:03

Hi Chris, thanks for the reply.

I have multiple routers / gateways on this network so switched gateway and bang it works!

Strange thing, Draytek Vigor 2820 does not work cheap old linksys works like a dream???

my server(ubuntu server with LAMP) and workstation(windows 7) are behind these nat devices using internal ip's with ports(80, 443) forwarded from router to server, (quite normal setup)

when i did a packet dump using wireshark i can see that my workstation repeatedly sends "200 OK" to with no response when using main internet connection but ok when using backup net connection, defo NAT issue.

Thanks for pointing me in the right direction!!

Kirk 2011-03-24 11:28:41

Damn it, just when it was working it gave up the ghost with

Opps... Caught exception:exception 'PhpSIPException' with message 'Failed to bind Address already in use' in /var/www/dial/php-sip/PhpSIP.class.php:1311
Stack trace:
#0 /var/www/dial/php-sip/PhpSIP.class.php(256): PhpSIP->createSocket()
#1 /var/www/dial/c2c.php(26): PhpSIP->__construct('')
#2 {main}

i have tried changing min port to 5070 5075 5080 etc etc etc and all say the same thing,

this happened after a reboot because it was saying "permission denied" in stead of "already in use" so i though that it had left a port opened so did a reboot and now this message.

Any ideas?

arrr so upset it was working fine!!

Chris 2011-03-24 11:46:34

Hi, you could try adding the following to createSocket method (before calling socket_bind):

if (!socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1))
$err_no = socket_last_error($this->socket);
throw new PhpSIPException (socket_strerror($err_no));

Andrey 2011-07-12 20:27:46

to Kirk:
I guess, you have two lines in the c2c.php like:

$api = new PhpSIP();
$api = new PhpSIP('');

change this to:

//$api = new PhpSIP();
$api = new PhpSIP('');

and everything will be ok
(I had same problem :))

Sean 2011-07-21 18:59:48

I've set up the basic web page on an OpenSuse box and given the correct permissions to our SIP proxy.

When I try to connect a call between two endpoints registered through the proxy, I get the following error:

Opps... Caught exception:exception 'PhpSIPException' with message 'Unexpected request INVITE received.' in /srv/www/htdocs.telecom/c2c/inc/PhpSIP.class.php:764
Stack trace:
#0 /srv/www/htdocs.telecom/c2c/index.php(51): PhpSIP->listen('NOTIFY')
#1 {main}

Any thoughts? If I replace the "sip:1234@ip_address" section with the actual IP addresses of the phones, it works fine.

Chris 2011-07-22 09:49:13


From what I can see below you have the following line:


which makes PHP-SIP listen for NOTIFY request *only*, but what it gets instead is INVITE back from your proxy (which seems odd??) and throws an error:

'Unexpected request INVITE received.'

I would suggest using ngrep or wireshark to get SIP trace and investigate the flow of SIP packets between PHP-SIP and your proxy.


Sean 2011-07-27 17:52:29

Thanks for the follow-up -- I'll try to get a sniffer in there to take a look. Here's the output from PHP-SIP if it helps:

Trying call from sip:1234@ to sip:6789@ ...

--> INVITE sip:1234@ SIP/2.0
<-- SIP/2.0 100 Trying
<-- SIP/2.0 200 OK
--> ACK sip:1234@ SIP/2.0
--> REFER sip:1234@ SIP/2.0
--> BYE sip:1234@ SIP/2.0
<-- INVITE sip:c2c@ SIP/2.0
<-- INVITE sip:c2c@ SIP/2.0
<-- INVITE sip:c2c@ SIP/2.0
<-- INVITE sip:c2c@ SIP/2.0
<-- INVITE sip:c2c@ SIP/2.0
<-- INVITE sip:c2c@ SIP/2.0

Where: is the SIP proxy is the VOIP server is the Linux server

I'm not sure I understand why there would be an invite for the "c2c" (or PHP-SIP) station. Is that coming from the SIP proxy?

guy 2011-11-28 18:45:54


sorry for my english.
i have a SipXecs IPBX and i know that this technologie is a litte bit harder for me.
But i'm trying any way.
I got this error when i try to connetc to an ekiga softphone :

Trying call from to ...

--> INVITE SIP/2.0
<-- SIP/2.0 100 Trying
<-- SIP/2.0 200 OK
--> ACK sip:201@ SIP/2.0
--> REFER sip:201@ SIP/2.0
<-- SIP/2.0 407 Proxy Authentication Required
--> REFER sip:201@ SIP/2.0
<-- SIP/2.0 407 Proxy Authentication Required
--> BYE sip:201@ SIP/2.0
<-- SIP/2.0 200 OK
<-- OPTIONS sip:anonymous@anonymous.invalid SIP/2.0
<-- OPTIONS sip:anonymous@anonymous.invalid SIP/2.0
Opps... Caught exception:exception 'PhpSIPException' with message 'Unexpected request OPTIONSreceived.' in /var/www/php/joomla/php-sip/PhpSIP.class.php:764
Stack trace:
#0 /var/www/php/joomla/c2c.php(51): PhpSIP->listen('NOTIFY')
#1 {main}

can you help me

Chris 2011-12-01 09:51:25

@guy The reason you are getting "Unexpected request OPTIONS received" exception is because after BYE your server sends OPTIONS request. To fix this, you would need to add:



Rom 2011-12-01 09:13:25

Hi, Chris!
I try to create click2call function on my site, but it's hardly for me.

I faced a problem, when i used php-sip class. When i call, the phone is ringing, but when i answer it's no sound in it. I tottaly understand that php-sip class can't handle audio stream, but how can i provide a sound streaming between users?
I've asterisk server and direct connection without NAT or proxy-servers.

Thx, for help, and sorry for my English :)

Chris 2011-12-01 10:01:34

@Rim Hi, can you use packet sniffer to check if there is "OK 200" with correct sdp body in reply to "INVITE" routed from sip:user2@@sip to sip:user1@sip as shown in diagram above?

William 2012-03-25 10:04:47

I downloaded and install the required files as listed above. I have installed this to a Elastix Server and been trying to make it work to no avail.

When I run it, it displays "Trying call from sip:5000@ to sip:5001@ ..." on the web page but nothing else happens. In checking my ssl_error_log I am getting "[Sun Mar 25 04:52:47 2012] [error] [client] PHP Fatal error: Call to undefined function sys_get_temp_dir() in /var/www/html/PhpSIP.class.php on line 254, referer:". I went in and set up a directory in /var/www/html/ as "tmp" and entered that on line 58 of PhpSIP.class.php and made it writeable by asterisk but it still throws this error.

Any suggestions?

Chris 2012-03-30 12:44:39


Looks like your PHP is missing sys_get_temp_dir() function for some reason.

To fix the problem go to public function __construct() method and
comment our the line:

$this->lock_file =

You will also need to set line 58 to:

private $lock_file = 'path_to_your_lock_file';


Bob 2013-07-29 16:50:12

I have a FreeSwitch running on the same computer and when I try the php-sip it gives error like below. Grateful for any comments. Thank you!
Trying call from sip:1002@ to sip:1001@ ...
--> INVITE sip:1002@ SIP/2.0
<-- SIP/2.0 100 Trying
<-- SIP/2.0 407 Proxy Authentication Required
--> ACK sip:1002@ SIP/2.0

Chris 2013-07-29 16:54:12


Looks like your proxy is expecting re-INVITE with authentication header.

You would need to add the following two lines to your code:



Leave Your Comment

Your name: *
Your Email: *   Will not be published
Your comment: *
Captcha image
To help us prevent automated abuse of this service
please enter the text displayed in the image above: *

All submitted reviews/comments become the licensed property of Level 7 Systems Ltd.


Enter your email address:

Delivered by FeedBurner

RSS Feed Subscribe with in your feed reader