a php script that executes or visits a set of dynamic urls

Discussion in 'PHP' started by IamNed, Apr 7, 2010.

  1. #1
    IamNed, Apr 7, 2010 IP
  2. peoplesmind

    peoplesmind Active Member

    Messages:
    107
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Answer: cURL + sleep() + nested for loops
     
    peoplesmind, Apr 7, 2010 IP
  3. IamNed

    IamNed Peon

    Messages:
    2,707
    Likes Received:
    276
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I tried this:

    <?php
    $time=1; $y=0; $x=10;

    for ($i=$y;$i<=$x;$i++)
    {
    $ch = curl_init();
    $url = "http://www.mysite.com/blah?u=".$i;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_exec($ch);
    curl_close($ch);
    sleep($time);
    }

    ?>

    but getting a 500 Servlet Exception error
     
    IamNed, Apr 7, 2010 IP
  4. peoplesmind

    peoplesmind Active Member

    Messages:
    107
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #4
    lol :) You have an infinite loop...

    for the for loop:

    i is equal to y.
    as long as i is less than or equal to x:
    process the loop.

    note how you reset i to the value of y (0) everytime the loop is itterated.

    in fact it doesn't need to be that complicated at all:


    for ($i = 1; $i <= 10; $i++) {
    $ch = curl_init();
    $url = "http://www.mysite.com/blah?u=".$i;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_exec($ch);
    curl_close($ch);
    sleep(1);
    }

    will do :
    http://www.mysite.com/blah?u=1
    ...
    http://www.mysite.com/blah?u=10
     
    peoplesmind, Apr 7, 2010 IP
  5. Cloud Computing Forum

    Cloud Computing Forum Guest

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    peoplesmind has indeed suggested the best method and CURL should be the best option across different environments. This only thing I would say is if you are doing loads of URL's at once you might want to explore the multi execute function CURL provides which would allow many to run at the same time, dramaticlly reducing the runtime of the script.
     
    Cloud Computing Forum, Apr 7, 2010 IP