Using the Bing Maps Web Services in PHP

<?php

class BingMaps {
  // get an application key from https://www.bingmapsportal.com/application/
  private $key = 'YOUR-APPLICATION-KEY';
  private $server = 'http://dev.virtualearth.net/webservices/v1';
  
  function geocode($query, $n = 1){
    $client = new SoapClient($this->server . '/geocodeservice/geocodeservice.svc?wsdl');
    $request = $this->request($query, $n);
    $result = $client->Geocode($request);
    return $result->GeocodeResult->Results->GeocodeResult;
  }

  function search($query, $n = 10){
    $client = new SoapClient($this->server . '/searchservice/searchservice.svc?wsdl');
    $request = $this->request($query, $n);
    $result = $client->Search($request);
    return $result->SearchResult->ResultSets->SearchResultSet->Results->SearchResultBase;
  }

  private function request($query, $n){
    return array('request' => array(
      'Credentials' => array('ApplicationId' => $this->key),
      'Query' => $query,
      'Options' => array('Count' => $n),
    ));
  }
}

/* example usage */

$api = new BingMaps();
$result = $api->geocode('1 Microsoft Way, Redmond, WA, United States');
print_r($result);

$api = new BingMaps();
$result = $api->search('Microsoft in Redmond');
print_r($result);

Microsoft's "documentation".

Comments

They're still using SOAP? Is there an alternative?

Posted by: Damian on January 19, 2010 4:17 PM

Yes they are. There is a Javascript API[1], but the low-level parameters aren't well documented and it doesn't return valid JSON.

[1] http://www.microsoft.com/maps/isdk/ajax/

I hate SOAP web services.

Any ideas on how to get a NuSOAP version up and running? SOAP isn't enabled on the server I'm working on!

Thanks.

Posted by: Leon on January 21, 2010 10:39 AM

PHP5 eh.......

Posted by: MrYellow on March 9, 2010 2:09 AM

All fields are optional, email address will not be shown; no HTML, URLs are automatically hyperlinked.