Channel Variables Offset When Running AGI Script

I’m having an odd problem with AGI variable in FreePBX 13 with Asterisk 13.

It seems like the same effect as if the channel variables are in an array and the index is somehow offset by 1. I’ve come across this before some time ago but can’t remember what fixed it.

Here’s AGI code:

    #!/usr/bin/php
    <?php
    require_once('fafs_include.php');
    require_once('lime_include.php');
    date_default_timezone_set('America/New_York');

    $myagi = new AGI();
    $myagi->set_music(true,"default");

    $APIUser = get_var('APIUser');
    $msg="APIUser: $APIUser";
    $myagi->verbose($msg);
    $APIPass = get_var('APIPass');
    $msg="APIPass: " . $APIPass;
    $myagi->verbose($msg);
    $SiteURL = get_var("SiteURL");
    $myagi->verbose("SiteURL: " . $SiteURL);
    $TransferPhone = get_var("TransferPhone");
    $myagi->verbose("TransferPhone: " .  $TransferPhone);
    $ProdDiscountPct = get_var("ProdDiscountPct");
    $myagi->verbose("ProdDiscountPct: " .  $ProdDiscountPct);

Here’s the CLI ouput:

<SIP/png_in-0000002e>AGI Rx << GET VARIABLE APIUser
<SIP/png_in-0000002e>AGI Tx >> 200 result=1 (Test_IVR)
<SIP/png_in-0000002e>AGI Rx << VERBOSE "APIUser: " 1
 find-orders.php: APIUser:
<SIP/png_in-0000002e>AGI Tx >> 200 result=1
<SIP/png_in-0000002e>AGI Rx << GET VARIABLE APIPass
<SIP/png_in-0000002e>AGI Tx >> 200 result=1 (test_pwd!123)
<SIP/png_in-0000002e>AGI Rx << VERBOSE "APIPass: Test_IVR" 1
 find-orders.php: APIPass: Test_IVR
<SIP/png_in-0000002e>AGI Tx >> 200 result=1
<SIP/png_in-0000002e>AGI Rx << GET VARIABLE SiteURL
<SIP/png_in-0000002e>AGI Tx >> 200 result=1 (https://devapi.ourcrm.com/admin/)
<SIP/png_in-0000002e>AGI Rx << VERBOSE "SiteURL: test_pwd!123" 1
 find-orders.php: SiteURL: test_pwd!123
<SIP/png_in-0000002e>AGI Tx >> 200 result=1
<SIP/png_in-0000002e>AGI Rx << GET VARIABLE TransferPhone
<SIP/png_in-0000002e>AGI Tx >> 200 result=1 (17277248006)
<SIP/png_in-0000002e>AGI Rx << VERBOSE "TransferPhone: https://devapi.ourcrm.com/admin/" 1
 find-orders.php: TransferPhone: https://devapi.ourcrm.com/admin/

As you can see the call to get_var() produces expected output, but the variable assignment is offset by 1. The first call returns nothing, 2nd returns first, 3rd returns second, etc. Like somehow an array assumed to start at 0 actually starts with 1. I have googled this till my fingers have calluses and dug through the PHPAGI code until my eyes are blurry and still stuck.

Any help SINCERELY appreciated.

Note: the 2 include files contain functions that are not called before this code, with the exception of get_var() which is copied from distro recordings.agi function agi_get_var().