-1

I've been working on a website and one of its functionalities is a budget maker, that is, a place where the user can somewhat find out what they'd be spending for their service of choice, however, googles API is paid and at the moment, for this project, it isn't viable for me to do it that way, I've been trying to use Microsoft's Bing maps API, but its all with "URLs" and JSON's and I'm kind of out of my depth, can anyone explain to me maybe what I need to do to "decode" the JSON file and get the distance parameters?

Documentation: https://learn.microsoft.com/en-us/bingmaps/rest-services/routes/calculate-a-route

My URL code: "http://dev.virtualearth.net/REST/v1/Routes?wayPoint.1=$startAdress&viaWaypoint.2=$UserInputedAdress&travelMode=Driving&optimize=distance&distanceUnit=km&key=BingMapsKey";

My code for the decoding json - print_r(json_decode($distanciaBing)); (always returns '1', nothing else no matter the inputs.

OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
iRuben
  • 1
  • 4
  • "... between 2 points", what are your points? lat/long, addresses? If lat/long you could use the haversine formula. See https://stackoverflow.com/a/10054282/296555 – waterloomatt Mar 31 '22 at 20:23
  • "googles API is paid and at the moment" Is that true? I'm still using an API key from a few years ago and don't get charged. That said, the site is low volume so that might change if I start getting more traffic. – waterloomatt Mar 31 '22 at 20:27
  • @waterloomatt addresses thought I said it my bad, I've looked into that post but I don't have the lat and long and they allow me to get the distance in KM through the documentation I presented. my issue is more of how to decode the JSON brought by the URL to get the KM's. And yes the API is paid, both GeoLocating and the Distance Matrix calculations – iRuben Mar 31 '22 at 20:37
  • OK. Please edit your question with a sample JSON response so we can help. – waterloomatt Mar 31 '22 at 22:32
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 01 '22 at 02:22
  • the code is there that's all the code I have, the API functions through URL's that you send the specifications through and then you decode a JSON file supposedely – iRuben Apr 02 '22 at 09:51

2 Answers2

1

It really depends on what $distanciaBing is from your question. I will infer that that is a string that contains the query data you need to parse.

From there it will just be a matter of locating the beginning and end of the JSON data that you actually need.

You will have to do this manually (at first) before hard coding it. It will simply be a matter of locating in that return result, manually, something unique that begins that JSON string. Again, and manually locate something unique that ends that JSON string. Then you will be able to extract it from $distanciaBing.

E.g.

$uniqueStart = ""; // replace what you found on that which is unique that is just before the JSON string

$uniqueEnd = ""; // replace what you found on that which is unique just after the JSON string

$jsonBeginCharPos = strpos($distanciaBing, $uniqueStart) + strlen($uniqueStart);

$jsonEndCharPos = strpos($distanciaBing, $uniqueEnd) - 1;

$jsonStringNeeded = substr($distanciaBing, $jsonBeginCharPos, ($jsonEndCharPos - $jsonBeginCharPos) + 1);

On latter, we extract the string we want, that being the JSON string. If on manual tests this ends up being the string we want, we can then hard code this functionality.

So up to this point we have sucessfully extracted the json string, and now can convert it into an associative array via PHP parlance:

$jsonArray = json_decode( $jsonStringNeeded, true );

And from here you can do whatever it is that needs to be done to use that data.

mardubbles
  • 129
  • 6
  • Thank you, despite the code, it still doesn't return anything really... I managed to get access to the JSON file going through the Microsoft URL but now I don't know how to access the "Travel Distance" attribute or any attribute within the JSON file for that matter. – iRuben Apr 02 '22 at 10:35
0

Might be worth it to someone in the future if they ever come across this post, so: this is the fix to get the object back from the JSON:

$getDistance = "http://dev.virtualearth.net/REST/v1/Routes?o=xml&wp.0=$startAdress&wp.1=$endAdress&travelMode=Driving&optimize=distance&distanceUnit=km&key=(bingKey)";

$fileContents = file_get_contents($getDistance);
$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
$simpleXml = simplexml_load_string($fileContents);

$json = json_encode($simpleXml);
$obj= json_decode($json);

$TravelDistance = $obj->ResourceSets->ResourceSet->Resources->Route->TravelDistance; 

It's a tad weird had to get it from JSON to XML back to JSON with 'encode' and then finally decode it one last time to get access to the object with every field to go through it.

iRuben
  • 1
  • 4