Example of POST vpz/output in PHP language

For the simulator ‘wwdm.vpz’ whose Id is 266, use the PHP code into a php tag :

<?php
… PHP code …

?>

Example illustrating :

  • modifying begin,

  • modifying duration,

  • modifying some parameters with ‘pars’,

  • tree’ as style of presentation,

  • json’ as format,

  • value ‘single’ as plan, or value ‘linear’ in case of multiple simulation

  • value ‘dataframe’ as restype (or ‘matrix’)

  • value ‘all’ as ‘outselect’,

  • with ‘application/json’ as ‘Content-Type’

    Memo

    # PHP code :
    
    # send POST request and return response datas
    function send_post_and_receive($url, $inputdata) {
        #
        ob_start();
        $c = curl_init();
        #
        curl_setopt($c, CURLOPT_POST, TRUE);
        curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        #
        curl_setopt($c, CURLOPT_URL, $url);
        #
        # to follow redirect
        curl_setopt($c, CURLOPT_FOLLOWLOCATION, True);
        #
        $json_inputdata = json_encode($inputdata);
        curl_setopt($c, CURLOPT_POSTFIELDS, $json_inputdata);
        #
        curl_exec($c);
        $buffer_str = ob_get_contents();
        ob_end_clean();
        #
        $responsedata = json_decode($buffer_str, TRUE);
        curl_close($c);
        
        return $responsedata;
    }
    
    
    # PHP code :
    
    # content of simulation results (res) in 'tree' style of presentation,
    # according to plan values ('single', 'linear') and
    # restype values ('dataframe' or 'matrix')
    function content_simulation_results_tree($res, $plan, $restype){
        #
        print ("plan, restype :".$plan.' '.$restype."<br />");
        print ("res :".$res."<br />");
        print ("\nDetailing the results :"."<br />");
        $res = json_decode($res, TRUE);
    
        if ($restype=='dataframe' and $plan=='single'){
            print ("(view name, output data name, value)"."<br />");
            foreach ($res as $viewname=>$outputs){
                foreach ($outputs as $outputname=>$val){
                    print ("- ".$viewname.' '.$outputname.' '.$val);
                }
            }
        }else if ($restype=='dataframe' and $plan=='linear'){
            foreach ($res as $a => $res_a){
                print ("*** simulation number ".$a." :"."<br />");
                print ("(view name, output data name, value)"."<br />");
                foreach ($res_a as $viewname=>$outputs){
                    foreach ($outputs as $outputname=>$val){
                        $val = json_encode($val);
                        print_r ("- ".$viewname.' '.$outputname.' '.$val."<br />");
                    }
                }
            }
        }else if ($restype=='matrix' and $plan=='single'){
            print ("(view name, value)");
            foreach ($res as $viewname=>$v){
                print ("- ".$viewname.' '.$v."<br />");
            }
        }else if ($restype=='matrix' and $plan=='linear'){
            foreach ($res as $a => $res_a){
                print ("*** simulation number ".$a." :"."<br />");
                print ("(view name, value)"."<br />");
                foreach ($res_a as $viewname=>$v){
                    $v = json_encode($v);
                    print ("- ".$viewname.' '.$v."<br />");
                }
            }
        }else{ # error (unexpected)
            ;
        }
    }
    
    
    # PHP code :
    
    #######################
    # request and response
    #######################
    
    $inputdata = ["vpz"=>266, "format"=>"json"];
    $inputdata["duration"] = 6;
    $inputdata["begin"] = 2453982.0;
    
    #----------------------
    # parameters and mode in case of single plan (single simulation)
    
    # some parameters modification with 'pars'
    $parameter_A = ["selection_name"=>"cond_wwdm.A", "cname"=>"cond_wwdm", "pname"=>"A", "value"=>0.0064];
    $parameter_Eb = ["selection_name"=>"cond_wwdm.Eb", "cname"=>"cond_wwdm", "pname"=>"Eb", "value"=>1.86];
    
    $pars = array($parameter_A, $parameter_Eb);
    $inputdata["pars"] = $pars;
    
    $inputdata["mode"] = ["tree", "single", "dataframe"];
    # or $inputdata["mode"] = ["tree", "single", "matrix"];
    
    #----------------------
    # parameters and mode in case of linear plan (multiple simulation)
    
    # some parameters modification with 'pars' (multiple values)
    $parameter_A = ["selection_name"=>"cond_wwdm.A", "cname"=>"cond_wwdm", "pname"=>"A", "value"=>[0.0064, 0.0065, 0.0066]];
    $parameter_Eb = ["selection_name"=>"cond_wwdm.Eb", "cname"=>"cond_wwdm", "pname"=>"Eb", "value"=>[1.84, 1.85, 1.86]];
    
    $pars = array($parameter_A, $parameter_Eb);
    $inputdata["pars"] = $pars;
    
    $inputdata["mode"] = ["tree", "linear", "dataframe"];
    # or $inputdata["mode"] = ["tree", "linear", "matrix"];
    
    #----------------------
    
    $inputdata["outselect"] = "all";
    
    $responsedata = send_post_and_receive($url="http://erecord.toulouse.inra.fr:8000/vpz/output/", $inputdata=$inputdata);
    
    #######################################################
    # responsedata in case of 'tree' style of presentation
    #######################################################
    
    # id as VpzOutput
    if (array_key_exists("id",$responsedata)){
        $id = $responsedata['id'];
    }
    if (array_key_exists('res', $responsedata) && array_key_exists('plan', $responsedata) && array_key_exists('restype', $responsedata)){
        content_simulation_results_tree($res=$responsedata['res'],$plan=$responsedata['plan'],$restype=$responsedata['restype']);
    }