I'm trying to build a little command-line IRC client in PHP because I'm getting sick of all those clients having you click through twenty GUI popups/windows to connect to a new server.
Everything is working so far, but I'm stuck with the main loop that sends my input commands/messages to the server and receives the new data from it.
As PHP isn't very multi-task-friendly I have two autonomous PHP scripts running at the same time:
The input reader where I can enter my messages - it stores the current message in a text file.
The server listener/writer which receives new data and reads and clears the text file where the input reader stored my current command in.
However fgets() which I use to read new data from the server pauses the script waiting until something new arrives.
So the input text file can't be read out until something new arrives from the server, which is not good.
Is there some special function/option to help me out?
You need to look at streams, and especially stream_set_blocking.
EDIT: in fact, you can get rid of having two processes and do everything in one process. Use non-blocking reads and you should be fine.
Related
Like a Log-file is written by a php-script via fwrite($fp, ---HTML---),
I need to save an HTML DIV as png-file on the server.
The client-browser only start the php-script,
but without any client-interaction the png-file should be saved on the server.
Is there a way to do this?
All posts (over thousands) I have been reading are about html2canvas,
which is (as I understand) client-side operating.
I know the html-(html-div)-rendering normally does the browser.[=client-side]
But is there a way to do it in PHP on server-side ?
Reason:
Until now the procedure is
print the div via browser on paper twice
one for the costumer,
one to scan it in again to save it on the server as picture and throw it in the paperbasket.
By more than 500 times a day ...
By security reasons it need to be a saved picture on the server.
Need the code in PHP for a similar implementation that you can find below link in Python.
One of the 3rd Party server sends the data in SSE (Server Sent Events), that I need to be captured and save to my database in PHP.
I tried with HTML5 to read the stream and with AJAX call the data can be pushed to a PHP file in turn to DB. But the drawback is we have to keep browser on always.
I tried with fopen the streaming URL and fread to fetch data. But the looping and waiting for event data is where I struck. Need help in PHP code to read the data, so that I can set a cron job of the PHP code to push to DB as and when data receives.
Here is a Python code, I need a similar code in PHP
Reading SSE data in python
I'm about to implement a REST server (in ASP.NET although I think that's irrelevant here). where what I want to do is the request is made and it returns the result. However, this result is an .XLSX file that could be a million rows.
If I'm generating a million row spreadsheet, it's going to take about 10 minutes. An http request will time out. So what's the best way to handle this delay in the result.
Second, what's the best way to return a very large file as the REST result?
Update: The most common use case is the REST server is an Azure cloud service web worker (basically IIS on Azure). The client is a PHP web app running on a different server in a different location. The PHP web app needs to send up a report template (generally 25K) and the data which can be a connection string to a SQL database, or... could be a 500M XML file. So that is the request, an XML file containing the template and datasource(s).
The response if a file - PDF, DOCX, XLSX, PPTX, or HTML. That can be a BLOB inside an XML file or it can be the file itself. In the case of an error then it must return XML with the error information. The big issue is it can take 10 minutes to generate this file if everything goes right. When it's a 1 million row spreadsheet, it takes time to pull down all that data and populate the created XLSX file. Second issue, this is then a really large file.
So even if everything is perfect, there's a big delay and a large response.
I see two options:
Write file to response stream during its generation (from client side this looks like downloading large file);
Start file generation task on server side and return task id immediatly. Add API methods, that allows retreive task status, cancel it or get results (if task completed).
interesting question,
i sure hope you have a stable connection, anyway, at the client side, in this case, php, set the timeouts to very high values. in php
set_time_limit(3600*10);
curl_setopt($curlh,CURLOPT_TIMEOUT,3600*10);
I have a setup with node and socket io to send a dymanic time (HH:MM:SS) to my index.php file that is served by apache. So far im able to see the time properly running on the php file.
Now what im trying to achive is to search dynamicly inside a text file for the text just after a time marker (that would be the time received through socket io) and print it, by example: Time received: 00:05:00, my text file look like:
<00:00:00>
Helo World
<00:05:00>
Welcome to my presentation
<00:10:00>
Now is time for a coffe
So when the time received is 00:10:00 i can display the text that follow. I will really appreciate any advice about the best approach to do this.
Regards
If you're trying to do a search through a text file, if I understand the problem correctly, you should be doing it on the server, not on the client.
It sounds like you should do the timestamp and file processing on the server, and send the content to the client after.
So your flow would be something like this:
1) Get timestamp on server
2) Find text from file (using fs module)
3) Send text to client via socket.io
To search through a file you can use the file system module that is a part of NodeJS's core.
Let me know if this helps
I have video running as a live stream and an editor can add captions at any time. Also when microphones are clicked in the broadcast room they move the camera and then update the caption.
The flash video pulls a text file every 3 seconds for the new caption.
The video takes an average of 7secs delay to get to the web users though and so I need to write some php to hold the update somewhere and then write it 7 seconds later to the text file.
I need to update away from a Java demon that needs to keep a socket open at the moment.
I have thought of trying out a queue or a cron job. Cron does not do second updates and queue seems to mean running a java demon again, and have not found a way or pausing yet.
The caption needs to stay in a text file and must not do a db call, but the api can be changed in any simple way to delay the updates.
So Jason object generated through the api (only 1-5k) held for 7seconds and then written to text file.
You should look into long polling and real time updates, then do some sort of loop that pushes content when specified.
You can setup a very simple webservice with something like Pubnub or Pusher. And then do some sort of loop in the server side of your choice that pushes contents with the right intervals to make sure data is there.
Please be aware that the design you mention has race conditions all over, so take that in consideration when building it.