Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6748973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:37:59+00:00 2026-05-26T12:37:59+00:00

I’m looking to write a PHP script to act as a mini daemon to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:37:59+00:00Added an answer on May 26, 2026 at 12:37 pm
    • 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 (\r\n) and two CR/LF mark the end of an AMI event/command, you may want to read until “\r\n\r\n” 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.