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 opensips.org 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.

Prerequisites

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 opensips.org - 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

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

<body>

<?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() ?>

  <pre>
  <?php 

    try{

      $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');

      $api->setDebug(true);

      // if your SIP service doesn't accept anonymous inbound calls uncomment two lines below
      //$api->setUsername('auth_username');
      //$api->setPassword('auth_password');

      $api->addHeader('Subject: click2call');
      $api->setMethod('INVITE');
      $api->setFrom('sip:c2c@'.$api->getSrcIp());
      $api->setUri($from);

      $res = $api->send();

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

        $api->setMethod('BYE');
        $api->send();

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

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

      echo $res;

    } catch (Exception $e) {

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

  ?>
  </pre>
  <hr />

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

<?php else : ?>

  <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
    <fieldset>
      From: <input type="text" name="from" size="25" value="" />
      To: <input type="text" name="to" size="25" value="sip:enum-test@sip.nemox.net" />
      <input type="submit" value="Call" />
    </fieldset>
  </form>

<?php endif ?>

</body>
</html>


Next we need to download PHP-SIP class from code.google.com/p/php-sip/downloads/list and extract it into your web server root directory, so it looks as shown below:

/web-root/c2c.php
/web-root/php-sip/PhpSIP.class.php
/web-root/php-sip/PhpSIP.Exception.php
/web-root/php-sip/README


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 opensips.org 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 opensips.org 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 opensips.org into fields (3) and (4)
  • enter your password into field (6)
  • select SIP server tab (7) and enter opensips.org 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:

opensips.org, registration succeeded (expires = 3600 secods)


3. Testing

Open a web browser and navigate to our "Click to Call" page. Enter sip:your_username@opensips.org into From field, leave default sip:enum-test@sip.nemox.net in To field and click Call button. Twinkle should indicate incoming call...

Twinkle - incoming call

...click Answer and your phone will be automatically connected to sip:enum-test@sip.nemox.net which is a public ENUM test service provided by nemox.net group.

Congratulations! Your Click to Call web page works!

Summary

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.



Comments

Gravatar
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:

from: sip:91112233@my-asterisk.ip.com
to: sip:1000@my-asterisk.ip.com

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!

Gravatar
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:

Uncomment:
$api->setUsername('1000');
$api->setPassword('auth_password');

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

Replace:
$api->setFrom('sip:c2c@'.$api->getSrcIp());
with:
$api->setFrom('sip:1000@asterisk_ip');

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

Add:
usleep(500000);
before:
$api->setMethod('REFER');

Gravatar
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@192.168.1.10
To: 1000@192.168.1.10

I did below changes in c2c.php:
$api->setUsername('1000');
$api->setPassword('1234');

$api->setFrom('sip:1000@192.168.1.10');

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

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

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



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...

Thanks

Gravatar
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.

Gravatar
Kathy 2009-11-18 18:27:12

Hi,

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:
sip:[mynum]*[ext]@[sip.provider.com]
Call to:
sip:[phonenum]@[sip.provider.com]

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

Any help would be great, Thanks

Gravatar
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
internet?

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

Gravatar
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

Thanks

Gravatar
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: http://opensips.org

Gravatar
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 sip:kloulamoez@opensips.org to sip:enum-test@sip.nemox.net ...

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

Gravatar
Chris 2010-02-14 22:15:33

Hi,

Try replacing:

$api = new PhpSIP();

with

$api = new PhpSIP('you_server_IP_address');

Gravatar
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...

Gravatar
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?

Gravatar
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.

Gravatar
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 my.sip.net:23 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('my.sip.net')
#2 {main}

Gravatar
Chris 2010-02-27 16:17:39

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

$api = new PhpSIP('10.10.10.12');

instead of:

$api = new PhpSIP('my.sip.net');

Gravatar
RobiGo 2010-03-29 08:55:32

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

Gravatar
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.

Gravatar
Ricardo 2010-03-30 02:57:46

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


Trying call from sip:-----@opensips.org to sip:enum-test@sip.nemox.net ...

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}

Gravatar
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

Gravatar
Ricardo 2010-04-02 06:40:29

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


Trying call from sip:----@opensips.org to sip:enum-test@sip.nemox.net ...

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}

Gravatar
Balfi 2010-04-04 01:52:29

Hi:-)

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 sip.provider.com:24 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('sip.provider.com')
#2 {main}
What is the problem? :-)

Gravatar
Chris 2010-04-06 07:52:58

@Balfi - you need to use:

$api = new PhpSIP('you_server_IP_address');

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

and NOT:

$api = new PhpSIP('sip.provider.com');

Gravatar
prox 2010-04-15 12:35:06

Hi I'm getting the following error:

Trying call from sip:095xxxxxxx@freephonie.net to sip:enum-test@sip.nemox.net ...

--> INVITE sip:095xxxxxxx@freephonie.net SIP/2.0
No final response in fr_timer seconds.

any idea why ?


Thanks.

Gravatar
Carel 2010-04-18 22:19:32

Hi

I am getting the following error:

Trying call from sip:966xxxx@sip.inphonex.com to sip:enum-test@sip.nemox.net ...

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

Can you help in this regard?

Tanks
#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}

Gravatar
Christoph 2010-04-25 14:11:35

My Problem:

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

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('95.156.208.50')
#3 {main}

Gravatar
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.

Gravatar
Christoph 2010-04-25 14:19:05

Line 58: private $lock_file;

What write i here?

Gravatar
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";

Gravatar
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.

Gravatar
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.

Gravatar
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.

Gravatar
Alan 2010-05-20 11:29:34

SIP server :10.6.3.29 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@10.6.3.29 to sip:1002@10.6.3.29 ...
The two X-lite system seems like nothing happened.
Is there something wrong ?

Gravatar
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

api->setmethod('BYE');
api->send();


but it doesn work, thanks in advance for your help

Gravatar
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:

api->setmethod('CANCEL');
api->send();

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.

Gravatar
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();
$api->setUsername('user');
$api->setPassword('password');
only and it was working very well, after that i commented these 2 lines
$api->setUsername('user');
$api->setPassword('password');
and then this error shows up

Opps... Caught exception:exception 'PhpSIPException' with message 'Failed to bind xxx.xxx.xxx.xxx:38 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?

Gravatar
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] = "10.11.12.13";
...

foreach $url_array as $i => $url
{
try
{
$api = new PhpSIP();
$api->setMethod('OPTIONS');
$api->setFrom('sip:'.$i.'@'.$url);
$api->setUri('sip:'.$i.'@'.$url);

$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?

Gravatar
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

Gravatar
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.

Gravatar
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...

Gravatar
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?

Gravatar
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).

Gravatar
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:

$api->setProxy('proxy_ip');

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

Trying call from sip:tnelson@starpoint.com to sip:enum-test@sip.nemox.net ...

--> INVITE sip:tnelson@starpoint.com SIP/2.0
<-- SIP/2.0 100 Trying
<-- SIP/2.0 487 LR2 - User not registered on this client
--> ACK sip:tnelson@starpoint.com SIP/2.0
487

Thanks for any help in advance.

Gravatar
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.

Gravatar
alirsm 2010-09-09 15:54:47

Hi,

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?

Thanks,
alirsm

Gravatar
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 http://code.google.com/p/php-sip/source/browse/trunk/PhpSIP.class.php#849 - this is where you need to add more parsing functions, if you want to extract more data from your incoming requests.

Gravatar
alirsm 2010-09-09 16:24:15

Chris,

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

<?php
require_once('PhpSIP.class.php');

try
{
$api = new PhpSIP('209.16.123.160'); // 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.




Gravatar
alirsm 2010-09-15 19:20:50

found my issue:

$api->setMethod('MESSAGE');
$api->listen('MESSAGE');

Gravatar
Patricio 2010-09-22 15:56:22

Chris,
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.

Gravatar
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?

Gravatar
Zorro 2010-10-05 01:08:33

PLEASE HELP ME:

Trying call from sip:10007@voipfon.org to sip:enum-test@sip.nemox.net ...

Opps... Caught exception:exception 'PhpSIPException' with message 'Failed to bind 95.156.209.18:2 Permission denied' in /var/www/web0/html/api.voipfon.org/PhpSIP.class.php:1315
Stack trace:
#0 /var/www/web0/html/api.voipfon.org/PhpSIP.class.php(260): PhpSIP->createSocket()
#1 /var/www/web0/html/api.voipfon.org/c2c.php(23): PhpSIP->__construct('95.156.209.18')
#2 {main}

Gravatar
Chris 2010-10-06 10:44:31

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

svn checkout http://php-sip.googlecode.com/svn/trunk/ php-sip

This issue has been fixed now.

Gravatar
Christoph 2010-10-06 23:09:35

@Chris

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

Gravatar
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 172.16.85.35:15 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?

Gravatar
Nek 2010-11-02 11:33:34

Hi,

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.

Gravatar
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!

Gravatar
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 ...

Gravatar
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

Gravatar
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
$api->call_id
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;

$api->setmethod('BYE');
$api->send();

I just can't seem to get the BYE request to work. I've tried adding (in varying combinations) the settings for:
$api->setFrom('sip:'...
$api->setUri('sip:'...
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.

Gravatar
Chris 2010-12-16 00:19:40

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

http://code.google.com/p/php-sip/source/browse/trunk/PhpSIP.class.php?r=28#1133

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();
$api->setFrom('sip:yyy');
$api->setUri('sip:xxx');
$api->setMethod('BYE');
$api->setCallId($call_id_from_db);
$api->send();

Good luck!

Gravatar
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:

11d2481ec2772224f83e4e80192b544a@10.6.1.84

(The IP is that of the server, hosting the web page GUI. 10.4.1.66 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?

Gravatar
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 - http://tools.ietf.org/search/rfc3261#page-69

Gravatar
Junior 2011-02-24 12:36:46

Hello, I'm having the following error:

Opps... Caught exception:exception 'PhpSIPException' with message 'Failed to bind 192.168.30.209:5070 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('192.168.30.209')
#2 {main}

Gravatar
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 192.168.30.209 is not a valid address.
Regards,
Chris

Gravatar
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.

Gravatar
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!
Chris

Gravatar
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 sip:enum-test@sip.nemox.net from (myuserid)@voiptalk.org 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 sip:test@sip.nemox.net 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*******9@voiptalk.org to sip:enum-test@sip.nemox.net ...

--> INVITE sip:8*******9@voiptalk.org 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
180

Back
--------------------------------------------------

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)

Gravatar
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!!

Gravatar
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 voiptalk.org (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?

Gravatar
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 voiptalk.org 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!!

Gravatar
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 192.168.1.2:5065 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('192.168.1.2')
#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!!

Gravatar
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));
}

Gravatar
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('192.168.1.2');

change this to:

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

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

Gravatar
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.

Gravatar
Chris 2011-07-22 09:49:13

Hi,

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

PhpSIP->listen('NOTIFY')

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.

Regards,
Chris

Gravatar
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@10.0.0.30 to sip:6789@10.0.0.30 ...

--> INVITE sip:1234@10.0.0.30 SIP/2.0
<-- SIP/2.0 100 Trying
<-- SIP/2.0 200 OK
--> ACK sip:1234@10.0.0.60:5060 SIP/2.0
--> REFER sip:1234@10.0.0.60:5060 SIP/2.0
--> BYE sip:1234@10.0.0.60:5060 SIP/2.0
<-- INVITE sip:c2c@10.0.0.80:5065 SIP/2.0
<-- INVITE sip:c2c@10.0.0.80:5065 SIP/2.0
<-- INVITE sip:c2c@10.0.0.80:5065 SIP/2.0
<-- INVITE sip:c2c@10.0.0.80:5065 SIP/2.0
<-- INVITE sip:c2c@10.0.0.80:5065 SIP/2.0
<-- INVITE sip:c2c@10.0.0.80:5065 SIP/2.0

Where:
10.0.0.30 is the SIP proxy
10.0.0.60 is the VOIP server
10.0.0.80 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?

Gravatar
guy 2011-11-28 18:45:54

hello,

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 sip:201@sip.pssympa.org to sip:200@sip.pssympa.org ...

--> INVITE sip:201@sip.pssympa.org SIP/2.0
<-- SIP/2.0 100 Trying
<-- SIP/2.0 200 OK
--> ACK sip:201@192.168.0.14:15060 SIP/2.0
--> REFER sip:201@192.168.0.14:15060 SIP/2.0
<-- SIP/2.0 407 Proxy Authentication Required
--> REFER sip:201@192.168.0.14:15060 SIP/2.0
<-- SIP/2.0 407 Proxy Authentication Required
--> BYE sip:201@192.168.0.14:15060 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

Gravatar
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:

$api->listen('OPTIONS')

Cheers,
Chris

Gravatar
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 :)

Gravatar
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?

Gravatar
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@192.168.1.100 to sip:5001@192.168.1.100 ..." 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 192.168.1.102] PHP Fatal error: Call to undefined function sys_get_temp_dir() in /var/www/html/PhpSIP.class.php on line 254, referer: https://192.168.1.100/c2c.php". 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?

Gravatar
Chris 2012-03-30 12:44:39

@William

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 =
rtrim(sys_get_temp_dir(),DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'phpSIP.lock';

You will also need to set line 58 to:

private $lock_file = 'path_to_your_lock_file';

Regards,
Chris

Gravatar
Bob 2013-07-29 16:50:12

Hi,
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@192.168.1.76 to sip:1001@192.168.1.76 ...
--> INVITE sip:1002@192.168.1.76 SIP/2.0
<-- SIP/2.0 100 Trying
<-- SIP/2.0 407 Proxy Authentication Required
--> ACK sip:1002@192.168.1.76 SIP/2.0
407

Gravatar
Chris 2013-07-29 16:54:12

Hi,

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

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

$api->setUsername("Your_SIP_Username");
$api->setPassword("Your_SIP_Password");

Regards,
Chris



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.

Categories

Enter your email address:

Delivered by FeedBurner

RSS Feed Subscribe with in your feed reader