I figured out a method. Asterisk passes some data to the agi script, which can be seen by running
agi set debug on
in asterisk cli. If you are using the phpagi library, then
$agi->request['agi_dnid']
will hold the dialled number or the number which the caller dialled to get to our asterisk server.