Example of POST vpz/inout 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’,

  • tree’ as style of presentation,

  • json’ as format,

  • values of type cname.pname as ‘parselect’ :

    value ‘cond_wwdm.A’ to select the parameter named ‘A’ of the condition named ‘cond_wwdm’.

    value ‘cond_wwdm.B’ to select the parameter named ‘B’ of the condition named ‘cond_wwdm’.

    value ‘cond_wwdm.Eb’ to select the parameter named ‘Eb’ of the condition named ‘cond_wwdm’.

    value ‘cond_wwdm.TI’ to select the parameter named ‘TI’ of the condition named ‘cond_wwdm’.

  • value ‘single’ as plan

  • value ‘dataframe’ as restype

  • 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 input information in 'tree' style of presentation
    # (vpzinput)
    function content_simulation_inputs_tree($vpzinput){
        #
        # duration
        if (array_key_exists('vleduration', $vpzinput)){
            $vleduration = $vpzinput['vleduration'];
            $duration_value = $vleduration['value'];
            #duration_verbose_name = $vleduration['verbose_name'];
            #$duration_id = $vleduration['id'];
            print ("duration value : ".$duration_value."<br />");
        }
        #
        # begin
        if (array_key_exists('vlebegin', $vpzinput)){
            $vlebegin = $vpzinput['vlebegin'];
            $begin_value = $vlebegin['value'];
            #$begin_verbose_name = $vlebegin['verbose_name'];
            #$begin_id = $vlebegin['id'];
            print ("begin value : ".$begin_value."<br />");
        }
        #
        # conditions and parameters
        if (array_key_exists('vlecond_list', $vpzinput)){
            $conds = $vpzinput['vlecond_list'];
            foreach ($conds as $cond){
                $cond_verbose_name = $cond['verbose_name'];
                $cond_id = $cond['id'];
                $cond_name = $cond['name'];
                print ("\nCondition name ".$cond_name."<br />");
                print ("List of its parameters (id, cname, pname, type, value, selected, verbose_name) :"."<br />");
                $pars = $cond['vlepar_list'];
                foreach ($pars as $par){
                    $par_verbose_name = $par['verbose_name'];
                    $par_id = $par['id'];
                    $par_cname = $par['cname'];
                    $par_pname = $par['pname'];
                    $par_type = $par['type'];
                    $par_value = $par['value'];
                    $par_selected = $par['selected'];
                    print ("- ".$par_id." ".$par_cname." ".$par_pname." ".$par_type." ".$par_value." ".$par_selected." ".$par_verbose_name."<br />");
                }
            }
        }
        #
        # views and output datas identity
        if (array_key_exists('vleview_list', $vpzinput)){
            $views = $vpzinput['vleview_list'];
            foreach ($views as $view){
                $view_verbose_name = $view['verbose_name'];
                $view_id = $view['id'];
                $view_name = $view['name'];
                $view_type = $view['type'];
                $view_timestep = $view['timestep'];
                $view_output_format = $view['output_format'];
                $view_output_location = $view['output_location'];
                $view_output_name = $view['output_name'];
                $view_output_plugin = $view['output_plugin'];
                print ("\nView name ".$view_name."<br />");
                print ("List of its output datas (id, vname, oname, shortname, selected, verbose_name) :<br />");
                $outs = $view['vleout_list'];
                foreach ($outs as $out){
                    $out_verbose_name = $out['verbose_name'];
                    $out_id = $out['id'];
                    $out_vname = $out['vname'];
                    $out_oname = $out['oname'];
                    $out_shortname = $out['shortname'];
                    $out_selected = $out['selected'];
                    print ("- ".$out_id." ".$out_vname." ".$out_oname." ".$out_shortname." ".$out_selected." ".$out_verbose_name."<br />");
                }
            }
        }
    }
    
    
    # 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' && $plan=='single'){
            print ("(view name, output data name, value)<br />");
            foreach ($res as $viewname=>$outputs){
                foreach ($outputs as $outputname=>$val){
                    $val = json_encode($val);
                    print_r ("- ".$viewname." ".$outputname." ".$val."<br />");
                }
            }
        }else if ($restype=='dataframe' && $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' && $plan=='single'){
            print ("(view name, value)");
            foreach ($res as $viewname=>$v){
                print ("- ".$viewname." ".$v."<br />");
            }
        }else if ($restype=='matrix' && $plan=='linear'){
            foreach ($res as $a => $res_a){
                print ("*** simulation number ".$a." :<br />");
                print ("(view name, value)<br />");
                foreach ($res_a as $viewname=>$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;
    
    $inputdata["mode"] = ["tree", "single", "dataframe"];
    
    # some parameters modification with 'cname.pname'
    $inputdata["cond_wwdm.A"] = 0.0064;
    $inputdata["cond_wwdm.Eb"] = 1.86;
    
    $inputdata["parselect"] = ["cond_wwdm.A", "cond_wwdm.B", "cond_wwdm.Eb", "cond_wwdm.TI"];
    
    $inputdata["outselect"] = ["view.top:wwdm.LAI", "view.top:wwdm.ST"];
    
    $responsedata = send_post_and_receive($url="http://erecord.toulouse.inra.fr:8000/vpz/inout/", $inputdata=$inputdata);
    
    #######################################################
    # responsedata in case of 'tree' style of presentation
    #######################################################
    
    # id as VpzAct
    if (array_key_exists('id', $responsedata)){
        $id = $responsedata['id'];
    }
    
    if (array_key_exists('verbose_name', $responsedata)){
        $verbose_name = $responsedata['verbose_name'];
    }
    
    #----------------------
    # input information of simulation
    if (array_key_exists('vpzinput', $responsedata)){
        $vpzinput = $responsedata['vpzinput'];
        #
        # id as VpzInput
        if (array_key_exists('id', $vpzinput)){
            $id = $vpzinput['id'];
        }
        #
        content_simulation_inputs_tree($vpzinput=$vpzinput);
    
    }
    
    #----------------------
    # output information of simulation
    if (array_key_exists('vpzoutput', $responsedata)){
        $vpzoutput = $responsedata['vpzoutput'];
        #
        # id as VpzOutput
        if (array_key_exists('id', $vpzoutput)){
            $id = $vpzoutput['id'];
        }
        #
        if (array_key_exists('res', $vpzoutput) && array_key_exists('plan', $vpzoutput) && array_key_exists('restype', $vpzoutput)){
            content_simulation_results_tree($res=$vpzoutput['res'], $plan=$vpzoutput['plan'], $restype=$vpzoutput['restype']);
        }
    }
    
    #----------------------
    # others
    if (array_key_exists('vpzorigin', $responsedata)){
        $vpzorigin = $responsedata['vpzorigin'];
    }
    
    if (array_key_exists('pkgname', $responsedata)){
        $pkgname = $responsedata['pkgname'];
    }
    
    if (array_key_exists('vlepath', $responsedata)){
        $vlepath = $responsedata['vlepath'];
    }
    
    if (array_key_exists('vpzname', $responsedata)){
        $vpzname = $responsedata['vpzname'];
    }
    
    if (array_key_exists('vpzworkspace', $responsedata)){
        $vpzworkspace = $responsedata['vpzworkspace'];
    }