<!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>
Dmitry
October 16, 2009 15:17
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!
Alan
October 16, 2009 19:47
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’);
Mehul
November 13, 2009 11:54
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
Chris
November 13, 2009 12:06
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
November 18, 2009 18:27
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
Chris
November 21, 2009 09:24
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
Franck Chionna
January 6, 2010 03:02
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
Chris
January 6, 2010 08:36
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
kloula moez
February 14, 2010 21:51
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
Chris
February 14, 2010 22:15
Hi,
Try replacing:
$api = new PhpSIP();
with
$api = new PhpSIP(‘you_server_IP_address’);
Pablo
February 20, 2010 21:18
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
February 23, 2010 16:06
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
February 23, 2010 21:17
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
February 27, 2010 10:23
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}
Chris
February 27, 2010 16:17
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’);
RobiGo
March 29, 2010 08:55
I can modify this script to call 2 phone numbers? (not 2 sip users)
Chris
March 29, 2010 08:59
@RobiGo – If your SIP server is a PSTN gateway at the same time, you can call phone numbers with this script.
Ricardo
March 30, 2010 02:57
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}
Chris
March 30, 2010 19:20
@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
April 2, 2010 06:40
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}
Balfi
April 4, 2010 01:52
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? 🙂
Chris
April 6, 2010 07:52
@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’);
prox
April 15, 2010 12:35
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.
Carel
April 18, 2010 22:19
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}
Christoph
April 25, 2010 14:11
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}
Alan
April 25, 2010 14:15
@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
April 25, 2010 14:19
Line 58: private $lock_file;
What write i here?
Alan
April 25, 2010 14:23
@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
May 17, 2010 08:39
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
May 17, 2010 11:50
@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.
Michael
March 21, 2015 02:03
Hi, a few years later, does the audio support for sending stream has been released? I look forward to the same thing as Richa.
Thanks!
Chris
March 21, 2015 05:41
Hi, PHP is a server side technology. Audio support would require client (browser) side solution. The only options available to implement real time audio streaming within a web browser are Adobe Flash (full of security issues and not available on some platforms – Android / iOS) or Web RTC (none of which has anything to do with PHP). Regards, Chris
Richa
May 18, 2010 02:00
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
May 20, 2010 11:29
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 ?
omar zorrilla
June 9, 2010 17:47
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
Chris
June 10, 2010 14:49
@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.
omar zorrilla
June 10, 2010 20:21
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?
WitchDoc
July 19, 2010 14:56
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: $resn”;
}
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
August 1, 2010 01:07
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
August 1, 2010 11:33
@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
August 30, 2010 14:52
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
August 31, 2010 15:03
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
August 31, 2010 17:15
@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
August 31, 2010 22:35
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.
gReg
September 1, 2010 12:26
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
September 9, 2010 15:54
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
Chris
September 9, 2010 16:03
@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.
alirsm
September 9, 2010 16:24
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: $resn”;
} 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
September 15, 2010 19:20
found my issue:
$api->setMethod(‘MESSAGE’);
$api->listen(‘MESSAGE’);
Patricio
September 22, 2010 15:56
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.
Christoph
October 4, 2010 23:45
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
October 5, 2010 01:08
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}
Chris
October 6, 2010 10:44
@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.
Christoph
October 6, 2010 23:09
@Chris
Thanks for Support. Script worked with this new Files from Google.
Cade
October 8, 2010 06:05
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?
Nek
November 2, 2010 11:33
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.
Chris
November 2, 2010 11:45
@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
November 2, 2010 11:58
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
November 2, 2010 19:36
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
December 15, 2010 00:05
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.
Chris
December 16, 2010 00:19
@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!
Davey
December 17, 2010 18:39
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?
Chris
December 17, 2010 18:48
@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
Junior
February 24, 2011 12:36
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}
Chris
February 25, 2011 09:27
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
Junior
February 25, 2011 17:48
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
February 26, 2011 08:23
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
Kirk
March 23, 2011 16:26
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)
kirk
March 23, 2011 16:40
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
March 23, 2011 16:55
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?
kir
March 24, 2011 10:57
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!!
Kirk
March 24, 2011 11:28
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!!
Chris
March 24, 2011 11:46
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
July 12, 2011 20:27
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 :))
Sean
July 21, 2011 18:59
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
July 22, 2011 09:49
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
Sean
July 27, 2011 17:52
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?
guy
November 28, 2011 18:45
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
Rom
December 1, 2011 09:13
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
December 1, 2011 09:51
@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
Chris
December 1, 2011 10:01
@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
March 25, 2012 10:04
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?
Chris
March 30, 2012 12:44
@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
Bob
July 29, 2013 16:50
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
Chris
July 29, 2013 16:54
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
razaq
June 22, 2014 04:51
Hi.
I’m very glad that i found this script..
but what i want to ask is, Is it posible i receive the phone call not using sip softphone but also using web to answer?
Chris
July 18, 2014 16:04
Hi, you will not be able to receive (or make) calls with PHP SIP class. It doesn’t handle media (audio) streams. It can only handle SIP Signaling. To make/receive calls directly from Web Browser you would need to look at some WebRTC solution.
razaq
June 22, 2014 04:58
hi..
do you know how to fix this issue?
—————————————————-
Opps… Caught exception:exception ‘Exception’ with message ‘Only sip: URI supported.’ in /home/rozak/ta/call/php-sip/PhpSIP.class.php:572
Stack trace:
#0 /home/rozak/ta/call/c2c.php(37): PhpSIP->setUri(‘2000@192.168.1….’)
#1 {main}
—————————————————-
Chris
July 18, 2014 16:02
Hi, you would need to change
PhpSIP->setUri(’2000@192.168.1….’)
to:
PhpSIP->setUri(’sip:2000@192.168.1….’)
Note: all URIs need to start with
sip:
Regards,
Chris
razaq
June 22, 2014 05:24
hi Chris
i got this issue when i try to run my c2c.php
————————————————–
Trying call from sip:2000@192.168.1.7 to sip:2001@192.168.1.7 …
–> INVITE sip:2000@192.168.1.7 SIP/2.0
< -- SIP/2.0 401 Unauthorized --> ACK sip:2000@192.168.1.7 SIP/2.0
–> INVITE sip:2000@192.168.1.7 SIP/2.0
< -- SIP/2.0 100 Trying <-- SIP/2.0 503 Service Unavailable --> ACK sip:2000@192.168.1.7 SIP/2.0
503
————————————————–
what should i do next?
Chris
June 22, 2014 07:39
Hi,
You would need to figure out why your SIP Server at 192.168.1.7 is responding with 503 Service Unavailable error.
Best regards,
Chris
hamdi
July 2, 2014 22:33
Hi Chris
I’m an intern at a telecommunications operator and my task is to create an application that enables me to send sip requests with the header fields values that i specify , for example generate a register request with the via,from,to,contact and authentication … header fields values that i supply.
is this possible using php-sip ?
Chris
July 18, 2014 15:57
Hi, yes, you should be able to do this with PHP SIP class. Look at
addHeader($header)
method – you can add any arbitrary header with it.Regards,
Chris
Fadice
September 4, 2014 02:59
Hi,
I’m a newcomer in this field and my boss ask me to build a system like…
A user,B user,web server and sip server.
A want to call B and click B’s link on web,then web server call B
then sip proxy connect A’s and B’s phone and can normally talk.
Can php-sip help me to do it?
and sorry for my English,I just use google to translate it….
Chris
September 4, 2014 05:17
Hi, yes, you will be able to use PHP-SIP to accommodate your scenario. Essentially you can almost copy/paste code from the example above except your
$from = 'userA@sipserver.com'
and$to = 'userB@sipserver.com'
Alex
January 5, 2015 17:52
I am triyng to make a call from 4500@asterisk-ip to 45001@asterisk-ip that is in my internal network.
I followed the steps you mention when the user Dmitry ask you for the same thing but It does not work for me 🙁
I didnt get any errors just a “blank” page after submiting
Chris
January 13, 2015 15:21
Hi, can you try adding at the very top of your page
error_reporting(E_ALL);
and see if you get any errors?Ben
January 27, 2015 15:08
Hi Chris,
Just wanted to say thank you for the excellent article – We were being blocked by a vendor and using the basis of your article we have been able to provide a potential Customer the functionality that they require.
Kind regards
Mangesh
April 5, 2015 06:27
hi,
i am trying to connect sip phone as you tell us but i have follow error
Trying call from sip:mangesh41@opensips.org to sip:enum-test@sip.nemox.net …
Opps… Caught exception:exception ‘PhpSIPException’ with message ‘Failed to bind http://opensips.org:8 Unknown host’ in /var/www/php-sip/PhpSIP.class.php:1311
Stack trace:
#0 /var/www/php-sip/PhpSIP.class.php(256): PhpSIP->createSocket()
#1 /var/www/c2c.php(26): PhpSIP->__construct(‘http://opensips…’)
#2 {main}
Chris
April 5, 2015 06:31
Hi, class constructor takes (optional) parameter which is local IP address on your machine you want it to bind to. For example:
$php_sip = new PhpSIP('192.168.10.1')
. Regards, ChrisMangesh
April 5, 2015 14:25
Does this work with my own configured SIP server?
If the socket connect fails – How do I confirm if my SIP server is listening?
I have configured SIP server using MATRIX hardware….
http://www.matrixtelesol.com/eternity-pe.html
Will this script work with this product?
The twinkle soft phone works perfectly.
Mangesh
April 5, 2015 14:31
also do I need to setup any proxy server?
or is it mandatory to have a opensips.org account?
Chris
April 11, 2015 05:27
In theory you don’t need a proxy server. You can send SIP traffic directly to the endpoints. However in practice a proxy server (and registrar) is much more practical. You don’t need to use opensips.org account. You can use any SIP Proxy/Registrar you like.
Alexander
May 8, 2015 14:10
Hello! I can call from sip to mobile phone? My SIP provider can. But i not understant how me call to mobile phone from c2c.php.
For example.
My sip account sip:user@provider.com
my mobile phone number is 79255552211
How me make connect mobile phone and sib account?
require_once(‘PhpSIP.class.php’);
$api = new PhpSIP();
$api->setDebug(true);
$api->setUsername($user);
$api->setPassword($pass);
$api->addHeader(‘Subject: click2call’);
$api->setMethod(‘INVITE’);
$api->setFrom(‘sip:79255552211@provider.com’);
$api->setUri(‘sip:user@provider.com’);
$res = $api->send();
if ($res == 200) {
usleep(500000);
$api->setMethod(‘REFER’);
$api->addHeader(‘Refer-to: sip:79255552211@provider.com’);
$api->addHeader(‘Referred-By: sip:user@provider.com”);
$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();
}
It’s possible? Where error? Thank’s!
Chris
May 10, 2015 07:31
Hi, yes, it is possible by all means. Your example would work just fine if you wanted to test it with our VoIP service http://voipstudio.com/en/ If it doesn’t work with your provider, the best option would be to collect PCAP traces of SIP Signalling during your click2call session and contact their support. Regards, Chris.
Alexander
May 9, 2015 12:04
SIP/2.0 415 Unsupported Media Type
Chris
May 10, 2015 07:37
Hi, that would mean SIP client you are using doesn’t accept any of the codecs (G711, G729, ilbc, speex) specified here https://code.google.com/p/php-sip/source/browse/trunk/PhpSIP.class.php#489 (which is rather odd as G711 is being accepted by virtually any SIP client out there). To fix this issue you would need to investigate what codecs are being accepted by your SIP endpoint and update the code mentioned above. Regards, Chris
Tomasz
May 16, 2015 19:52
call from sip:101@127.0.0.1 to sip:enum-test@sip.nemox.net …
–> INVITE sip:101@127.0.0.1 SIP/2.0
ACK sip:101@127.0.0.1 SIP/2.0
Opps… Caught exception:exception ‘PhpSIPException’ with message ‘Missing username’ in C:\xampp\htdocs\c2c\php-sip\PhpSIP.class.php:1186
Stack trace:
#0 C:\xampp\htdocs\c2c\php-sip\PhpSIP.class.php(689): PhpSIP->auth()
#1 C:\xampp\htdocs\c2c\c2c.php(126): PhpSIP->send()
#2 {main}
Please help me, dear Chris…
I use Xampp, 3CX Central and Jitsi client on Windows.
Chris
May 20, 2015 21:30
Hi, the solution should be simple… use
setUsername()
andsetPassword()
methods to set your SIP credentials.Vyacheslav
November 19, 2015 17:32
Hi, i need to register sip-client and wait incoming call.
Can you show example code?
Thank you.
Cristiano
May 11, 2016 16:45
Hello can be used to do the same w/o a software phone?
I mean do the same but let ring my hw ip phone.
Example
i call : sip:103@192.168.1.1 and when 103 respond my sip:105@192.168.1.1 will ring too.
Chris
May 12, 2016 05:58
Yes, you can use this class to make your hardware phone ring. However when it answers you need to perform REFER to transfer the call to some other SIP endpoint with media capabilities, as this PHP SIP class doesn’t handle any media (it is only capable of SIP Signalling).
Sean
August 1, 2016 16:41
HI
I wonder if you could help me with the following error:
“Trying call from sip:sip1@sip2sip.info to sip:sip2@sip2sip.info …
Opps… Caught exception:exception ‘PhpSIPException’ with message ‘Failed to send data. Operation not permitted’ in /home/…/sip/PhpSIP.class.php:737
Stack trace:
#0 /home/…/sip/PhpSIP.class.php(671): PhpSIP->sendData(‘INVITE sip:sip1@@sip2sip.info’)
#1 /home/…/sip/c2c.php(48): PhpSIP->send()
#2 {main} ”
Any thoughts would be very helpful.
Thanks,
Sean
Chris
August 1, 2016 19:09
Hi, you are getting exception here:
https://github.com/level7systems/php-sip/blob/master/PhpSIP.class.php#L835
which indicates http://php.net/manual/en/function.socket-sendto.php function is not allowed in your environment. You would need to check with your ISP whether they allow it.
Regards,
Chris