CrimpJiggler Posted November 29, 2013 Share Posted November 29, 2013 I made a PHP spider which downloads a series of wiki pages from a list, I put a 2 second delay in there so as not to cause excessive traffic and as a result, it takes a long time to load. I want to make the script tell you its made every time it saves a file, i.e. File 1 has been saved, file 2 has been saved etc. How can I make the PHP script make an AJAX request each time it iterates through a loop? Quote Link to comment https://forums.phpfreaks.com/topic/284381-how-to-make-ajax-requests-while-a-php-script-is-running/ Share on other sites More sharing options...
codefossa Posted November 30, 2013 Share Posted November 30, 2013 You could save a message to a file and just have JS call a PHP page loading the contents of that file. Another option is to just run the script in a terminal, where echoing out a string will show right in front of you live. Quote Link to comment https://forums.phpfreaks.com/topic/284381-how-to-make-ajax-requests-while-a-php-script-is-running/#findComment-1460685 Share on other sites More sharing options...
trq Posted November 30, 2013 Share Posted November 30, 2013 Take a look at the Symfony "Process" component. Quote Link to comment https://forums.phpfreaks.com/topic/284381-how-to-make-ajax-requests-while-a-php-script-is-running/#findComment-1460700 Share on other sites More sharing options...
CrimpJiggler Posted December 1, 2013 Author Share Posted December 1, 2013 You could save a message to a file and just have JS call a PHP page loading the contents of that file. Another option is to just run the script in a terminal, where echoing out a string will show right in front of you live. I tried your first solution, heres the PHP script: echo '<div id="status_update"> STATUS UPDATE SHOULD APPEAR HERE </div>'; for ($i=1; $i < 10; $i++) { echo "Number $i<br>"; $fh = fopen('include/status_update.txt','w'); fwrite($fh,"Line $i"); fclose($fh); sleep('3'); } and heres the javascript: function status_update() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("status_update").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","include/status_update.txt",true); xmlhttp.send(); } setInterval(function(){ status_update(); },1000); but it doesn't work, the AJAX doesn't seem to load while the PHP script is loading. In the PHP script, I put in sleep('3') which makes the script wait 3 seconds for each iteration of the loop, that would be plenty of time for the javascript to make these AJAX requests. Quote Link to comment https://forums.phpfreaks.com/topic/284381-how-to-make-ajax-requests-while-a-php-script-is-running/#findComment-1460865 Share on other sites More sharing options...
CrimpJiggler Posted December 1, 2013 Author Share Posted December 1, 2013 Wait a minute, it doesn't work on chrome but it works on firefox. If I can figure out how to get it to work on all browsers, then it would be a very elegant solution compared to the ones I've read about, all of which require the PHP script to be initiated by AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/284381-how-to-make-ajax-requests-while-a-php-script-is-running/#findComment-1460871 Share on other sites More sharing options...
kicken Posted December 1, 2013 Share Posted December 1, 2013 You have to break this up into separate scripts. One that will do the downloading and one that will do the status updates. Quote Link to comment https://forums.phpfreaks.com/topic/284381-how-to-make-ajax-requests-while-a-php-script-is-running/#findComment-1460873 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.