skip to Main Content

I use php for my Facebook bots that use graph API. The scripts contain get and post requests etc, but in order to run the script, I have to visit the URL in a browser, for example I use xammp for local host, so i have to constantly have 127.0.0.1 open in my browser. When I go to the web page, the script runs once. I want the script to be running every x seconds, and what i do right now is I have a header refresh of x seconds. This means the browser reloads the page, and the script runs again every x seconds.

What i want is for my server to do this on its own, without needing a client to visit the webpage. Is there some sort of php technique i should be using?

As a side note, when i use webhooks in php with graph API, i do not need to have a browser window open. It all runs just using the server.

2

Answers


  1. Use a Cron Job, with PHP command line to execute your script.

    Login or Signup to reply.
  2. If you are planning to run the script for every x seconds (x small ) then the best choice if to run the script one time, using an infinite loop, and sleep () to pause the script for x second before continuing excution.

    For example bot.php must look like this

    #!/usr/sbin/php
    <?php
    function run_bot (){
        ...
     }
    
     while  ( 1 ){
          run_bot ();
          sleep(x);
     }
    

    And after that in shell type this:

     chmod +x bot.php
    
     nohup ./bot.php &
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search