[Freeswitch-users] Outbound socket PHP question

William Suffill william.suffill at gmail.com
Tue Aug 4 16:04:08 PDT 2009


I wrote some notes on this but have yet to wiki it.

example of an outbound socket connection where the call is answered, a
variable is set then perhaps play one of the pre-installed files and
hangup.
ivrd

fs_ivrd comes with freeswitch. It being a small daemon just invokes
the script defined in a variable and passes data from it via STDIN/OUT

Since this is an outbound socket connections it needs to be defined in
the dialplan.



Ex:

<extension name="outbound-socket">

    <condition field="destination_number" expression="^55(522)$">

        <action application="set"
data="ivr_path=/usr/local/freeswitch/scripts/ivrd-demo.php"/>

        <action application="socket" data="127.0.0.1:8084 async full"/>

    </condition>

  </extension>



The above dialplan sample would invoke ivr-demo.php when 55522 is
called as long as fs_ivrd is running. To start fs_ivrd:

/usr/local/freeswitch/bin/fs_ivrd -h 127.0.0.1 -p 8004



It takes 2 arguments -h for hostname and -p for port.



PHP Code

#!/usr/bin/php -q

<?php



    // set a couple of things so we dont kill the system

    ob_implicit_flush(true);

    set_time_limit(30);





    // Open stdin so we can read the AGI data in

    $in = fopen("php://stdin", "r");

    // Connect

    echo "connect\n\n";

    // Answer

    echo "sendmsg\n";

    echo "call-command: execute\n";

    echo "execute-app-name: answer\n\n";



    // Play a prompt

    echo "sendmsg\n";

    echo "call-command: execute\n";

    echo "execute-app-name: playback\n";

    echo "execute-app-arg:
/usr/local/freeswitch/sounds/en/us/callie/ivr/8000/ivr-welcome_to_freeswitch.wav\n\n";



    // Wait

    sleep(5);



   // Hangup

   echo "sendmsg\n";

   echo "call-command: hangup\n\n";



fclose($in);



?>



ivrd will call this script for each call. All itdoes is answer the
channel tell FreeSWITCH to play the “welcome to freeswitch” prompt.
Since the script is now controlling all call flow I needed to add a
wait or it would send the hangup immediately before the prompt was
played.

Some improvements possible but that's 1 way to do it. It would be
possible to do the socket directly in PHP but fs_ivrd is a nice option
too.

-- W




More information about the FreeSWITCH-users mailing list