# Calling AGI from AMI

**URL:** <https://community.asterisk.org/t/calling-agi-from-ami/35286>\
**Category:** Asterisk Support\
**Created:** [May 18, 2011, 1:40pm UTC](https://community.asterisk.org/t/calling-agi-from-ami/35286 "2011-05-18T13:40:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sadzas](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@sadzas](https://community.asterisk.org/u/sadzas)\
**Post date:** [May 18, 2011, 1:40pm UTC](https://community.asterisk.org/t/calling-agi-from-ami/35286/1 "2011-05-18T13:40:17Z")

</div>

Hi!

I need to do a kind of auto-Dialer. The thing is that calls will be originate from nowhere, so I can’t use AGI for that. I must use AMI for create a connection to Asterisk first.

So, I’m using PHP for this but once I connect to my Asterisk, I don’t know how to execute an AGI for make calls. Google 😄 didn’t help me to find an example for this.

Is it possible to call an AGI from AMI? I want to execute a file wich will have the “dial” things.  
How I do that?  
Should I execute the file from the “commands” line?

```auto
function managerLogin()
{
        $socket = fsockopen("127.0.0.1","5038", $errno, $errstr);

        fputs($socket, "Action: Login\r\n");
        fputs($socket, "UserName: test\r\n");
        fputs($socket, "Secret: test\r\n\r\n");
        fputs($socket, "Events: on\r\n\r\n");
}

while (managerLogin())
{
        fputs($socket, "Action: AGI\r\n");
        fputs($socket, "Channel: \r\n");
        fputs($socket, "Commands: \r\n\r\n");

}
```

Thanks!

---

<div class="post-metadata">

**Author:** ![david55](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@david55](https://community.asterisk.org/u/david55)\
**Post date:** [May 18, 2011, 3:15pm UTC](https://community.asterisk.org/t/calling-agi-from-ami/35286/2 "2011-05-18T15:15:48Z")

</div>

Originate

I hope this isn’t bulk marketing or debt collection.

---

<div class="post-metadata">

**Author:** ![sadzas](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@sadzas](https://community.asterisk.org/u/sadzas)\
**Post date:** [May 19, 2011, 12:15pm UTC](https://community.asterisk.org/t/calling-agi-from-ami/35286/3 "2011-05-19T12:15:12Z")

</div>

[quote=“david55”]Originate

I hope this isn’t bulk marketing or debt collection.[/quote]  
No, it’s only for internal purposes.

david, if I use Originate, I can’t figure out how to catch channel status like such as: ${ANSWEREDTIME}, ${HANGUPCAUSE}, etc.

When I use Originate, I don’t know where catch this. Should I use AGI? but where?

Should I originate the call and call and AGI? that’s my question.

thanks

---

<div class="post-metadata">

**Author:** ![david55](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@david55](https://community.asterisk.org/u/david55)\
**Post date:** [May 19, 2011, 12:17pm UTC](https://community.asterisk.org/t/calling-agi-from-ami/35286/4 "2011-05-19T12:17:28Z")

</div>

For failed party A calls, wrap the call in a local channel.

The party B side is normal dialplan.
