user1254065
user1254065

Reputation: 1

cronjob running php web application

Using cPanel I want to setup a cronjob. I have a php page doing the import with post data. But to reach that import page I have to pass a login page also working with post data.

Can it be done?

Upvotes: 0

Views: 588

Answers (2)

mihaidoru
mihaidoru

Reputation: 327

You can create a PHP script that is accessed via GET and does the two POST requests itself.

You can simulate the POST request on both the login page and the import page by using PHP CURL.

Steps

  1. Send a POST request with CURL to the login page. You specify to CURL to keep your cookies (cookie jar, read the manual for the exact syntax).

  2. After the session cookie is saved in the cookie jar (a file), send another post to your import page, using the authentication received from the first POST.

You can find more information about the CURL POST here: http://www.electrictoolbox.com/php-curl-form-post/

Upvotes: 2

hohner
hohner

Reputation: 11588

There are two simple ways of doing this:

1. Using cURL

In your cron panel, enter this command:

curl --silent --compressed curl http://example.com/script.php > /dev/null 2>&1

The /dev/null 2>&1 disables mail alerts when the crontab task runs. This option will not work, however, if the script you're accessing requires login verification. All the server is doing is pinging this web address, it's not filling in any forms. So if you're using login forms, you can either use step 2. or clone the script and enable it to work without login verification.

2. Accessing the PHP script internally

I prefer this method because it avoids an unnecessary TCP stack setup and teardown. You need to know the location of your PHP bin directory, though. The command you need is:

/usr/local/bin/php -f /home/(username)/public_html/(scriptname).php

Some servers you can just use:

php -f /home/(username)/public_html/(scriptname).php

Upvotes: 1

Related Questions