Question

I have a php script which calculates something very very long.

It is called from a js file and I want to call it from another php file in addition to calling it from the js file.

The file is called strategy.server.php and it is build like so

 <?php
    include_once("../config.php");
    include_once("../class/simulator.class.php");

    $simulator = new Simulator();
    $SPOT = $_GET["strikePrice"];
    $INTEREST = $_GET["interestRate"] / 100;
    $VOLATILITY = $_GET["volatility"] / 100;
    $REMAININGDAYS = $_GET["remainingDays"] / 365;

    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    header("Content-type: application/json; charset=utf-8");

    if(!empty($_GET['mycase']))
    {
        switch($_GET['mycase'])
////calculations... 
            $data['totalMargin'];  
           echo json_encode($data);

I am trying to us the same file to calculate for different parameters (in a loop).
So I thought about using a an include and posting the parameters like so :

$params         = "some params...";
$params     .=  "strikePrice=".$data["strikePrice"].
        "&volatility=".$data["volatility"].
        "&remainingDays=".$data["remainingDays"].
        "&interestRate=".$data["interestRate"].
        "&commission="."7".//$data["commission"].
        "&profitDays="."7";//$data["profitDays"];

include("server/strategy.server.php?" .$params);
json_decode($data);
echo $data['totalMargin'];

but it gives an error:
Warning: include(server/strategy.server.php?mycase=1&rangeMin=0&rangeMax=0&strikePrice=1078&volatility=24&remainingDays=51&interestRate=2.5&commission=7&profitDays=7) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\s-maof-vb\PRO\functions_alarms.php on line 86

If I include the file with the parameters, it loads up (but with no parameters..)

Was it helpful?

Solution

Including a PHP file inserts all the operations from within the file into the current PHP file being executed, so any parameters defined in scope are accessible from within the code.

For example if I had a file called echo.php with the contents:

<?php
echo $test."\n";

And included it within another file:

<?php
$test= "hello";
include("echo.php");
$test = "world";
include("echo.php");

The result would be:

hello
world

So parameters to included files are not passed using HTTP GET method, but just by what is defined in scope. Also, get parameters to the end of a filename in unix will result in the filesystem looking for the entire string on disk.

For the above example, the file would compile out to be:

<?php
$test= "hello";
echo $test."\n";
$test = "world";
echo $test."\n";

Therefore in your case, you may want to edit the strategy.server.php script to check first whether data is passed in a params array and then default to GET:

if(empty($params)){
   $params = $_GET; 
}
// filter params for user input;

OTHER TIPS

The include function doesn't accept parameters in a way HTTP does. Also, what you are trying to do isn't best practice, because it includes the same file multiple times. You have to create a for- or while-loop going through the array of $data values.

If you still want this functionality I suggest using the cURL library or file_get_contents() to call the script (instead of including).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top