Check website rank in Bing SERP using Bing Search API ‏ 2

A warning before you proceed:
as stated on Bing API 2.0 documentation: “You can not Use API results for search engine optimization (SEO). In particular, using the API for rank checks is explicitly prohibited.”

So, use my code on your own risk!

Last time, we have covered on how to use Google AJAX Search API to check Google SERP, now, we will utilize Bing API to check SERP on Bing search engine.

You need to signup for API key here: https://ssl.bing.com/webmaster/developers/appids.aspx
I’ve written the working code here:

error_reporting(E_ALL^E_NOTICE);

//helper function -- file_get_contents using curl
function file_get_contents_curl($url, $referer='', $ua='')
    {
    $ch=curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    if ($referer!='') {
        curl_setopt($ch, CURLOPT_REFERER, $referer);
    }

    if ($ua!='') {
		curl_setopt($ch, CURLOPT_USERAGENT, $ua);
	}

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $data=curl_exec($ch);
    curl_close ($ch);

    return $data;
    }

//this is the main function
function b_serp($keyword, $site, $api_key)
{
	$first=TRUE;
	$found=FALSE;
	$prev_offset=0;
	$current_offset=1;
	$theweb='';
	$pos=0;
	$ret=array();
	$limit=1;

    $site   =str_replace(array
        (
        'http://',
        'www.'
        ),               '',
                         $site);

    $target_url='http://api.search.live.net/json.aspx?Appid='.$api_key.'&query='.urlencode($keyword).'&sources=web&web.count=50&web.offset=0';

    $response=file_get_contents_curl($target_url, '', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.12) Gecko/20101027 Ubuntu/10.04 (lucid) Firefox/3.6.12 GTB7.1');
	$json_data=json_decode($response);
	//var_dump($target_url, $response); exit();
	$prev_offset=$json_data->SearchResponse->Web->Offset;

	$pos=1;
	foreach ($json_data->SearchResponse->Web->Results as $res)
	{
		$theweb=parse_url($res->Url);
		//var_dump($pos, $theweb['host'], $site, $res);
		if (substr_count(strtolower($theweb['host']), $site))
        {
			$found=TRUE;
			$ret['position']=$pos;
			$ret['title']=$res->Title;
			$ret['url']=$res->Url;
            return $ret;
        }

        $pos++;
	}

	while ($current_offset!=$prev_offset)
	{
		if ($first) {
			$prev_offset=0;
			$current_offset=0;
			$first=FALSE;
		}

		$prev_offset=$current_offset;

		$target_url='http://api.search.live.net/json.aspx?Appid='.$api_key.'&query='.urlencode($keyword).'&sources=web&web.count=50&web.offset='.($limit*50+1);

		$response=file_get_contents_curl($target_url, '', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.12) Gecko/20101027 Ubuntu/10.04 (lucid) Firefox/3.6.12 GTB7.1');
		$json_data=json_decode($response);
		//var_dump($target_url, $response);
		$current_offset=$json_data->SearchResponse->Web->Offset;

		foreach ($json_data->SearchResponse->Web->Results as $res)
		{
			$theweb=parse_url($res->Url);
			//var_dump($pos, $theweb['host'], $site);
			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;
	}
}

Example usage 

    //example usage
    $bing_key='change this to your own API key';  // Your BING API key here
    $site='www.mp32u.net';
    $keyword='how to upload mp3 to youtube';

    var_dump(b_serp( $keyword, $site, $bing_key));

Example output:

    
array(3) {
      ["position"]=>
      int(5)
      ["title"]=>
      string(34) "MP32U.NET - Create simple video from your MP3 and a JPG image"
      ["url"]=>
      string(21) "http://www.mp32u.net/"
    }

Apparently mp32u.net rank better in Bing SERP than in Google SERP for keyword: how to upload mp3 to Youtube

Fully working demo can be tested here: http://demo.ahowto.net/bserp/

  1. i had run the above script.it is getting error.

  2. Nice piece of work, can you add other SE as well such as yahoo and google?

Leave a Comment


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

Trackbacks and Pingbacks: