Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Is it possible in PHP, to send the data right now to the client, and continue the PHP processing (that might be blocking) ?
<?php
// some code
echo json_encode(array('ok' => '1')); // the client is waiting for this AJAX answer !!
// how to send the response right now before finishing this PHP file ?
// the output is REALLY finished here, so client, you can work with it
some_blocking_processing(); // this is just some server processing that would
// block the client for ~ 5 seconds
// but it produces no output useful for client
?>
I know the right way might be to use queues or other process to perform the processing.
But just as a general purpose question, is it possible to send the data to the client, in the middle of a PHP file?
Well it really depends on what some_blocking_processing() is actually doing. I can't come up with a solution without knowing what is happening there.
However I can point you to some areas where you can do more research. One of these might be the right solution for you:
PHP threading
spawning asynchronous php process
logging your state in file/db and then do the extra processing via a cron job
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
An Android application as client needs to check the changes of a variable in a JSON file placed in a PHP server
With HTTP request from the application to the server, the variable could be retrieved (sent back to the application by PHP code utilizing echo)
The variable value shall be updated in the Android application as quick as possible
In other word, it's preferable if it gets updated in the Android application as soon as there is any changed in the server
It's possible to send HTTP request every interested seconds but it does not seem to be a true manner cause the variable may not change for several days while during all these several days, HTTP request is being sent and sent
Edit:
Is HTTP request the only solution?
I think it's better to use Push Notification ( FCM Technology ) in this case.
You can be notified whenever the variable has been changed and do whatever you want in the android application while the push notif been received.
Also there is no overload on server by calling a web-service to check the variable every minutes.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Sorry in advance if what I am going to ask is silly, I am having trouble understanding the asynchronous HTTP responses. I am trying to use jQuery .ajax() with PHP and MySQL in the following context:
when page loads I will query and return all the applications for a particular id, and then I will use a while loop to output them;
also there is a button that allows the user to add new application for that particular id -> on click it:
shows a hidden form and gets the values in the inputs;
sends the data to PHP using the .ajax() method;
performs the insert.
and I am stuck at this point
I need to output the newly added application above the existing ones, but the query and while will do it only when an synchronous HTTP request is sent.
I tried to use the .success() but the HTML I need to output inside is really long and I am afraid it will be hard to maintain having outputs from both PHP while loop and jQuery.
Can you please help me understand an efficient way to do this? I have never dealt with asynchronous HTTP request before. Also, I am can't use any JavaScript templating libraries.
I would really appreciate your help!
An ajax postback typically implies some sort of JSON-based web api behind it. Examples of this on the internet abound.
https://www.lennu.net/jquery-ajax-example-with-json-response/
http://www.9lessons.info/2012/05/create-restful-services-api-in-php.html
Your REST endpoint should return the minimal amount of JSON data you need. At which point you can use that data to update/bind to elements in your DOM inside your "done" or "success" callback.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am thinking of creating a simple web based instant messaging using a combination of PHP and SQL. To keep it simple I was thinking of not sending the message to the other clients browser using COMET or AJAX, but simply uploading it to a SQL database. The other clients computer will then periodically refresh the webpage which will cause the PHP code on the server to check for and return any new messages.
Would this method be simply to slow to be actually useful?
Thanks in advance :)
That depends on the scope of your project. If you're thinking of server a thousand users, this is not a recommended method. If you want to chat with your 5 colleagues on an internal LAN: it doesn't really matter much. It will be fast and work just fine.
You could also consider building it with jQuery + PHP + SQL though; read up on jQuery a bit and you'll be amazed by the power of its AJAX functions.
Also, if you're lazy or simple don't have enough time, use a premade library like this one here and i'm sure there are many more to be found on the internet.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My C++ program communicates to my PHP server through a TCP socket.
Data is sent on the server at periodic intervals.
I want the Data sent on the server to be displayed on my HTML page.
I am new to HTTP/PHP programming. How can I achieve this.
Thanks in advance.
There are a lot of ways to do this, the most simple should be this one (if your c++ program run out of the server):
create a "upload.php" file in your server with:
<?php
$a = $_POST["data"];
//$a contains all your data
?>
send a POST message to
http://yourserver/upload.php
from your c++ program with 'data' parameter containing your data message. You can use libcurl to achieve this. Here is an example How do you make a HTTP request with C++?
c++ to PHP use Socket
PHP to HTML use HTTP
To run PHP you will need to install apache.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I would like to be able to make synchronous server requests for a game I plan on making. I've used AJAX's synchronous calls in the past sparingly because they are slow and I know that AJAX isn't cut out for this sort of task.
My reason for this is because I want it to be (as much as possible) hack-proof. For example, if a person buys an item. It will send a request to the server that they wish to buy the item and on that end it will check if they have enough of the currency and then send back if it's OK to allow them to buy it. Using this example, it'd obviously be a pain if it took several seconds each time you try to buy something.
The client side would be HTML5/JS and the server side would be PHP/SQL. What would be the best method to achieve this? Before anyone says "show me your code": I'm not asking for help on fixing something broken. I'm asking for a suggestion on the best way to access a server quickly and synchronously. If it isn't possible faster, then such an answer would suffice.
I'd start by building the most basic approach: simple PHP script with minimal dependencies that only loads what is required to validate the request, connect to database, and make the query so it can return the result.
Then test its performance. If that's insufficient, I'd start looking at websockets or other technology stacks for just the fast-access portions (maybe node.js?).
Making the request run synchronously doesn't make it faster. It just means that the browser is going to become unresponsive until the request is complete. Making it fast is a matter of writing your server-side code such that it can run quickly, and without any details we can't tell you how to do that.