Repeat script x number of times?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
murphyslaw
Forum Newbie
Posts: 4
Joined: Thu Aug 14, 2008 11:20 am

Repeat script x number of times?

Post by murphyslaw »

I'm new to PHP and I'm guessing there is an easy solution to this. (So easy in fact I can't figure it out .. :? )

Basically I have a bit of code I want to repeat x number of times .. for example:

Code: Select all

   <?php
 
    // create a new cURL resource
    $ch = curl_init();
 
    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
 
    // grab URL and pass it to the browser
    curl_exec($ch);
 
    // close cURL resource, and free up system resources
    curl_close($ch);
 
    ?>
So say I wanted this to repeat 10 times, one right after the other .. so as soon as the first request has been sent to my browser it immediately requests the second one and so on until it has done this 10 times .. and how would I do the same thing but this time making the script wait for x seconds until it runs again?

And finally .. can I somehow save the results to my server in a text file or similar and not have the result posted to the browser? Would this make the script run faster if the results were saved somewhere rather than waiting for the browser to load?

Apologies for all the questions on my first post.

Any help is appreciated.

Thanks!
Geteburg
Forum Commoner
Posts: 25
Joined: Tue Aug 12, 2008 1:57 pm

Re: Repeat script x number of times?

Post by Geteburg »

Code: Select all

 
for($i=0; $i < 10; $i++)
{
// everything here will be executed 10 times
}
 
As for saving and speed. No, the script itself will run at the same speed. It really has nothing to do with browser how fast script will run as PHP is executed server-side.
murphyslaw
Forum Newbie
Posts: 4
Joined: Thu Aug 14, 2008 11:20 am

Re: Repeat script x number of times?

Post by murphyslaw »

Works like a charm! Thanks Geteburg .. :)

Is there a way to save the results to a text file or similar with a time stamp for each request?
Or alternatively, have a time stamp beside each of the 10 results displayed in the browser?
Geteburg
Forum Commoner
Posts: 25
Joined: Tue Aug 12, 2008 1:57 pm

Re: Repeat script x number of times?

Post by Geteburg »

To save to file:

Code: Select all

$fp = fopen('/path/to/folder/filename.txt', 'w'))
fwrite($fp, $data); // $data should be the result aka text that you want to save
fclose($fp);
 
Set chmod 777 to folder where you want the file to be saved!

More info about "fopen" function: http://www.php.net/fopen

As for date, use:

Code: Select all

 
date("d. m. Y - H:i:s"); // will produce: 31. 12. 2008 - 24:60:60
 
More info about date and format options: http://www.php.net/date

Basically when you add things to $data add date to each line like:

Code: Select all

 
$data .= 'here comes some text' . date("d. m. Y - H:i:s"); // .= means that you will append right side to left side
 
murphyslaw
Forum Newbie
Posts: 4
Joined: Thu Aug 14, 2008 11:20 am

Re: Repeat script x number of times?

Post by murphyslaw »

Thanks again Geteburg.

So far I have:

Code: Select all

 
<?php
 
for($i=0; $i < 10; $i++)  // everything inside { } will be executed 10 times
 
{
 
// create a new cURL resource
$ch = curl_init();
 
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
 
// grab URL and pass it to the browser
curl_exec($ch);
 
// close cURL resource, and free up system resources
curl_close($ch);
 
}
 
 ?>
This works fine, but .. I have been trying to integrate Geteburg's code to save results to a text file with date and time into it without success.
I have tried .. a lot .. but can't seem to do it.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Re: Repeat script x number of times?

Post by psychotomus »

what error was giving?
murphyslaw
Forum Newbie
Posts: 4
Joined: Thu Aug 14, 2008 11:20 am

Re: Repeat script x number of times?

Post by murphyslaw »

It's more a case of not knowing where to insert the code rather than the errors as I'm totally new to PHP.
I've been doing some reading up but progress is slow.

Basically I have no idea where to insert Geteburg's code to save the results to a text file with date & time though I have tried a lot of combinations.
Geteburg
Forum Commoner
Posts: 25
Joined: Tue Aug 12, 2008 1:57 pm

Re: Repeat script x number of times?

Post by Geteburg »

Code: Select all

 
<?php
 
$result = '';
for($i=0; $i < 10; $i++)  // everything inside { } will be executed 10 times
 
{
 
// create a new cURL resource
$ch = curl_init();
 
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // This will give the result back hopefully, never used CURL, i might be wrong, read more about options here: [url]http://www.php.net/manual/en/function.curl-setopt.php[/url]
 
// grab URL and pass it to the browser
$result .= '---------DATE: ' . date("d. m. Y - H:i:s") . '-------------' . "\n"; // This will write date to $result string
$result .= curl_exec($ch); // This will append curl results to string $result
$result .= '----------THE END------------' . "\n";
 
// close cURL resource, and free up system resources
curl_close($ch);
 
}
 
$fp = fopen('/path/to/folder/filename.txt', 'w')) // Change path and filename and chmod folder to 777
fwrite($fp, $result);
fclose($fp);
 
 ?>
Post Reply