<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ed&#039;s BlogEd&#039;s Blog &#187; PHP AGI</title>
	<atom:link href="http://edmundlong.com/edsBlog/category/asterisk/php-agi/feed/" rel="self" type="application/rss+xml" />
	<link>http://edmundlong.com/edsBlog</link>
	<description>Stuff I&#039;m working on at the moment</description>
	<lastBuildDate>Thu, 26 Apr 2012 09:37:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>SSH onto iTouch, PHP AGI optimisation and hangup code for Asterisk</title>
		<link>http://edmundlong.com/edsBlog/91/</link>
		<comments>http://edmundlong.com/edsBlog/91/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 23:02:35 +0000</pubDate>
		<dc:creator>edlong</dc:creator>
				<category><![CDATA[Archive Post]]></category>
		<category><![CDATA[Asterisk]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[PHP AGI]]></category>

		<guid isPermaLink="false">http://edmundlong.com/edsBlog/?p=91</guid>
		<description><![CDATA[Hi all, Have been back on the php trail again recently. On problem I’ve had is to do with executing a certain piece of code every 20 seconds. One way in which I was doing it was: [code lang="php"] startTime = time(); while(true) { endTime = time(); if(endTime - startTime &#62; 20) { startTime = [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fedmundlong.com%2FedsBlog%2F91%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fedmundlong.com%2FedsBlog%2F91%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Hi all,</p>
<p>Have been back on the php trail again recently. On problem I’ve had is to do with executing a certain piece of code every 20 seconds. One way in which I was doing it was:</p>
<p>[code lang="php"]<br />
startTime = time();</p>
<p>while(true)</p>
<p>{    endTime = time();</p>
<p>if(endTime - startTime &gt; 20)</p>
<p>{    startTime =  time();</p>
<p>//Code</p>
<p>}</p>
<p>}<br />
[/code]</p>
<p>This however is quite harsh on the CPU. If came up with a better and more obvious way to do time-based looping</p>
<p>[code lang="php"]<br />
while(true)</p>
<p>{    sleep(20);</p>
<p>//Code</p>
<p>}<br />
[/code]</p>
<p>This loops around every 20 seconds and runs the piece of code and is much nicer on the CPU <img src='http://edmundlong.com/edsBlog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I’ve been using PHP AGI which hooks into the Asterisk Extensions.conf dialplan.</p>
<p>One of the other problems I’ve been having is that the php scripts weren’t being killed by asterisk when a user hangs up in the middle of the call.</p>
<p>The solution is to register a handler to react to the hangup event</p>
<p>[code lang="php"]<br />
if(function_exists('pcntl_signal'))</p>
<p>{    pcntl_signal(SIGHUP,'sig_handler');</p>
<p>}<br />
[/code]</p>
<p>This registers the function sig_handler to be run when a hangup is detected within the php script execution</p>
<p>[code lang="php"]<br />
function sig_handler($signo)</p>
<p>{    //Other code, CDR and closing MYSQL conns</p>
<p>     exit(0);</p>
<p>}<br />
[/code]</p>
<p>The exit closes the php script thus freeing up the CPU.</p>
<p>I ssh’ed onto my iTouch yesterday. I followed the instructions here <a href="http://hacktheiphoneitouch.blogspot.com/2007/12/installing-ssh-client-on-112-iphone.html">http://hacktheiphoneitouch.blogspot.com/2007/12/installing-ssh-client-on-112-iphone.html</a> for the MAC, works nicely. Put on Apache and PHP, not tested them as of yet but I’m sure they’ll work nicely as a simple webserver. Haven’t upgraded from 1.1.2 as of yet couldn’t be bothered jailbreaking again but when 1.1.3 is jailbroken out I’ll consider upgrading. Want to get the mail, stocks etc new apps at some stage……free preferably. Thinking of looking for a ColdFusion server for the iTouch, major long shot but would be hilarious if I got it working!</p>
<div class="fullcircle-social-links" style="display: block;"></div><div style="clear: both;"></div>]]></content:encoded>
			<wfw:commentRss>http://edmundlong.com/edsBlog/91/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Agi &#8211; beware!</title>
		<link>http://edmundlong.com/edsBlog/php-agi-beware/</link>
		<comments>http://edmundlong.com/edsBlog/php-agi-beware/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 22:58:18 +0000</pubDate>
		<dc:creator>edlong</dc:creator>
				<category><![CDATA[Archive Post]]></category>
		<category><![CDATA[Asterisk]]></category>
		<category><![CDATA[PHP AGI]]></category>

		<guid isPermaLink="false">http://edmundlong.com/edsBlog/?p=88</guid>
		<description><![CDATA[Hi all, I setup a new install of Asterisk on a Virtual Ubuntu Server this week. As expected I ran into several problems when trying to get the whole thing up and running, mostly to do with CDR. I’m using a mySQL backend to record CDR data. I obtained the asterisk-addons package and the instructions [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fedmundlong.com%2FedsBlog%2Fphp-agi-beware%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fedmundlong.com%2FedsBlog%2Fphp-agi-beware%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Hi all,</p>
<p>I setup a new install of Asterisk on a Virtual Ubuntu Server this week. As expected I ran into several problems when trying to get the whole thing up and running, mostly to do with CDR. I’m using a mySQL backend to record CDR data. I obtained the asterisk-addons package and the instructions indicate to do the usual ./configure, make and make install to cleanly install the cdr_addon_mysql module which is loaded by Asterisk at startup and is used to connect and write to the MySQL DB. The build goes fine, no problems indicated along the way.</p>
<p>So I went and got all the sql conf files up to speed(add cdr_addon_mysql to modules.conf, set enabled to yes in cdr.conf and add a mysql module to the end of it and altered cdr_mysql.conf &#038; res_mysql.conf to match my DB settings) but nothing happened to my CDR table. After looking at the modules loaded in Asterisk I saw that the cdr_addon_mysql.so wasn’t being loaded. Seeing that I went back to the Makefile in the asterisk-addons directory and saw that asterisk.h was missing. Looking at the Asterisk wiki I was indicated to create a symbolic link to /usr/src/asterisk so that the build could find Asterisk. I did this but the error persisted. I placed the asterisk.h in the same directory but still the same problem.</p>
<p>Become impatient I looked for other ways to get the mysql module installed. I added the lines deb:ftp://ftp.us.debian.org/debian unstable main and deb-src ftp://ftp.us.debian.org/debian unstable main to /etc/apt/sources.list and did an apt-get update. Now I was able to do apt-get install asterisk-mysql and the addon was automatically got and placed in my /usr/lib/asterisk/modules and loaded up at startup of Asterisk <img src='http://edmundlong.com/edsBlog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Still isn’t working though my settings must be wrong somewhere.</p>
<p>Here is what I did to install Asterisk….</p>
<pre class="brush:plain">
 Need to get install seperately :
    apt-get install Asterisk
    apt-get install Build-essential
    apt-get instal subversion

    Setup Call Logging CDR - still not working fully
    MySQL - cmd MySQL for CDR
    apt-get instal Mysql-server
    apt-get install Mysql-devel
    Download asterisk-addons from digium svn and compile, this is whats giving me bother building is failing. Follow instructions above.

    Execute MYSQL scripts to create database tables. Grant privileges to asterisk@localhost user.

    apt-get Install PHP5-cli php5-common php5-pear php5-mysql

    Install SOX and Lame for mp3 playback and file conversions
    apt-get install sox lame mpg123

    For TTS:
    apt-get festival festvox-8k
    Change /etc/Festival.scm to match one at http://www.voip-info.org/wiki/view/Asterisk+festival+installation

    Update festival.scm to set the default to be voice_rab_diphone
    Change the .scm /usr/share/festival/voices/whatever the voice is called/festvox/voicename.scm. Search for Duration_Stretch and change this value. Restart festival and test. I used 1.4 for rab_diphone.

    Get PHPAGI.php and the other agi modules at http://phpagi.sourceforge.net/ put them in /usr/share/asterisk/bin directory
    Get Magpie RSS for podcast reader and place MagpieRSS directory in /usr/share/asterisk/bin
    Create a cache directory that is writable by Asterisk in the /usr/share/asterisk/bin directory

    Copy existing iax.conf,sip.conf, extensions.conf over to /etc/asterisk

    Also copy existing cdr.conf, jabber.conf, gtalk.conf, festival.conf, dsnmgr.conf,cdr_custom.conf,res_mysql_conf

    Need to get new sound files and put in /usr/share/asterisk/sounds
    Get php scripts and put in /var/lib/asterisk/agi-bin OR /usr/share/asterisk/bin

    Disable res_pgsql and res_odbc
</pre>
<p>Have been doing a lot of work with PHP AGI in the last week. I’ve been charged with the job of grabbing some XML from a database, parsing it and then playing back a bunch of files whilst recording user input. So looking around I saw that SimpleXML is a nice easy way of parsing XML with PHP and is built into PHP5. Sweet. So I began to go parse some of the xml using SimpleXML…below is an XML snippet like what I had:</p>
<pre class="brush:sql">
<root>
    <child>xyz</child>
</root>
</pre>
<p>So I basically created the SimpleXMLElement</p>
<pre class="brush:php">
$theXML = new SimpleXMLElement('<root><child>xyz</child></root>');
</pre>
<p>Then to access the child of the root element I did this:</p>
<pre class="brush:php">
$child = $theXML->child;
</pre>
<p>All good so far. To see what was coming back I used the ‘verbose’ function of phpagi. As all that was being saved in the child variable was a string I was suprised to see what was being output on the Asterisk console:</p>
<pre class="brush:php">
SimpleElement Object
{
    [0] -> xyz
}
</pre>
<p>So I began to think an array was being saved in ‘child’ and tried accessing and outputting $child[o]. Again the verbose function output the EXACT same output for the $child[0] and $child. Confused I was going around in circles and doing lots of tutorials to see where I was going wrong but couldn’t find it, it is very simple after all its hard to get wrong!</p>
<p>The problem was to do with the php agi VERBOSE function which does a print_r on the passed in information. I decided to test if Asterisk would convert the ‘array’ in $child to text so I used the text2wav function to output the variable. Funny thing was it would just say ‘xyz’ which is exactly what I wanted in $child. So essentially I was troubleshooting a problem that didn’t exist all because of some funky output from the verbose function.</p>
<p>All in all my advice to any php agi Asterisk programmers would be : DON’T TRUST THE PHP AGI VERBOSE FUNCTION!!</p>
<div class="fullcircle-social-links" style="display: block;"></div><div style="clear: both;"></div>]]></content:encoded>
			<wfw:commentRss>http://edmundlong.com/edsBlog/php-agi-beware/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP AGI Hangup</title>
		<link>http://edmundlong.com/edsBlog/php-agi-hangup/</link>
		<comments>http://edmundlong.com/edsBlog/php-agi-hangup/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 21:53:24 +0000</pubDate>
		<dc:creator>edlong</dc:creator>
				<category><![CDATA[Archive Post]]></category>
		<category><![CDATA[Asterisk]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP AGI]]></category>

		<guid isPermaLink="false">http://edmundlong.com/edsBlog/?p=13</guid>
		<description><![CDATA[Hi all, Hangups are handled by PHP AGI by registering a sig_handler function. Whenever a user hangs up then this function is called. However as I found out recently you cannot use the agi within the hangup handler. You can use the verbose command and see what it outputs to the system/error log as the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fedmundlong.com%2FedsBlog%2Fphp-agi-hangup%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fedmundlong.com%2FedsBlog%2Fphp-agi-hangup%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Hi all,</p>
<p>Hangups are handled by PHP AGI by registering a sig_handler function. Whenever a user hangs up then this function is called. However as I found out recently you cannot use the agi within the hangup handler. You can use the verbose command and see what it outputs to the system/error log as the verbose command outputs to the default php error log as well as to the Asterisk CLI. Here is some simple code to register your hangup handler:</p>
<pre class="brush:php">
function sig_handler($signo)
{	//Do some stuff in here
	exit(0);
}

//Register the hangup handler
if (function_exists('pcntl_signal'))
{
      pcntl_signal(SIGHUP,  "sig_handler");
}
</pre>
<p>I’ve no idea what pcntl_signal is but its installed alongside Asterisk by default I believe so you shouldn’t need to do anything extra to get it working.</p>
<p>If you are stuck, the approach we took is to set some variables in the dialplan and have some conditional statements within the dialplan to check these variables. There is a special extension ‘h’ which is called when a hangup occurs. The conditional Gotoif checks for a boolean statement which if true will attempt to go to the first extension, if false it will go to the second extension.</p>
<pre class="brush:plain">
exten =&gt; h,1,GotoIf($[${EXISTS(${aVar})}]?2:4)
exten =&gt; h,2,DoSomething()
exten =&gt; h,3,GotoIf(${aVar} &gt; 0 ? 4:5)
exten =&gt; h,4,Goto(some_extension)
exten =&gt; h,5,Goto(some_extension)
</pre>
<p>PS. I’ve noted that labels don’t appear to be working(for me at least) in the dialplan. By labels I mean:</p>
<pre class="brush:php">
exten =&gt; h(myLabel),2,DoSomething()
</pre>
<p>According to the docs I should be able either of below within a GotoIf:</p>
<pre class="brush:php">
exten =&gt; h,1,GotoIf($[${EXISTS(${aVar})}]?myLabel:4)
exten =&gt; h,1,GotoIf($[${EXISTS(${aVar})}]?2(myLabel):4)
</pre>
<p>Both of these fail for me, telling me that the extension doesn’t exist.</p>
<div class="fullcircle-social-links" style="display: block;"></div><div style="clear: both;"></div>]]></content:encoded>
			<wfw:commentRss>http://edmundlong.com/edsBlog/php-agi-hangup/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
