![]() |
PHP advice on loops
I need to create a loop that do something.
If that something fails to happen, I need the loop to go back and do it again until it happens... The basic: top: Get a URL that may timeout; (if URL isn't grabbed) -> goto top; My current understanding of loops in PHP can't figure out this kind of loop. Where it will keep going back into infinity when the if match isn't being met. Anyone? |
lookup 'while' on php.net :) It's really a great resource, and you'll thank yourself when you learn it properly.
|
do you mean something like this?
do { $fp = fopen($url,'r'); $fout = ''; while ($s=fgets($fp,1000)) { $fout .= $s } fclose($fp); } while (empty($fout)); |
I know very little PHP, but from what I understand this is the correct structure:
PHP Code:
|
Ok, I was no help at all haha
|
I am using cURL wrapped inside a function to get data from a URL... If it times out, I need it to ask for the data again. and again, until the function returns the data I want.
|
Argf, hate when you gets tuck with php. I'm stuck too right now with an AJAX Live search script. :(
Gets results fine but i can't research without refreshing the page... |
Quote:
|
Haha, yes!!! I solved my problem :) - well, someone else did :1orglaugh
function handleHttpResponse() { if (http.readyState hahahaha 4 && http.status hahahaha 200) { document.getElementById('results').innerHTML = http.responseText; done = false; } } |
Quote:
do { grab your url with curl } while (url hasnt been gotten); is the basic concept/syntax |
Nice Sam... now...
The thing I guess I am not grasping is that the while statement is going to have to call the funtion in question and the check to see if it worked seems to come before that... hmm... OK, got it... Fuck.. talk about hammering a server... |
why aren't you using a cron? what happens if the server is down and your script sits in a continuous loop until it eats enough memory to crash the box?
|
Quote:
|
!!! Just a thought !!!
Maybe you should cache the results locally get the time before you enter a loop, (while loop or whatever doesn't matter) loop in it making your read requests, if you don't make the read in x amount of seconds, (1 sec would be a max time for me) read the cached local content. It is very hard on your server to hang up apache on these long processes, I'd only read remotely if i had to. If it is a news feed or something you can get away normally with only having to read this in once or twice a day. The rest of the time reading from the local cached file you created. In addition if you are pharsing the information you are reading in. Cache it in the form of already being pharse as pharsing is an expensive process as well. If this site gets any type of traffic, a code written in such a way to keep trying if even for only 3 - 5 seconds could bring the server to its knees. So you need to think of other ways to achive your end result. With apache you only have so many processes you can use, generally around 250 apache processes on a server. After this, or close to this number you'll stop serving pages, aka loose your ass on your money. Long connection times can really cause this number to go much higher than needed and even spiral out of control, in general it's a really bad idea. As mentioned by others if it keeps trying and you have no memory limit set and no script execution time set, the script could (and in my opinion at some time will) crash your server. In addition about 'sleeping' in your php scripts this produces the above results, which are very harmful to your server. There is no reason to sleep in your php. I don't know what problem you have, but you need to take a different look at it as what i see written here will not scale at all, and will greatly harm your server. |
Quote:
:1orglaugh :1orglaugh :1orglaugh |
Quote:
|
If this is a mission-critical script, one piece of advice: don't do it yourself. Stuff like this, when done wrong, is exactly what causes huge server load, infinite loops, dead pages, etc. It seems to me that you aren't very experienced in PHP yet, so if you aren't just doing this as some exercise but as something essential to one of your sites, getting a real programmer is probably a good idea.
|
Quote:
:2 cents: This server in question BTW will not be dead on the amount of traffic it is getting... |
Quote:
You said: Quote:
|
Quote:
|
All times are GMT -7. The time now is 07:15 AM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123