Stefan
Stefan

Reputation: 2194

PHP based server using PHP CLI SAPI

Is it possible (and practicable) to develop a (simple) server software using the PHP CLI SAPI? Is there some usable way of (real) multi-threading in PHP so the server is able to handle several request simultaneously? Or would you recommend another Scripting language for a project like this (Python or something like that)?

Upvotes: 2

Views: 977

Answers (2)

hek2mgl
hek2mgl

Reputation: 158150

Yes it is possible to develop a TCP or UDP server in PHP. Have a look at the set of socket functions in PHP: PHP:Sockets - Manual

Although PHP has no multithreading capabilities you can create simple parallelism using non blocking IO (seesocket_set_nonblock()) along with socket_select()

To answer the question 'Is it practible' it needs more information about the project requirements. Reasons for a 'yes' could be:

  • You need to integrate the server into an existing PHP class, model framework
  • You plan to interexchange PHP data structures using the server. (Might be generated using serialize(), unserialize())

You may find additional reasons for you of course.

Another thing you should note is that when in web server environment (e.g apache SAPI) you already have a parallel TCP server - the web server. You would just have to implement the communication between several requests. You may use PHP's IPC capabilities, a database or at least a file for that communication.

Upvotes: 1

Flavius
Flavius

Reputation: 13816

Is it possible (and practicable) to develop a (simple) server software using the PHP CLI SAPI?

There's already one built-in: http://php.net/manual/en/features.commandline.webserver.php

Is there some usable way of (real) multi-threading in PHP so the server is able to handle several request simultaneously?

You don't usually do that in PHP, but rather hand it off to a sepparated service (which you can also write in PHP), like http://php.net/manual/en/book.gearman.php

Or would you recomend another Scripting language for a project like this (Phyton or something like that) ?

This one depends on your needs and cannot be answered without more information. Python, Java or Golang may well be better alternatives

Note: PHP is a multithreaded language, but it doesn't expose multithreading capabilities to the scripts' runtime.

Upvotes: 0

Related Questions