I am trying to use a custom field value within a google api url.
I have 2 custom fields. I need to output the value of these custom fields within a url.
Custom field keys:
app_collection-postcode
app_delivery-postcode
This is the google url. I need to put the two postcodes in place of $postcode1
& $postcode2
$url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$postcode1&destinations=$postcode2&mode=driving&language=en-EN&sensor=false";
I have tried the following which sort of worked. My problem with the below is I for some reason had 3333
appear on the page before the result of the echo
and it is using arrays for single value keys.
$custom_fields = get_post_custom(the_ID());
$my_custom_field = $custom_fields['app_collection-postcode'];
$custom_fields2 = get_post_custom(the_ID());
$my_custom_field2 = $custom_fields2['app_delivery-postcode'];
$url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$my_custom_field[0]&destinations=$my_custom_field2[0]&mode=driving&language=en-EN&sensor=false";
$data = @file_get_contents($url);
$result = json_decode($data, true);
foreach($result['rows'] as $distance) {
echo 'Distance from you: ' . $distance['elements'][0]['distance']['text'] . ' (' . $distance['elements'][0]['duration']['text'] . ' in current traffic)';
}
Output of above:
3333Distance from you: 1.9 km (4 mins in current traffic)
Is there a way to simplify the above as it seems bulky and I also am unsure why I am getting the 3333