I would recommend that you adjust your DB check code to run in a loop. CDR submission occurs <i>after</i> CHANNEL_HANGUP - so you have a race condition and may check the DB before the CDR has been committed. You&#39;d need to handle a) trying again after a short delay if the CDR isn&#39;t there yet and b) a timeout in case xml_cdr fails and the CDR isn&#39;t available (or the script&#39;ll keep running until it is).<br>

<div class="gmail_extra"><br><br><div class="gmail_quote">On 29 November 2012 19:23, Richard Gration <span dir="ltr">&lt;<a href="mailto:richgration@gmail.com" target="_blank">richgration@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Steve,<br>
<br>
Thanks for all your help, I have working code now :-)<br>
<br>
I don&#39;t know if this is a local configuration &quot;feature&quot; but the events<br>
are returned synchronously from the &quot;event plain CHANNEL_HANGUP&quot; call,<br>
so I just loop looking for my uuid.<br>
<br>
Cheers,<br>
Rich<br>
<br>
PS For anyone interested in some alpha code ...<br>
<br>
$dExt = $_GET[&#39;dExt&#39;];<br>
$pin = $_GET[&#39;pin&#39;];<br>
<br>
if ($dExt and $pin) {<br>
<div class="im">    $fp = event_socket_create($host, $port, $password);<br>
<br>
</div>    # How shall we abuse the callee today?<br>
    $wav1 = &#39;ivr/32000/ivr-douche_telecom.wav&#39;;<br>
    $wav2 = &#39;ivr/32000/ivr-thank_you_for_calling.wav&#39;;<br>
<div class="im"><br>
    # Place call and find uuid<br>
</div>    $cmd = &quot;api originate<br>
{ignore_early_media=true,originate_timeout=30}sofia/external/677${dExt}@MGC<br>
&#39;&amp;play_and_get_digits(4 9 1 10000 # $wav1 $wav2 foo \d+)&#39;&quot;;<br>
    $response = event_socket_request($fp, $cmd);<br>
    $response = strip_cr($response);<br>
<div class="im">    $bits = preg_split(&#39;/\s+/&#39;,$response);<br>
</div>    $uuid = $bits[1];<br>
<br>
    $continue = 1;<br>
    $cmd = &quot;event plain CHANNEL_HANGUP&quot;;<br>
    while ($continue) {<br>
        error_log(&quot;running $cmd&quot;);<br>
        $response = event_socket_request($fp, $cmd);<br>
        error_log(&quot;got $response&quot;);<br>
        if (preg_match(&quot;/$uuid/&quot;,$response)) {<br>
            $continue = 0;<br>
        }<br>
    }<br>
<br>
    $dbhRo = DbConnect::GetConnection(&#39;db-readonly&#39;);<br>
    $res_struct = mysql_query(&quot;SELECT strSrcMedia FROM cdr WHERE<br>
strSipSessionId = &#39;$uuid&#39;&quot;,$dbhRo);<br>
    $res = mysql_fetch_array($res_struct);<br>
    $pin_from_user = $res[0];<br>
    $result = $pin == $pin_from_user ? 1 : 0 ;<br>
<br>
    echo &quot;&lt;pre&gt;&quot;;<br>
<div class="im">    echo date(&#39;H:i:s&#39;) . &quot;\n&quot;;<br>
</div>    echo &quot;uuid = $uuid\n&quot;;<br>
    echo &quot;$result\n&quot;;<br>
    echo &quot;&lt;/pre&gt;&quot;;<br>
<br>
    # Close socket<br>
    fclose($fp);<br>
} else {<br>
    echo -1;<br>
<div class="HOEnZb"><div class="h5">}<br>
<br>
<br>
On 29 November 2012 13:56, Steven Ayre &lt;<a href="mailto:steveayre@gmail.com">steveayre@gmail.com</a>&gt; wrote:<br>
&gt;&gt; ... how I listen for these events. Is it just a case of polling the<br>
&gt;&gt; socket for them at frequent enough intervals?<br>
&gt;<br>
&gt;<br>
&gt; You subscribe to the events you&#39;re interested in with &#39;events&#39;<br>
&gt; <a href="http://wiki.freeswitch.org/wiki/Event_socket#event" target="_blank">http://wiki.freeswitch.org/wiki/Event_socket#event</a><br>
&gt;<br>
&gt; There&#39;s no polling involved. FS will send all subscribed events through the<br>
&gt; socket, you just need to receive them. Use select()/poll()/equivalent to<br>
&gt; detect when data has arrived.<br>
&gt;<br>
&gt; Since you already have xml_cdr working that would be the best place to<br>
&gt; collect it, if you can add an extra DB insert in the handler.<br>
&gt;<br>
&gt; -Steve<br>
&gt;<br>
&gt;<br>
&gt; On 29 November 2012 11:58, Richard Gration &lt;<a href="mailto:richgration@gmail.com">richgration@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; Hi,<br>
&gt;&gt;<br>
&gt;&gt; Thanks for your reply. I was wondering ...<br>
&gt;&gt;<br>
&gt;&gt; &gt; If you listen to events then you should be able to reliably receive a<br>
&gt;&gt; &gt; hangup<br>
&gt;&gt; &gt; event containing the variables if you are still connected at that time.<br>
&gt;&gt;<br>
&gt;&gt; ... how I listen for these events. Is it just a case of polling the<br>
&gt;&gt; socket for them at frequent enough intervals?<br>
&gt;&gt;<br>
&gt;&gt; &gt; If you want a 100% reliable method use CDRs.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; mod_xml_cdr can submit call CDRs to a webserver just after call hangup.<br>
&gt;&gt; &gt; The<br>
&gt;&gt; &gt; XML is verbose and will contain a full call history and all channel<br>
&gt;&gt; &gt; variables. Because it&#39;s submitted ASAP you get the results in real time<br>
&gt;&gt; &gt; and<br>
&gt;&gt; &gt; reliable. If the module can&#39;t submit the CDRs it writes them to an error<br>
&gt;&gt; &gt; folder on disk where you can resubmit them.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Real time seems useful for your use-case, as your script would just need<br>
&gt;&gt; &gt; to<br>
&gt;&gt; &gt; check your DB whether the CDR had been submitted yet. The channel UUID<br>
&gt;&gt; &gt; can<br>
&gt;&gt; &gt; either be captured from events or specified in advance in the originate<br>
&gt;&gt; &gt; (originate_uuid). There&#39;s a uuid api call to generate them for you. I&#39;d<br>
&gt;&gt; &gt; suggest looking at this method.<br>
&gt;&gt;<br>
&gt;&gt; This is all great info, thanks :-) I&#39;m capturing the uuid from the<br>
&gt;&gt; response from the originate command, that&#39;s no problem.<br>
&gt;&gt;<br>
&gt;&gt; The box I&#39;m doing this on is being a B2BUA for us at the moment. The<br>
&gt;&gt; CDRs are already being HTTP POSTed to a URL, I can&#39;t change that as<br>
&gt;&gt; our billing depends on it. I can see the channel variable coming<br>
&gt;&gt; though in the POST content, so maybe I can frob the CDR script to give<br>
&gt;&gt; me what I want.<br>
&gt;&gt;<br>
&gt;&gt; Thanks again for the info.<br>
&gt;&gt;<br>
&gt;&gt; Cheers,<br>
&gt;&gt; Rich<br>
&gt;&gt;<br>
&gt;&gt; --<br>
&gt;&gt; Once our basic material needs are met - in my utopia, anyway - life<br>
&gt;&gt; becomes a perpetual celebration in which everyone has a talent to<br>
&gt;&gt; contribute. But we cannot levitate ourselves into that blessed<br>
&gt;&gt; condition by wishing it. We need to brace ourselves for a struggle<br>
&gt;&gt; against terrifying obstacles, both of our own making and imposed by<br>
&gt;&gt; the natural world. And the first step is to recover from the delusion<br>
&gt;&gt; that is positive thinking.<br>
&gt;&gt;        -- Barbara Ehrenreich<br>
&gt;&gt;<br>
&gt;&gt; _________________________________________________________________________<br>
&gt;&gt; Professional FreeSWITCH Consulting Services:<br>
&gt;&gt; <a href="mailto:consulting@freeswitch.org">consulting@freeswitch.org</a><br>
&gt;&gt; <a href="http://www.freeswitchsolutions.com" target="_blank">http://www.freeswitchsolutions.com</a><br>
&gt;&gt;<br>
&gt;&gt; FreeSWITCH-powered IP PBX: The CudaTel Communication Server<br>
&gt;&gt; <a href="http://www.cudatel.com" target="_blank">http://www.cudatel.com</a><br>
&gt;&gt;<br>
&gt;&gt; Official FreeSWITCH Sites<br>
&gt;&gt; <a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a><br>
&gt;&gt; <a href="http://wiki.freeswitch.org" target="_blank">http://wiki.freeswitch.org</a><br>
&gt;&gt; <a href="http://www.cluecon.com" target="_blank">http://www.cluecon.com</a><br>
&gt;&gt;<br>
&gt;&gt; FreeSWITCH-users mailing list<br>
&gt;&gt; <a href="mailto:FreeSWITCH-users@lists.freeswitch.org">FreeSWITCH-users@lists.freeswitch.org</a><br>
&gt;&gt; <a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a><br>
&gt;&gt; UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/options/freeswitch-users</a><br>
&gt;&gt; <a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; _________________________________________________________________________<br>
&gt; Professional FreeSWITCH Consulting Services:<br>
&gt; <a href="mailto:consulting@freeswitch.org">consulting@freeswitch.org</a><br>
&gt; <a href="http://www.freeswitchsolutions.com" target="_blank">http://www.freeswitchsolutions.com</a><br>
&gt;<br>
&gt; FreeSWITCH-powered IP PBX: The CudaTel Communication Server<br>
&gt; <a href="http://www.cudatel.com" target="_blank">http://www.cudatel.com</a><br>
&gt;<br>
&gt; Official FreeSWITCH Sites<br>
&gt; <a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a><br>
&gt; <a href="http://wiki.freeswitch.org" target="_blank">http://wiki.freeswitch.org</a><br>
&gt; <a href="http://www.cluecon.com" target="_blank">http://www.cluecon.com</a><br>
&gt;<br>
&gt; FreeSWITCH-users mailing list<br>
&gt; <a href="mailto:FreeSWITCH-users@lists.freeswitch.org">FreeSWITCH-users@lists.freeswitch.org</a><br>
&gt; <a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a><br>
&gt; UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/options/freeswitch-users</a><br>
&gt; <a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a><br>
&gt;<br>
<br>
<br>
<br>
--<br>
Once our basic material needs are met - in my utopia, anyway - life<br>
becomes a perpetual celebration in which everyone has a talent to<br>
contribute. But we cannot levitate ourselves into that blessed<br>
condition by wishing it. We need to brace ourselves for a struggle<br>
against terrifying obstacles, both of our own making and imposed by<br>
the natural world. And the first step is to recover from the delusion<br>
that is positive thinking.<br>
       -- Barbara Ehrenreich<br>
<br>
_________________________________________________________________________<br>
Professional FreeSWITCH Consulting Services:<br>
<a href="mailto:consulting@freeswitch.org">consulting@freeswitch.org</a><br>
<a href="http://www.freeswitchsolutions.com" target="_blank">http://www.freeswitchsolutions.com</a><br>
<br>
FreeSWITCH-powered IP PBX: The CudaTel Communication Server<br>
<a href="http://www.cudatel.com" target="_blank">http://www.cudatel.com</a><br>
<br>
Official FreeSWITCH Sites<br>
<a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a><br>
<a href="http://wiki.freeswitch.org" target="_blank">http://wiki.freeswitch.org</a><br>
<a href="http://www.cluecon.com" target="_blank">http://www.cluecon.com</a><br>
<br>
FreeSWITCH-users mailing list<br>
<a href="mailto:FreeSWITCH-users@lists.freeswitch.org">FreeSWITCH-users@lists.freeswitch.org</a><br>
<a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a><br>
UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/options/freeswitch-users</a><br>
<a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a><br>
</div></div></blockquote></div><br></div>