BING Maps REST API using PHP

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



BING Maps REST API using PHP



I am using Bing Maps API to Geocode addresses. I took out an example from this page https://msdn.microsoft.com/en-us/library/ff817004.aspx which was form based process, in which you have to submit the address to get longitude and latitude. Now the requirement in front of me is that, I would like to manually put addresses in PHP page and get longitude and latitude. It is not happening. This is what we have tried as of now:


$key=key;

$baseURL = "http://dev.virtualearth.net/REST/v1/Locations";

$query ="Rajalakshmi Mill Road, Singanallur";
$findURL = $baseURL."/".$query."?output=xml&key=Aukd2ilaNJexSjdSjdkoGr26cpoqaVUhOg0MbDTZtfPGClozardCt_1iRscSm5Xo";

$output = file_get_contents($findURL);
$response = new SimpleXMLElement($output);

// Extract and pring latitude and longitude coordinates from results
$latitude = $response->ResourceSets->ResourceSet->Resources->Location->Point->Latitude;
$longitude = $response->ResourceSets->ResourceSet->Resources->Location->Point->Longitude;
$longitude = $response->ResourceSets->ResourceSet->Resources->Location->Point->Longitude;
$address = $response->ResourceSets->ResourceSet->Resources->Location->Address->FormattedAddress->State;

echo "Latitude: ".$latitude."<br>";
echo "Longitude: ".$longitude."<br>";
echo $address1;



What shall I do?





first i would remove your api key ;)
– Domenik Reitzner
Aug 6 at 14:48





What exactly do you mean by "it is not happening"? What have you tried to debug your problem?
– Nico Haase
Aug 6 at 14:50





I have updated the question. Do check.
– Husain
Aug 6 at 14:52





Thanks Domenik. I did this in hurry, got it wrong.
– Husain
Aug 6 at 14:53





You don't use the schema that is provided: http://dev.virtualearth.net/REST/v1/Locations/countryRegion/adminDistrict/postalCode/locality/addressLine?key=yourBingMapsKey. I would also recommend just typing the url into the browser....
– Domenik Reitzner
Aug 6 at 14:53



http://dev.virtualearth.net/REST/v1/Locations/countryRegion/adminDistrict/postalCode/locality/addressLine?key=yourBingMapsKey




2 Answers
2



Looking at the MSDN page, I see that you have to do this before sending in the query,



$query = str_ireplace(" ","%20",$_POST['query']);


$query = str_ireplace(" ","%20",$_POST['query']);



The api requires you to manually replace the space character with %20. You could also use url_encode as a safer option.


%20



Other options include using curl as @Domenik Reitzner suggested.


$ch = curl_init();

// Set query data here with the URL
curl_setopt($ch, CURLOPT_URL, $findURL);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$content = trim(curl_exec($ch));
curl_close($ch);
$response = new SimpleXMLElement($content);



Update:



I just saw the update to your answer and you included a URL in it, when I tried to access it this was the result.



XML



By the way, I recommend you delete your credentials and generate a new one and also remember not to post your API keys online next time.



Update 2



Found the problem, the baseUrl should be



http://dev.virtualearth.net/REST/v1/Locations


http://dev.virtualearth.net/REST/v1/Locations



instead of



http://dev.virtualearth.net/REST/v1/Routes


http://dev.virtualearth.net/REST/v1/Routes



The code in your question has the right URL but the error you got has the wrong URL.



Error in code



This is the result I get when I query http://dev.virtualearth.net/REST/v1/Locations/Rajalakshmi%20Mill%20Road,%20Singanallur,%20Coimbatore?output=xml&key=YOURAPIKEY


http://dev.virtualearth.net/REST/v1/Locations/Rajalakshmi%20Mill%20Road,%20Singanallur,%20Coimbatore?output=xml&key=YOURAPIKEY



response





The problem is in the URL format... :)
– Domenik Reitzner
Aug 6 at 15:01





My bad I thought valid credentials was an error. Didn't think to look twice lol
– kks21199
Aug 6 at 15:03





@DomenikReitzner Seems the baseURL he was querying was the problem.
– kks21199
Aug 6 at 15:11


baseURL





Yeah now it works for me too :) yeay
– Domenik Reitzner
Aug 6 at 15:13





@kks21199. Thanks, I could bear with your sarcasm since you get me the solution lol. Anyway, thanks all of you for the help.
– Husain
Aug 6 at 15:27



This is what I get as a response with the provided link:


<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
<Copyright>
Copyright © 2018 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.
</Copyright>
<BrandLogoUri>
http://dev.virtualearth.net/Branding/logo_powered_by.png
</BrandLogoUri>
<StatusCode>400</StatusCode>
<StatusDescription>Bad Request</StatusDescription>
<AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
<ErrorDetails>
<string>One or more parameters are not valid.</string>
<string>
travelMode: This parameter value has an invalid format.
</string>
</ErrorDetails>
<TraceId>
dea21427b82347128fc5f7012bda5592|DB40060630|7.7.0.0
</TraceId>
<ResourceSets/>
</Response>



Which brings me back to the expected URL format:


http://dev.virtualearth.net/REST/v1/Locations/countryRegion/adminDistrict/postalCode/locality/addressLine?key=yourBingMapsKey






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard