execute .php file from php file

Discussion in 'PHP' started by kalaid, Jan 28, 2008.

  1. #1
    Hi Guy's

    I am working with the concept of threading, Here I want to execute a same e php file more than one time simultaneously. Is there is any command to execute php file

    Thanks in advance
    Edit/Delete Message
     
    kalaid, Jan 28, 2008 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #2
    You can use shell functions to execute php.
    
    exec("php-cgi /path/to/script.php  >/dev/null 2>&1");
    exec("php /path/to/script.php  >/dev/null 2>&1");
    
    PHP:
    Or you can use wget and run it in background mode to run simultaneous script.
    
    exec("wget -bq -o /dev/null http://www.example.com/script1.php");
    exec("wget -bq -o /dev/null http://www.example.com/script2.php");
    
    PHP:
    Or you can use cURL to execute simultaneously.
    
    <?php
    // create both cURL resources
    $ch1 = curl_init();
    $ch2 = curl_init();
    
    // set URL and other appropriate options
    curl_setopt($ch1, CURLOPT_URL, "http://www.example.com/script1.php");
    curl_setopt($ch1, CURLOPT_HEADER, 0);
    curl_setopt($ch2, CURLOPT_URL, "http://www.example.com/script2.php");
    curl_setopt($ch2, CURLOPT_HEADER, 0);
    
    //create the multiple cURL handle
    $mh = curl_multi_init();
    
    //add the two handles
    curl_multi_add_handle($mh,$ch1);
    curl_multi_add_handle($mh,$ch2);
    
    $running=null;
    //execute the handles
    do {
        curl_multi_exec($mh,$running);
    } while ($running > 0);
    
    //close the handles
    curl_multi_remove_handle($ch1);
    curl_multi_remove_handle($ch2);
    curl_multi_close($mh);
    
    ?>
    
    PHP:
     
    Kaizoku, Jan 28, 2008 IP
  3. kalaid

    kalaid Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Kaizoku

    Thank's for your post
    I tried using
    exec("wget -bq -o /dev/null http://www.example.com/script1.php");
    function, along with the file name i passed arguments, but the file is not executed. Please help me to solve this
     
    kalaid, Jan 28, 2008 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    Okay it looks like you are on shared host or restricted from having extra processes.
    Try this:
    
    exec("wget -q http://www.example.com/script1.php >/dev/null 2>&1");
    
    PHP:
     
    Kaizoku, Jan 28, 2008 IP
  5. kalaid

    kalaid Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi Kaizoku

     
    kalaid, Jan 28, 2008 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    That is strange, the code I gave worked on my server. What OS/Distro are you using? Are you using dedicated or shared hosting?
    Also turn error_reporting to E_ALL so you will see errors and debug.
     
    Kaizoku, Jan 28, 2008 IP
  7. kalaid

    kalaid Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi Kaizoku

    I am using windows xp os and the file which i executed kept in my local machine
     
    kalaid, Jan 28, 2008 IP
  8. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #8
    Do you have wget installed on your local box?
     
    Kaizoku, Jan 28, 2008 IP
  9. kalaid

    kalaid Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I don't know whether wget is installed in my local machine. If I want to install what I have to do
     
    kalaid, Jan 28, 2008 IP
  10. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #10
    shallowink, Jan 28, 2008 IP
  11. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #11
    Because windows can't execute by just calling wget. It doesn't have the feature like /usr/bin in linux I think.
    You have to execute wget using the absolute path.
    
    exec("C:/path/to/wget.exe -q http://www.example.com/script1.php");
    
    PHP:
     
    Kaizoku, Jan 28, 2008 IP
  12. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #12
    I don't know anything about that, could test it though. If i wanted multi process headaches, I'd just use perl.
    Thought if he used wget it would have to be somewhere in the webserver path.
     
    shallowink, Jan 28, 2008 IP
  13. kalaid

    kalaid Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Hi Kaizoku
    I have uploaded my files into server and tried to execute with both exec() commands
    The file crawl.php is not executed, but when i execute the file crawl.php separately then it is executed.
    Our's is LINUX Shared hosting
    Please help me solve this issue
     
    kalaid, Jan 29, 2008 IP
  14. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #14
    Dude, you just said you were using windows, now it's linux...
     
    Kaizoku, Jan 29, 2008 IP
  15. kalaid

    kalaid Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    In am writing coding for my project in my windows machine and after completing i want to upload into our site, which is linux shared hosting site.
    Now it is working in windows with this function
    But when i upload and call the file with this either of the function
     
    kalaid, Jan 29, 2008 IP
  16. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #16
    exec("whereis wget", $output);
    print_r($output);

    Report back the results.
     
    Kaizoku, Jan 29, 2008 IP
  17. kalaid

    kalaid Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Hi Kaizoku
    I got the result as
    Array ( [0] => wget: /usr/bin/wget /usr/share/man/man1/wget.1.gz )

    Whether i have to provide this path in exec()
     
    kalaid, Jan 29, 2008 IP
  18. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #18
    I think your shared hosts restricts you to extra processes.
    So try this
    exec("wget -q http://mycollege.in/crawl.php");
    exec("/usr/bin/wget -q http://mycollege.in/crawl.php");

    If all fails, the admin probably restricts the usage of wget for normal users.
     
    Kaizoku, Jan 29, 2008 IP
  19. kalaid

    kalaid Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Hi
    Also I want to pass the arguments along with the file inside exec() function.
    I tried like this in windows
    exec("C:/path/wget.exe -q http://localhost/sitemapprototype/crawl.php?thread=$tnames[0]&url=$url[0]");
    The arguments are not passed
     
    kalaid, Jan 29, 2008 IP
  20. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #20
    You need to wrap it around apostrophes. Arrays need to be wrapped around curly braces as well.
    
    exec("C:/path/wget.exe -q 'http://localhost/sitemapprototype/crawl.php?thread={$tnames[0]}&url={$url[0]}'");
    
    PHP:
     
    Kaizoku, Jan 29, 2008 IP