admin管理员组文章数量:1026989
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 = "=".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; }
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/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; }Share Improve this question edited Mar 23, 2019 at 1:05 bart asked Mar 22, 2019 at 23:14 bartbart 11 bronze badge 2
- Hi Bart. Have you looked at wp_remote_get rather than curl? – Howard E Commented Mar 24, 2019 at 10:19
- Thx for your reply. Havent looked at it to be honest becuse the url gets fetched. – bart Commented Mar 25, 2019 at 11:06
1 Answer
Reset to default 0Ok 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/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; }
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 = "=".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; }
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/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; }Share Improve this question edited Mar 23, 2019 at 1:05 bart asked Mar 22, 2019 at 23:14 bartbart 11 bronze badge 2
- Hi Bart. Have you looked at wp_remote_get rather than curl? – Howard E Commented Mar 24, 2019 at 10:19
- Thx for your reply. Havent looked at it to be honest becuse the url gets fetched. – bart Commented Mar 25, 2019 at 11:06
1 Answer
Reset to default 0Ok 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/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; }
版权声明:本文标题:Use gform_pre_renderGoogle distance Matrix to calculate distance on page 2 of form based on input on page1 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745663687a2162046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论