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 by ‘cname.pname’,

  • compact’ 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 of type vname.oname as ‘outselect’ :

    value ‘view.top:wwdm.LAI’ to select the ‘LAI’ output data of the view named ‘view’.

    value ‘view.top:wwdm.ST’ to select the ‘ST’ output data of the view named ‘view’.

  • 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 'compact' style of presentation,
    # according to plan values ('single', 'linear') and
    # restype values ('dataframe' or 'matrix')
    function content_simulation_results_compact($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 ("(Output data name (ie selection_name) and value)"."<br />");
            foreach ($res as $compact_outputname=>$val){
                print ("- ".$compact_outputname.' '.$val."<br />");
            }
        }else if ($restype=='dataframe' and $plan=='linear'){
            foreach ($res as $a => $res_a){
                print ("*** simulation number ".' '.$a.":"."<br />");
                print ("(Output data name (ie selection_name) and value)"."<br />");
                foreach ($res_a as $compact_outputname=>$val){
                    $val = json_encode($val);
                    print_r ("- ".$compact_outputname.' '.$val."<br />");
                }
            }
        }else if ($restype=='matrix' and $plan=='single'){
            print ("(View name (ie selection_name) and value)"."<br />");
            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 (ie selection_name) and value)"."<br />");
                foreach ($res_a as $viewname=>$v){
                    $v = json_encode($v);
                    print ("- ".$viewname.' '.$v."<br />");
                }
            }
        }else{ # error (unexpected)
            ;
        }
    }
    
    #######################
    # 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 'cname.pname'
    $inputdata["cond_wwdm.A"] = 0.0064;
    $inputdata["cond_wwdm.Eb"] = 1.86;
    
    $inputdata["mode"] = ["compact", "single", "dataframe"];
    # or $inputdata["mode"] = ["compact", "single", "matrix"];
    
    #----------------------
    # parameters and mode in case of linear plan (multiple simulation)
    
    # some parameters modification with 'cname.pname' (multiple values)
    $inputdata["cond_wwdm.A"] = [0.0064,0.0065,0.0066];
    $inputdata["cond_wwdm.Eb"] = [1.84,1.85,1.86];
    
    $inputdata["mode"] = ["compact", "linear", "dataframe"];
    # or $inputdata["mode"] = ["compact", "linear", "matrix"];
    
    #----------------------
    
    $inputdata["outselect"] = ["view.top:wwdm.LAI", "view.top:wwdm.ST"];
    
    $responsedata = send_post_and_receive($url="http://erecord.toulouse.inra.fr:8000/vpz/output/", $inputdata=$inputdata);
    
    ##########################################################
    # responsedata in case of 'compact' style of presentation
    ##########################################################
    
    if (array_key_exists('res', $responsedata) && array_key_exists('plan', $responsedata) && array_key_exists('restype', $responsedata)){
        content_simulation_results_compact($res=$responsedata['res'],$plan=$responsedata['plan'],$restype=$responsedata['restype']);
    }