0

I have a php file(index.php) which calls another file(ajax.php), which consists of javascript and ajax calls.

index.php, $mid is a variable fetched from database:

<?php
$url = "http://localhost/parser/ajax.php?m=$mid";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);

ajax.php :

<?php
$mid = $_GET["mid"];

var r = {
    feedLife: 60,
    feedParams: {
      competition: '129',
      season: SEASON,
      sport: SPORT,
      match: <?php echo $mid ?>,
    }
    };

$.ajax({
    url: 'upload_json.php?m=<?php echo $mid ?>',
    dataType: 'json',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify(a),
    processData: false,
    success: function(){

    },
    error: function( jqXhr, textStatus, errorThrown ){
        console.log( errorThrown );
    }
});

I want to run a cron, but as console can not parse javascript, I have no idea how to achieve automation? Any ideas? Maybe phantomJS can do a trick?

thanks in advance

Believe It or Not
  • 631
  • 2
  • 8
  • 21
S.Kav
  • 65
  • 6
  • do you have an idea of serverside and clientside execution and that JavaScript is executed clientside while php runs on the server? – messerbill May 30 '16 at 13:08
  • Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Pedro Lobito May 30 '16 at 13:09
  • You can mimic the AJAX request with cURL. If you don't want to go through many cURL calls, you should consider PhantomJS. You know what to do, so get started – Adam Azad May 30 '16 at 13:16
  • Can you give us the end goal of the process. It looks like you are jumping through a bunch of hoops just to pass a variable to upload_json.php. Im sure there is a reason, its just not clear what that reason is. – MerlinTheMagic Jun 11 '16 at 15:31

3 Answers3

0

you can not write javascript in php file

try to use NodeJs + Cron

Dumkaaa
  • 488
  • 2
  • 9
0

You have error, you need close php tag after variable $mid. Example

<?php
$mid = $_GET["mid"];

?>
0

concatenate you're all javascript code in string, and wrap in tag and use echo

    <?php
$mid = $_GET["mid"];

$script = "<script> var r = {
    feedLife: 60,
    feedParams: {
      competition: '129',
      season: SEASON,
      sport: SPORT,
      match: '".$mid."',
    }
    };

$.ajax({
    url: 'upload_json.php?m=".$mid."',
    dataType: 'json',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify(a),
    processData: false,
    success: function(){

    },
    error: function( jqXhr, textStatus, errorThrown ){
        console.log( errorThrown );
    }
});</script>";

echo $script;