logo logo

[php] New Bing SERP checker using Windows Azure Datamarket

So, I got email from Bing Developer Team informing that Bing Search API 2.0 will be gone and moving to new platform: Windows Azure Datamarket. On my previous Bing SERP checker project, I’m using Bing Search API 2.0 which means that those codes will no longer working after 1st August.

Windows Azure Datamarket provide a trial package and a free package that has limit for 5000 requests per month. More than enough for us to toying for Bing SERP checker.

Go get your Account Key (was Application ID) : https://datamarket.azure.com/

This is the revised main function php codes so it will work on new Windows Azure Datamarket web service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
error_reporting(E_ALL^E_NOTICE);
 
//this is the main function
function b_serp($keyword, $site, $market, $api_key)
{
	$found=FALSE;
	$theweb='';
	$pos=0;
	$ret=array();
	$limit=1;
 
	$site=str_replace(array('http://'), '', $site);
 
	$context = stream_context_create(array(
		'http' => array(
		    'request_fulluri' => true,
		    'header'  => "Authorization: Basic " . base64_encode($api_key . ":" . $api_key)
			)
		)
	);
 
 
	$pos=1;
	while ((!$found)&&($pos<=100)) {
		$skip=($limit-1)*50;
 
		//this is the end point of microsoft azure datamarket that we should call  -- only take data from web results
		$end_point='https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Web?Query='.urlencode("'".$keyword."'").'&Market='.urlencode("'".$market."'").'&$format=JSON&$top=50&$skip='.$skip;
 
		//I'm using generic file_get_contents because cURL CURLOPT_USERPWD didn't work (no idea why)
		$response=file_get_contents($end_point, 0, $context);
 
		$json_data=json_decode($response);
 
		foreach ($json_data->d->results as $res) {
			$theweb=parse_url($res->Url);
 
			if (substr_count(strtolower($theweb['host']), $site))
		        {
				$found=TRUE;
				$ret['position']=$pos;
				$ret['title']=$res->Title;
				$ret['url']=$res->Url;
		                return $ret;
		        }
 
			$pos++;
		}
 
		$limit++;
	}
 
	if (!$found)
	{
		return NULL;
	}
}

How you call it:

1
2
$account_key='put your account key here';
$res=b_serp('how to upload mp3 to youtube', 'mp32u.net', 'en-US', $account_key);

Example output:

1
2
3
4
5
6
7
8
array(3) {
  ["position"]=>
  int(3)
  ["title"]=>
  string(70) "MP32U.NET - Helping independent artist getting acknowledged in the ..."
  ["url"]=>
  string(21) "http://www.mp32u.net/"
}

Fully working demo: http://demo.ahowto.net/new_bserp/

Actually, there are a lot of other “Search Market” that supported by Bing but I only listed some of them just for example. Download this document for complete list of Bing’s supported Search Market: http://www.bing.com/webmaster/content/developers/ADM_SCHEMA_GUIDE.docx

Notes:

  1. Your PHP version must be PHP 5.2 or higher (because of json_decode command)
  2. You must activate PHP OpenSSL module (php_openssl) because we are using file_get_contents and the webservice end point must be accessed via HTTPS

Update (2012-07-20) : the endpoint URL is changed

bottom

2 Responses to “[php] New Bing SERP checker using Windows Azure Datamarket”

  1. Bmonkiezz says:

    I need to use it.

    I can’t run this script it don’t show something.

    Please send me the full script.

    Thanks you very much.

Trackbacks/Pingbacks

  1. Windows Azure and Cloud Computing Posts for 7/30/2012+ - Windows Azure Blog - [...] post to his How To blog (missed when published): In my previous post [see below], I show[ed] you how …

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

bottom