Adrian
Adrian

Reputation: 2012

Running a url scipt inside php (that contains ajax)

I have a site that sends urls via email to my client, once they receive them they click the link, it loads in a browser and completes some Ajax that calls a PHP script. There are several AJAX functions being called in this script.

They have requested that this process be automated so they dont have to click the link and wait approximately 15 minutes each time for all the Ajax to be complete.

Ideally, without recoding the functionality and continuing to use the exact same Ajax I would love to automate this process. So I would like to run a cron that loads a script that calls these URLS instead.

Is this possible?

I have tried several things, but nothing is happening when I load the script. When I mean nothing I mean neither errors, nor the functionality of the script. (I have error reporting turned on).

E.g.

cUrl

init_set('display_errors',1);
error_reporting(E_ALL);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/myscript.php?varialbe=sample_get_content");
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);

curl_close($ch);

exec

exec('http://example.com/myscript.php');

simply opening the script...

$contents = file_get_contents('http://example.com/myscript.php?varialbe=sample_get_content');

I know that another option is to rebuild the functionality so that Im not using AJAX, but I would prefer not do that as it will take time.

EDIT: The actual script URL itself being called changes due to change in GET variables, so I cannot run it directly via cron (or can I?)

Upvotes: 0

Views: 116

Answers (1)

KevInSol
KevInSol

Reputation: 2642

Suggested approach.

In script that sends link, instead of sending a link with unique GET data, have it do this:

exec("./myscript.php $param_1 $param_2");

In myscript.php replace:

$param_1 = $_GET['param_1'];
$param_2 = $_GET['param_2'];

With

$param_1 = $argv[1];
$param_2 = $argv[2];

http://php.net/manual/en/reserved.variables.argv.php

Also add

#!/path/to/phpcgibin -q

to myscript.php before the <? and make sure to upload as ascii

Upvotes: 1

Related Questions