#Bing #Image search from C# or #PHP
EDIT: NB: This is no longer supported, please see this instead;
https://blog.dotnetframework.org/2017/04/24/bing-image-search-using-cognitive-search/
If you want to get an image to represent an arbitrary string in Javascript, then I would recommend the Google Image Search API, but if you want to do it server-side, then I’d recommend instead, the Bing Image search API, which offers 5,000 lookups per month for free. – you’ll need to get a key here : https://datamarket.azure.com/dataset/bing/search before you can use this code.
using System;
using System.Collections.Generic;
using System.Net;
using System.Web.Script.Serialization;///
/// Summary description for BingImageSearch
///public class BingImageSearch
{
public static string GetImage(string searchText)
{
var strUrl = “https://api.datamarket.azure.com/Bing/Search/v1/Image?Query= ‘ ” + searchText + “‘&$format=json “;
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(“{{KEY}}“, “{{KEY}}“);
var strJson = wc.DownloadString(strUrl);
var oBingSearch = JavascriptDeserialize(strJson);
if (oBingSearch.d.results.Count == 0) return “”;
return oBingSearch.d.results[0].MediaUrl;
}
public static T JavascriptDeserialize(string json)
{
var jsSerializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue };
return jsSerializer.Deserialize(json);
}public class Thumbnail
{public string MediaUrl { get; set; }
public string ContentType { get; set; }
public string Width { get; set; }
public string Height { get; set; }
public string FileSize { get; set; }
}public class Result
{
public string ID { get; set; }
public string Title { get; set; }
public string MediaUrl { get; set; }
public string SourceUrl { get; set; }
public string DisplayUrl { get; set; }
public string Width { get; set; }
public string Height { get; set; }
public string FileSize { get; set; }
public string ContentType { get; set; }
public Thumbnail Thumbnail { get; set; }
}public class D
{
public List results { get; set; }
public string __next { get; set; }
}public class BingSearchObject
{
public D d { get; set; }
}
}
What this code does, is that it makes an authenticated HTTP request to azure datamarket, where the Bing API resides. The $format=json specifier is used to ask for the response in JSON format rather than XML.
The JavaScriptSerializer, and the various classes below map the JSON to a .NET object, so you can easily access properties using dot notation. Here, we check that the results count is more than zero, to ensure that there is a valid response and returning an empty string as an error condition rather than throwing an exception.
If, however, you want to use PHP to call the API instead of c#, then here is the equivalent code in PHP
function GetImageFromBing($searchTerm)
{
$URL = “https://api.datamarket.azure.com/Bing/Search/v1/Image?Query='” . urlencode($searchTerm) . “‘&\$format=json”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_USERPWD, “{{YOUR KEY}}:{{YOUR KEY}}“);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$return = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
echo $error;
$oBingSearch = json_decode($return,true);
$MediaUrl = $oBingSearch[‘d’][‘results’][0][‘MediaUrl’];
return $MediaUrl;
}header( ‘Location: ‘ . GetImageFromBing(‘red cars’) ) ;
Here, the process is similar to the C# version, however, there is no classes to define the structure of the JSON, so it’s much more loosely typed, but still perfectly usable. One of the ‘Gotya’s’ I found when writing this was the error:
SSL certificate problem: unable to get local issuer certificate
Which is probably down to the use of TLS 1.2 on Azure, but an easy way to get around this is to skip the HTTPS verification process by setting CURLOPT_SSL_VERIFYPEER to false.