使用GForm_Pre_Render/Google距离矩阵根据第一页上的输入计算第二页上的距离

时间:2019-03-22 作者:bart

新手对PHP大发雷霆,但对此进行了一番解读:

使用gform\\u pre\\u render将第1页中输入的街道地址和城市放在第2页,用于计算地址到客户地址的距离

$result\\u end现在在目标字段上传递“ARRAY”,如果我将其更改为$result\\u end(\'meters\'),表单将从第1页挂起到第2页。

编辑:在Browserconsole的“requests”下,我看到Status Server错误500,但不明白原因。。

任何指点都将不胜感激。

下面是我的代码(functions.php):

/* GDM 2019 */
function getDrivingDistance($startstreet, $startcity, $endstreet, $endcity)
{
    $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".urlencode($startstreet)."+".urlencode($startcity)."&destinations=".urlencode($endstreet)."+".urlencode($endcity)."&mode=driving&key=AIzaSyDtNDZRDEy2b8FLchNY4I2Okl1sLlFEIDw";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    curl_close($ch);
    $response_a = json_decode($response, true);

    $dist = $response_a[\'rows\'][0][\'elements\'][0][\'distance\'][\'text\'];
    $meters = $response_a[\'rows\'][0][\'elements\'][0][\'distance\'][\'value\'];
    $time = $response_a[\'rows\'][0][\'elements\'][0][\'duration\'][\'text\'];


    return array(\'distance\' => $dist, \'meters\'=> $meters, \'time\' => $time);
}


function gdmDist($street, $city){
    $result = getDrivingDistance(\'Lindenstraat 12\', \'Schijndel\', $street, $city);

    return ($result[\'meters\']);
}


add_filter( \'gform_pre_render_6\', \'populate_html\' ); 

    function populate_html( $form ) {
    //this is a 2-page form with the data from page one being displayed in an html field on page 2
    $current_page = GFFormDisplay::get_current_page( $form[\'id\'] );

    if ( $current_page == 2 ) {
        foreach ( $form[\'fields\'] as &$field ) {
            //gather form data to save into html field (id 6 on my form), exclude page break
            $field_data = rgpost(\'input_\' . $field->id );
            if ( $field->id == 49 && $field->type != \'page\' ) 
                {                                        
                    $html_content_49 .= $field_data;                                        
                }            
            elseif ( $field->id == 50 ) 
            {         
                    $html_content_50 .= $field_data;                               
            }            
        }                                   
        //loop back through form fields to get html field (id 6 on my form) that we are populating with the data gathered above
        foreach( $form[\'fields\'] as &$field ) {
            //get html field
            if ( $field->id == 51 ) {
                //set the field content to the html
                $field->content = $html_content_49;
            }              
            elseif ( $field->id == 58 ) {
                //set the field content to the html
                $field->content = $html_content_50;
            } 
            elseif ( $field->id == 60 ) {
                $fieldval_address =  $html_content_49;
                $fieldval_city = $html_content_50;
                $result_end = getDrivingDistance(\'Industrieweg 16\', \'Vught\', $fieldval_address, $fieldval_city);                    
                //$result = gdmDist($fieldval_address, $fieldval_city);                    
                //set the field content to the html                
                $field->content = $result_end;                
            } 
            else {}
        }                                        
    }
    //return altered form so changes are displayed
    return $form;
}

1 个回复
SO网友:bart

好的,现在我要在客户填写表格第1页的街道/城市后退回“米”。

唯一的问题是,我只能将结果放入一个“html块”,下面有代码(现在是插件)。我希望将结果放入产品的quantity字段,但将结果解析为相关字段id的“value”似乎不起作用。

示例:TerraMileu。nl/测试

我的代码:


function getDrivingDistance($startstreet, $startcity, $endstreet, $endcity)
{
    $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".urlencode($startstreet)."+".urlencode($startcity)."&destinations=".urlencode($endstreet)."+".urlencode($endcity)."&mode=driving&key=AIzaSyDtNDZRDEy2b8FLchNY4I2Okl1sLlFEIDw";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    curl_close($ch);
    //$response_a = json_decode($response, true);
    $json = file_get_contents($url); // get the data from Google Maps API
    $result = json_decode($json, true); // convert it from JSON to php array

    $dist = $result[\'rows\'][0][\'elements\'][0][\'distance\'][\'text\'];
    $meters = $result[\'rows\'][0][\'elements\'][0][\'distance\'][\'value\'];
    $time = $result[\'rows\'][0][\'elements\'][0][\'duration\'][\'text\'];

    return array(\'distance\' => $dist, \'meters\'=> $meters, \'time\' => $time);
}


function gdmDist($street, $city){
    $result = getDrivingDistance(\'Industrieweg 16\', \'Vught\', $street, $city);

    return ($result[\'meters\']);
}


add_filter( \'gform_pre_render_6\', \'populate_html\' ); 

    function populate_html( $form ) {
    //this is a 2-page form with the data from page one being displayed in an html field on page 2
    $current_page =GFFormDisplay::get_current_page( $form[\'id\'] );

    if ( $current_page == 2 ) {
        foreach ( $form[\'fields\'] as &$field ) {
            //gather form data to save into html field (id 6 on my form), exclude page break
            $field_data = rgpost(\'input_\' . $field->id );
            if ( $field->id == 49 && $field->type != \'page\' ) 
                {                                        
                    $html_content_49 = $field_data;                                        
                }            
            elseif ( $field->id == 50 ) 
            {         
                    $html_content_50 = $field_data;                               
            }            
        }                                   
        //loop back through form fields to get html fields that we are populating with the data gathered above
        foreach( $form[\'fields\'] as &$field ) {
            //get html field
            if ( $field->id == 51 ) {
                //set the field content to the html
                $field->content = $html_content_49;
            }              
            if ( $field->id == 58 ) {
                //set the field content to the html
                $field->content = $html_content_50;
            } 
            if ( $field->id == 60 ) {
                $fieldval_address =  $html_content_49;
                $fieldval_city = $html_content_50;
                $result_end = getDrivingDistance(\'Industrieweg 16\', \'Vught\', $fieldval_address, $fieldval_city);                    
                //$result = gdmDist($fieldval_address, $fieldval_city);                    
                //set the field content to the html                
                $field->content = $result_end[\'meters\'];                
            }             
        }  
    }
    //return altered form so changes are displayed
    return $form;
}       

相关推荐