510 Invalid or unknown command while execute the agi script

Hi to all,
I am new to AGI World. I tried a very simple and basic php script to run at agi but each time I am getting error code of 510 invalid or unknown command.
I am not sure What I am missing or what can be the reason…

I am calling like

[color=brown]5,1,AGI(myscript.php)
5,n,Goto(another context)
[/color]
and the script is as follow

[color=green]#!/usr/bin/php -q

<?php ob_implicit_flush(true); set_time_limit(6); $in = fopen("php://stdin","r"); $stdlog = fopen("/var/log/asterisk/my_agi.log", "w"); // toggle debugging output (more verbose) $debug = false; // Do function definitions before we start the main loop function read() { global $in, $debug, $stdlog; $input = str_replace("\n", "", fgets($in, 4096)); if ($debug) fputs($stdlog, "read: $input\n"); return $input; } function errlog($line) { global $err; echo "VERBOSE \"$line\"\n"; } function write($line) { global $debug, $stdlog; if ($debug) fputs($stdlog, "write: $line\n"); echo $line."\n"; } // parse agi headers into array while ($env=read()) { $s = split(": ",$env); $agi[str_replace("agi_","",$s[0])] = trim($s[1]); if $env == "") { break; } } // main program echo "VERBOSE \"Here we go!\" 2\n"; read(); errlog("Call from ".$agi['channel']." - Calling phone"); read(); write("SAY DIGITS 22 X"); // X is the escape digit. since X is not DTMF, no exit is possible read(); write("SAY NUMBER 2233 X"); // X is the escape digit. since X is not DTMF, no exit is possible read(); // clean up file handlers etc. fclose($in); fclose($stdlog); exit; ?> [/color]

[color=green]ah, let me answer this by myself so that it becomes beneficial for others

501 error comes out when the agi script send unsupported commands or in other words

This is returned when the script issues a command
that AGI does not support[/color]