English 中文(简体)
PHP sockets, permanent connection to server to receive Events from Asterisk AMI?
原标题:

I m looking to write a PHP script to act as a mini "daemon" to receive data from a remote socket. The remote server is an Asterisk VoIP server and I ll be connecting to the Asterisk Management Interface (AMI) in an attempt to receive AMI Event notifications. The connection will be through an always-on SSH tunnel (using autossh) which has been stable enough for our use so far.

Here s the plan...

  • A PHP script connecting to the local port of the SSH tunnel which forwards to the remote port at the other end using fsockopen() or most likely pfsockopen()
  • The PHP script will be run from CLI and I guess I should have some sort of shell script on a cron job to check that the PHP script hasn t stopped for any reason
  • I ll need this PHP script to be running permanently, and permanently connected to the socket to receive data whenever it s published by the other end
  • Memory and CPU is not a problem as we have plenty of resources on our intranet server (criminally under used) but equally I don t want this script spiralling out of control
  • The PHP script will hopefully react to occasional data appearing at the other end of the socket and sometimes inserting or updating data in a MySQL database. Obviously I ll open/close the MySQL connection when necessary, not just leave it hanging.

First of all, is this a terrible idea that will never work?

I realise PHP s probably not the best language for a sort of small daemon like this but I ve had success with PHP on CLI before and it s the language I m most comfortable with these days.

Are there any PHP functions that can spring into action when data is published at the other end of the socket?
Or would I just loop using fread() like this...

while (!feof($socket)) {
$output .= fread($socket, 8192);
}

The loop option seems a bit of a mess so I m just wondering if there s another way that will mean the script stays connected to the socket but basically idle until some data appears.

What cons/pitfalls should I be aware of when thinking about having a permanently running PHP script connected to a socket?

Cheers, B

最佳回答
  • you should be aware of memory consumption when writing a PHP daemon. If not carefully unset()/free()d, your daemon may eat more and more memory over time.

  • as you already said, PHP is not the best language for this, but it will definitely work. i already did that "hack" a few times.

  • be aware that fread() on a TCP socket will be non-blocking. As each line is CR/LF terminated ( ) and two CR/LF mark the end of an AMI event/command, you may want to read until " " occurs, then process this event and then begin reading again. i d set the socket blocking and use fgets() instead of fread(). that way its much easier to detect the end of an AMI event - without needing to do string magic / splitting.

问题回答

Have a look at ZeroMQ, asynchronous message library. http://en.wikipedia.org/wiki/ZeroMQ

Here s the link to the php bindings:

http://www.zeromq.org/bindings:php





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签