BING 使用 PHP 映射 REST API

BING Maps REST API using PHP

我正在使用 Bing 地图 API 对地址进行地理编码。我从这个页面 https://msdn.microsoft.com/en-us/library/ff817004.aspx 中取出了一个示例,它是基于表单的过程,您必须提交地址才能获得经度和纬度。现在摆在我面前的需求是,我想在PHP页面中手动输入地址并获取经纬度。它没有发生。这是我们目前已经尝试过的:

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

我该怎么办?

查看 MSDN 页面,我发现您必须在发送查询之前执行此操作,

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

api 需要您手动将 space 字符替换为 %20。您也可以使用 url_encode 作为更安全的选择。

其他选项包括按照@Domenik Reitzner 的建议使用 curl。

$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);

更新:

我刚看到您的回答更新,您在其中包含了一个 URL,当我尝试访问它时,结果就是这样。

顺便说一下,我建议您删除您的凭据并生成一个新凭据,并且记住下次不要 post 您的 API 密钥在线。

更新 2

发现问题,baseUrl应该是

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

而不是

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

你问题中的代码是正确的URL但是你得到的错误是错误的URL。

这是我查询得到的结果http://dev.virtualearth.net/REST/v1/Locations/Rajalakshmi%20Mill%20Road,%20Singanallur,%20Coimbatore?output=xml&key=YOURAPIKEY

这是我通过提供的 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>

这让我回到了预期的 URL 格式:

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