I have a PHP script that loads a function which uses Curl to log itself in to another webpage to get some data. This operation takes about 14 seconds altogether and some users might become impatient. I have a little busy loader to indicate activity.
I have seen this on other websites and want to know what technology I need to implement the following:
How can I send little notification messages back to the main site while the PHP function is running so the user knows about the progress.
Messages could be in the following form:
Logging in to website
Extracting data
Sorting data
Closing connection
You can do it using ajax, do the following.
1) Keep your curl code in the seperate php file.
2) While click the button, using js or jquery call that php file using ajax.
3) Before ajax return the reponse, you can display the div with your message, once you
got response, you can hide the div.
Related
We are making a app with php in which we want to push data to client browser same in the way in which facebook notificaiton sent by facebook. i know about commet and also have used ajax in past but ajax is not efficeint while commet programing is out of my mind.
Sample can seen at ESPNCRICINFO.COM live scorecard which is automatically pushed
by server to brwoser and than append to document without refreshing. Same app we
have to made.
Simply i need to build some code that send data to all the browser which have our webpage opened. No restrictions. Just need to send to all. So there is no need to check to whom data will be push.
I really need it urgently.
can you use JavaScript setTimeout(function name, seconds) ?
In JavaScript function you can generate .post request for some PHP script which can load messages, when the message will load, you can show it via you JavaScript. the timer will call this function for all people.
I have two separate processes and I am wondering how I can combine them.
I have a PHP email sign-up form which posts the users email address and other data into an SQL DB.
I have a sync PHP script API which once run adds all info from the SQL DB to a 3rd party site (mailchimp)
How can I combine them so once a new user adds their details, and the form is submitted the PHP API script runs. I did this, however it runs the script and the user has to wait until the API call is done:
$appUrl = $_SERVER['HTTP_HOST'];
$path = 'newsletter/mailchimp.php';//your path here
$appUrl = 'http://'.$appUrl.'/'.$path;
if (count($_POST)>0) echo file_get_contents($appUrl);?>
Find an HTML-element which you can bind your function to, like your form, button, checkbox or whatever. There's many different ways like $("form").submit(function(){});, .click, .changed. After that you can use the $.post() function to call the PHP-files.
$(document).ready(function(){
$("#buttonId").click(function(){
$.post('phpfile1.php');
$.post('phpfile2.php');
});
});
If you're getting the content from a remote site, then you can't send it to the client before you've got it.
But in your example you are calling a URL on the local machine - if you include the file instead of calling it in a new HTTP request then you'll get a small improvement in speed.
If you don't need the content from the other URL, then call it from a shutdown function after your script generates the content flushes its buffers and exits.
Calling the second url via javascript is ot a good idea if the transaction spans both codesets - you're putting the user in control of the flow in your application.
I m trying to make messenger kind of service. In which I am facing a problem. I am supposed to reload the page manually. But the problem in running it automatically is that it gets reloaded for every second. I had used a javascript for reloading the page for every 1 sec. Inside which i m calling a php script. But the problem with that code is that complete page gets refreshed. Is there a way to reload only a particular part of the whole page and also i should be able run the php script in the background.
Use Ajax. Basic w3schools tutorial
Ajax is a group of interrelated web development techniques used on the client-side to create asynchronous web applications. With Ajax, web applications can send data to, and retrieve data from, a server asynchronously (in the background) without interfering with the display and behavior of the existing page. Data can be retrieved using the XMLHttpRequest object. Despite the name, the use of XML is not required (JSON is often used instead), and the requests do not need to be asynchronous.
[Wikipedia]
I have a signup form that calls a PHP script which can interact with our CRM's API, like so:
CRM API <--> PHP script <--> Signup form
The signup form passes some information to the PHP script in one
AJAX call
The PHP script run a dozen API calls to the CRM to create
an account and attach various data
The CRM returns the new account id it just created to the PHP script
The PHP script passes the account id back to the signup form, at which point the AJAX call is complete and the signup form can continue.
The problem is #2, those dozen calls take about 20 seconds to complete, but the data the signup form needs is generated after the first API call so it could in theory return that data much sooner and do the rest of the stuff server side without holding that AJAX call open the whole time.
I tried flush() and ob_flush() which does output account id to the client before processing is complete, but the jQuery AJAX connection remains open so I'm still stuck waiting for the connection to be closed on the signup form side before anything happens.
So what's the easiest route for returning that account id to the form as fast as possible?
Maybe break out using curl and exec?
if(signing up){
stuff
exec(curl myself, notsignup)
}
else {
bunch of api calls
}
You should probably think about creating a seperate process for the rest of the steps that are needed. One way is that you could after the #1 first api calls has been completed. It responds back to the user, and doesn't try to complete the rest of the 20 calls on the user side.
Then create a queue that will finish the rest. You could always create a table in mysql to store the queue.
Next just create a cronjob that will run in the background, knocking the queue out.
Note: You will not want this cronjob to just start and never stop. Maybe have it run every 5 minutes, but before it starts to run, check to see if another cron is still in progress. If it is then it will check in another 5 minutes to see if it is ok to run.
Hope this helps!
If you only need the information from the first API call to return the form, then I would probably try a different workflow:
Form calls PHP Script
PHP Calls first API Call
PHP Returns to Form
Form processes response
Form calls second PHP Script to complete the process
PHP finishes API Calls (the form can abandon at this point since it sounds like you don't care what happens from here on out).
The workflow requires a little more work and co-ordination for the developer, but presents the most responsive interface to the user.
Here is the scenario:
I have a page that is logging data to MYSQL. I have another page that reads that data and allows it to be viewed. When a new piece of data is logged I would like to have the first script check and see if the viewing page is open in the browser, and if so append the newest data to the end of the view. Also - could anyone point to some info giving an overview of how PHP and the browser interact? I think I have the concept of the DOM down for javascript...but as far as PHP it just appears that once the page is sent, that's it...
You're correct in that once the PHP is sent, that's it.
There is no way to send data to a PHP page once the page is loaded. There is another slightly nastier method, but the easiest way of doing this is going to be polling the page via Ajax.
So, have a script that every 20 seconds, sends a message to another PHP script that contains the timestamp of the last MySQL log you received, then get the script to return all the data that has been set by that time.
I'm unsure how new you are to JavaScript, but the easiest way of doing that is probably using JQuery's $.ajax and encoding the new MySQL records as JSON.
No this isn't possible as you describe. The viewing page will have to poll the server for changes, either by periodically reloading itself, or by javascript / AJAX.
You are right that once the page is sent by PHP it can have no further influence. In fact the PHP execution thread on the server is killed as soon as output is complete, so the thing that generated the page no longer even exists.
To expand on Dolondro's suggestion, rather than periodically polling the server for updates, you could use Server-Sent-Events (newly supported in modern browsers).
With these, you basically just send 1 ajax request to the server, and the connection is held open. Then, the server can send updates whenever it wants. When the browser receives an event, it can add the data to the screen. Even still, the connection is held open, and the server can send additional events/updates as they occur.
W3C page:
http://dev.w3.org/html5/eventsource/
Wikipedia:
http://en.wikipedia.org/wiki/Server-sent_events
More Info:
https://www.google.com/search?ix=hcb&sourceid=chrome&ie=UTF-8&q=server+sent+events