0

Newby to PHP im affraid but gave this a spin:

Use gform_pre_render to put streetaddress and city entered in page 1 available on page 2, to be used to calculate a distance from addr to customer address

$result_end now delivers "ARRAY"on the target field , if i change this to $result_end('meters') the form hangs from page 1 to page 2.

EDIT: In the Browserconsole under "requests" i see Status Server error 500 but dont understand why..

Any pointers would be greatly appreciated.

Here's my code (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;
}
  • Hi Bart. Have you looked at wp_remote_get rather than curl? – Howard E Mar 24 at 10:19
  • Thx for your reply. Havent looked at it to be honest becuse the url gets fetched. – bart Mar 25 at 11:06
0

Ok so now im as far as to getting "meters" returned after customer filled in street/city in page 1 of the form.

Only thing is i only manage to get the result into an "html-block" with code underneath (by now plugin). I would like to get the result into the quantity field of a product, but parsing result to "value" of relevant field id doenst seem to work.

example: terramilieu.nl/test

My code:


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;
}       

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.