[asterisk-users] Using PHP to reload extensions

Michael Iedema michael at askozia.com
Thu Oct 4 21:09:50 CDT 2007


Hello,

> >> I am trying to use PHP to reload the extensions in an Asterisk
> >> installation. I keep getting this error:

> Easiest way without compromising security or changing permissions.  Use
> the AMI.
>
> 1. Download phpagi (Just google it)
> 2. Use it to connect to the Manager interface
> 3. Use it to issue:
> Action: Command
> Command: reload

Here's the two functions I've been using. Add a new user to
manager.conf with the appropriate permissions and this should work
fine.

Hope that helps,
-Michael


function extensions_reload() {
	return asterisk_exec("dialplan reload");
}

function asterisk_exec($cmd, $output=NULL) {

	$token = md5(uniqid(rand()));
	$errno = 0;
	$errstr = 0;
	$fp = fsockopen("localhost", 5038, &$errno, &$errstr, 20);
	if (!$fp) {
	  return 1;
	}
	
	fputs($fp, "Action: login\r\n");
	fputs($fp, "Username: newusername\r\n");
	fputs($fp, "Secret: newpassword\r\n");
	fputs($fp, "Events: off\r\n\r\n");
	usleep(500);
	
	fputs($fp, "Action: COMMAND\r\n");
	fputs($fp, "command: $cmd\r\n");
	fputs($fp, "ActionID: $token\r\n\r\n");
	usleep(500);
	
	$out = fread($fp, 38000);
	while(strpos($out,"--END COMMAND--")==0) {
		$out .= fread($fp, 38000); 	
	}
	fclose ($fp);
	
	$out = substr($out, strpos($out, "ActionID"));
	$out = substr($out, strpos($out, "\n") + 1);
	$out = substr($out, 0, strpos($out, "--END COMMAND--") - 1);

	$output = $out;

	return 0;
}



More information about the asterisk-users mailing list