X
    Categories: PHP

bing image search api php

Bing image search api php

Hi friends, In this tutorial I am going to explain about how the bing image search api will work in php. To get Bing images you need to register in Bing Api and have to get API key to get Image results.

Bing Providing following Search Queries :

1. Query bing image api.

2. Query bing web api.

3. Query bing video api.

4. Query bing news api.

5. Query bing released search api.

6. Query bing spelling suggestions api.

URL for Bing searc API : https://api.datamarket.azure.com/Bing/Search/v1/

By default Bing will return 50 results per request.

1. Query bing image api : Which is going to provide the result of images. Per request we will get 50 images.

2. Query bing web api : Which is going to provide the result of web.

3. Query bing video api : Which is going to provide the result of videos. Per request we will get 50 videos.

4. Query bing news api : Which is going to provide the result of News.

5. Query bing released search api : Which is going to provide the related results of searc term.

6. Query bing spelling suggestions api : Which is going to provide the suggestion for misspelled term.

Example :


BingSearch.php

<?php 

	/*
	 * Simple PHP 5 cURL Wrapper for the Bing Search API via Windows Azure Marketplace
	 * @author Daniel Boorn info@rapiddigitalllc.com
	 * @copyright 2012 Daniel Boorn
	 * @bingapidoc https://datamarket.azure.com/dataset/bing/search#schema
	 * @license apache 2.0, code is distributed "as is", use at own risk, all rights reserved
	 */
	class BingSearch{
		
		protected $apiKey = '4xWgh8**************************eg5GYAAV0=';
		protected $apiRoot = 'https://api.datamarket.azure.com/Bing/Search/v1/';
		
		/*
		 * construct class
		 * @param string $apiKey (optional)
		 * @throws exception with no api key
		 */
		public function BingSearch($apiKey=false){
			if($apiKey) $this->apiKey = $apiKey;
			if($this->apiKey=="") throw new Exception("API Key Required");
		}

		/*
		 * query bing image api
		 * @param mixed (string or array) $query
		 * @return object
		 */
		public function queryImage($query){
			return $this->query('Image',$query);
		}
		
		/*
		 * query bing web api
		 * @param mixed (string or array) $query
		 * @return object
		 */
		public function queryWeb($query){
			return $this->query('Web',$query);
		}

		/*
		 * query bing video api
		 * @param mixed (string or array) $query
		 * @return object
		 */
		public function queryVideo($query){
			return $this->query('Video',$query);
		}
		
		/*
		 * query bing news api
		 * @param mixed (string or array) $query
		 * @return object
		 */
		public function queryNews($query){
			return $this->query('News',$query);
		}
		
		/*
		 * query bing released search api
		 * @param mixed (string or array) $query
		 * @return object
		 */
		public function queryRelatedSearch($query){
			return $this->query('RelatedSearch',$query);
		}
		
		/*
		 * query bing spelling suggestions api
		 * @param mixed (string or array) $query
		 * @return object
		 */
		public function querySpellingSuggestions($query){
			return $this->query('SpellingSuggestions',$query);
		}
		
		/*
		 * query bing api
		 * @param string $type (see api specs)
		 * @param mixed (string or array) $query
		 * @return object
		 */
		public function query($type,$query){
			if(!is_array($query)) $query = array('Query'=>"'{$query}'");
			try{
				print_r(self::getJSON("{$this->apiRoot}{$type}",$query));
				exit;
				return self::getJSON("{$this->apiRoot}{$type}",$query);
			}catch(Exception $e){
				die("<pre>{$e}</pre>");
			}
		}
		
		/*
		 * get json via curl with basic auth
		 * @param string $url
		 * @param array $data
		 * @return object
		 * @throws exception on non-json response (api error)
		 */
		protected function getJSON($url,$data){
			if(!is_array($data)) throw new Exception("Query Data Not Valid. Type Array Required");
			$data['$format'] = 'json';
			$url .= '?' . http_build_query($data);
			
			$ch = curl_init($url);
		
			curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		    curl_setopt($ch, CURLOPT_USERPWD,  $this->apiKey . ":" . $this->apiKey);
		    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		    $r = curl_exec($ch);
		    $json = json_decode($r);
		    if($json==null) throw new Exception("Bad Response: {$r}\n\n{$url}");
		    return $json;
		}
		
		
	}

Index.php

<?php 

	/*
	 * sample example code for BingSearch.php class
	 * @author Daniel Boorn info@rapiddigitalllc.com
	 * @license apache 2.0
	 * @bingapiurl https://datamarket.azure.com/dataset/bing/search#schema
	 */


	ini_set('display_errors','1');
	require('BingSearch.php');
	
	//register for key on windows azure
	$apiKey = '4xWghqIH8s****************************izeg5GYAAV0=';
	
	$bing = new BingSearch($apiKey);
	
	# Example 1: simple image search
	echo '<pre>';
	$r = $bing->querySpellingSuggestions('testse');
	var_dump($r);

	
	# Example 2: advanced image search
	//https://datamarket.azure.com/dataset/bing/search#schema
	//be sure to respect the data types in the values

	$r = $bing->queryImage(array(
		'Query'=>"'xbox'",//string
		'Adult'=>"'Moderate'",//string
		'ImageFilters'=>"'Size:Small+Aspect:Square'",//string
		'Latitude'=>47.603450,//double
		'Longitude'=>-122.329696,//double
		'Market'=>"'en-US'",//string
		'Options'=>"'EnableHighlighting'",//string
	));
	var_dump($r);
	
	
	//repeat examples for other search types...

 

Microsoft have announced they are replacing the Bing search api that we are currently using  with a new version, this will happen from December 9th. Here you can find a new Version on bing API v5.

Hope this tutorial will help someone.

Happy coding.

If you like this article please don’t forget to share and comment.

Thanks!.

Marimuthu:

View Comments