Bing Local Business Search

Spread the love

In this tutorial we are going to see how to use the Bing Local Business Search API to get results. The Bing Local Business Search API is a RESTful service that helps you identify local business entities in a targeted area based on users’ text search queries, like restaurants hair salons etc.

Prerequisites

  1. To run the sample code you must have Visual Studio installed.
  2. To use the Bing Web Search API you need to create a Cognitive Services API account with access to the Bing Search APIs. If you don’t have an Azure subscription, you can create a free account.

Create a new project

To make a Search Request follow the steps below:

  1. Create a Console Application in Visual Studio 2017
  2. Add the following code under Program.
    const string accessKey = "enter key here";
            const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/localbusinesses/search";
            const string searchTerm = "restaurant in new york";
    
            // Returns search results including relevant headers
            struct SearchResult
            {
                public String jsonResult;
                public Dictionary<String, String> relevantHeaders;
            }
    
            static void Main()
            {
                Console.OutputEncoding = System.Text.Encoding.UTF8;
                Console.WriteLine("Searching locally for: " + searchTerm);
    
                SearchResult result = BingLocalSearch(searchTerm, accessKey);
    
                Console.WriteLine("\nHTTP Headers:\n");
                foreach (var header in result.relevantHeaders)
                    Console.WriteLine(header.Key + ": " + header.Value);
    
                Console.WriteLine("\nJSON Response:\n");
                Console.WriteLine(result.jsonResult);
    
                Console.Write("\nPress Enter to exit ");
                Console.ReadLine();
            }
    
            /// <summary>
            /// Performs a Bing Local business search and return the results as a SearchResult.
            /// </summary>
            static SearchResult BingLocalSearch(string searchQuery, string key)
            {
                // Construct the URI of the search request
                var uriQuery = uriBase + "?q=" + Uri.EscapeDataString(searchQuery) + "&mkt=en-us";
    
                // Perform the Web request and get the response
                WebRequest request = HttpWebRequest.Create(uriQuery);
                request.Headers["Ocp-Apim-Subscription-Key"] = key;
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result;
                string json = new StreamReader(response.GetResponseStream()).ReadToEnd();
    
                // Create result object for return
                var searchResult = new SearchResult();
                searchResult.jsonResult = json;
                searchResult.relevantHeaders = new Dictionary<String, String>();
    
                // Extract Bing HTTP headers
                foreach (String header in response.Headers)
                {
                    if (header.StartsWith("BingAPIs-") || header.StartsWith("X-MSEdge-"))
                        searchResult.relevantHeaders[header] = response.Headers[header];
                }
    
                return searchResult;
            }
  3. Replace the const string accesskey= “enter your key here”;  with your subscription key
  4. Run the program

Get the results

The results are shown as the following json. Now you can use the results in your application.

  "_type": "SearchResponse",
  "queryContext": {
    "originalQuery": "restaurant in new york"
  },
  "places": {
    "totalEstimatedMatches": 50,
    "value": [
      {
        "_type": "LocalBusiness",
        "id": "https://api.cognitive.microsoft.com/api/v7/#Places.0",
        "name": "Balthazar Restaurant",
        "url": null,
        "entityPresentationInfo": {
          "entityScenario": "ListItem",
          "entityTypeHints": [
            "Place",
            "LocalBusiness",
            "Restaurant"
          ]
        },
        "geo": {
          "latitude": 40.72276,
          "longitude": -73.99842
        },
        "routablePoint": {
          "latitude": 40.7228355,
          "longitude": -73.99837
        },
        "address": {
          "streetAddress": "80 Spring St",
          "addressLocality": "New York",
          "addressRegion": "NY",
          "postalCode": "10012",
          "addressCountry": "US",
          "neighborhood": "SoHo",
          "text": "80 Spring St, New York, NY, 10012"
        },
        "telephone": "(212) 965-1414"
      },
      {
        "_type": "LocalBusiness",
        "id": "https://api.cognitive.microsoft.com/api/v7/#Places.1",
        "name": "The Odeon",
        "url": null,
        "entityPresentationInfo": {
          "entityScenario": "ListItem",
          "entityTypeHints": [
            "Place",
            "LocalBusiness",
            "Restaurant"
          ]
        },
        "geo": {
          "latitude": 40.71692,
          "longitude": -74.00792
        },
        "routablePoint": {
          "latitude": 40.7169876,
          "longitude": -74.0080643
        },
        "address": {
          "streetAddress": "145 W Broadway",
          "addressLocality": "New York",
          "addressRegion": "NY",
          "postalCode": "10013",
          "addressCountry": "US",
          "neighborhood": "Downtown",
          "text": "145 W Broadway, New York, NY, 10013"
        },
        "telephone": "(212) 233-0507"
      },
...
...

You can find the complete source code in my Github in this repository

Leave a Reply

Your email address will not be published. Required fields are marked *