6 thoughts on - click to call with php

  • If you are going to use call files don’t write them directly to


    write them in some temp directory and then move them to



  • Whenever you open a file for writing, a link is created in the containing
    folder’s directory (which says where on the disk the file is located)
    pretty much straight away — so other processes can see the file. And files
    are written to disk, not one character at a time, but in blocks whose size
    depends on the filesystem, one full block at a time. The last block may well
    be incomplete, and so contain junk after the file proper; but the directory
    entry gives the actual file size, so the junk can be ignored.#

    This creates a race condition: Asterisk may try to parse a call file which is
    still in an incomplete state (empty or just the first block of several),
    and get its knickers in a twist.

    The *proper* way to avoid this situation, is to create the call file in a
    temporary location first; then `mv` it to the /var/spool/asterisk/outgoing/
    folder. Moving a file within a filesystem just entails putting a new link in
    the destination folder’s directory, and removing the one from the old
    directory. Moving a file across filesystems entails a copy operation; but
    either way, the important thing is that *the link to the destination file
    won’t be placed in the folder’s directory until the data is actually there*.

    The *bodgy* way to avoid this situation, is to make sure the file is smaller
    than one logical block on the filesystem where …/outgoing/ resides; turn
    off buffer autoflushing in the scripting language; and cross your fingers
    that the file will already be complete in the cache when the directory is
    updated. And even if it works on your system today, you might find that an
    upgrade to Asterisk, your scripting language, whatever invoked the script,
    the filesystem driver in the kernel, or even a change in RAM or disk usage on
    your server, breaks it tomorrow.

  • This might help you a bit, too:

    < ?php
    $spool = “/var/spool/asterisk/outgoing/”; # outgoing callfile folder

    $filename = “asterisk-” . date(“U”) . “-” . $_SERVER[“REMOTE_PORT”] . “.call”;
    # this should end up being fairly unique. (if logging to a database with an
    # auto increment field, you can also use mysql_insert_id() as a unique
    # reference)

    $src = … ; # source extension
    # you can determine this based on $_SERVER[“REMOTE_ADDR”], which is the
    # IP address of the requesting client, by looking up in an array or a database
    $ctxt = … ; # context
    $dest = … ; # destination number
    # you probably want to get this from $_REQUEST

    $callfile = “Channel: SIP/$srcnContext: $ctxtnExtension: $destnPriority:
    1nCallerId: $srcn”; # line break added by email, not used in real life!
    if ($fh = fopen(“/tmp/$filename”, “w”)) {
    fwrite($fh, $callfile);
    system(“mv /tmp/$filename $spool”);
    else {
    die(“Call file creation failed”);

  • thanks a lot for your advice i really appreciate it 🙂

    2011/5/20 Mark Deneen