<?php
ob_implicit_flush();
include "../Lib/Include.php";
$Domain=$_GET["S"];
$Type=$_GET["T"];
switch ($Type){
    case 1:
        $GoogleCount=GetGoogle($Domain,0);
        echo $GoogleCount;
        break;
    case 2:
        $BaiduCount=GetBaidu($Domain,0);
        echo $BaiduCount;
        break;
    case 3:
        $BingCount=GetBing($Domain,0);
        echo $BingCount;
        break;
    case 4:
        $YahooCount=GetYahoo($Domain,0);
        echo $YahooCount;
        break;
    case 5:
        $SosoCount=GetSoso($Domain,0);
        echo $SosoCount;
        break;
    case 6:
        $YoudaoCount=GetYoudao($Domain,0);
        echo $YoudaoCount;
        break;
    case 7:
        $SogouCount=GetSogou($Domain,0);
        echo $SogouCount;
        break;
    case 11:
        $GoogleLCount=GetGoogle($Domain,1);
        echo $GoogleLCount;
        break;
    case 12:
        $BaiduLCount=GetBaidu($Domain,1);
        echo $BaiduLCount;
        break;
    case 13:
        $BingLCount=GetBing($Domain,1);
        echo $BingLCount;
        break;
    case 14:
        $YahooLCount=GetYahoo($Domain,1);
        echo $YahooLCount;
        break;
    case 15:
        $SosoLCount=GetSoso($Domain,1);
        echo $SosoLCount;
        break;
    case 16:
        $YoudaoLCount=GetYoudao($Domain,1);
        echo $YoudaoLCount;
        break;
    case 17:
        $SogouLCount=GetSogou($Domain,1);
        echo $SogouLCount;
        break;
    case 21:
        $SogouPCount=GetSogou($Domain,3);
        echo $SogouPCount;
        break;
    default:
        echo "淇℃伅镆ヨ鍑洪敊";
}

//銮峰彇鐧惧害鏀跺綍
function GetBaidu($url,$type){
	$fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /s?wd=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /s?wd=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.baidu.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp); 
		$data=mb_convert_encoding($data,"UTF-8","gb2312");
		$Arr=spliti("鐧惧害涓€涓嬶紝镓惧埌鐩稿叧缃戦〉",$data);
		$Arr=spliti("绡?,$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}
//銮峰彇璋锋瓕鏀跺綍
function GetGoogle($url,$type){
	$fp = fsockopen("www.google.cn", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /search?q=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /search?q=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.google.cn\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp); 
		$data=mb_convert_encoding($data,"UTF-8","gb2312");
		$Arr=spliti("銮峰缑绾?<b>",$data);
		$Arr=spliti("</b> 鏉＄粨鏋?,$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}
//銮峰彇寰蒋BING鏀跺綍
function GetBing($url,$type){
	$fp = fsockopen("www.bing.com", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /search?q=site%3A".$url."&mkt=zh-CN HTTP/1.1\r\n" ;
		}else{
			$out = "GET /search?q=link%3A".$url."&mkt=zh-CN HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.bing.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp);
		$Arr=spliti("鏉＄粨鏋滐紝鍏?",$data);
		$Arr=spliti(" 鏉?,$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}
//銮峰彇板呰柠鏀跺綍
function GetYahoo($url,$type){
	$fp = fsockopen("www.yahoo.cn", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /s?p=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /s?p=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.yahoo.cn\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp); 
		$Arr=spliti("镓惧埌鐩稿叧缃戦〉绾?,$data);
		$Arr=spliti("鏉?,$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}
//銮峰彇鎼沧悳鏀跺綍
function GetSoso($url,$type){
	$fp = fsockopen("www.soso.com", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /q?w=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /q?w=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.soso.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp); 
		$data=mb_convert_encoding($data,"UTF-8","gb2312");
		$Arr=spliti("鎼灭储鍒扮害",$data);
		$Arr=spliti("椤圭粨鏋?,$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}
//銮峰彇链夐亾鏀跺綍
function GetYoudao($url,$type){
	$fp = fsockopen("www.youdao.com", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /search?q=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /search?q=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.youdao.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp); 
		$Arr=spliti("<span class=\"fr\">鍏?,$data);
		$Arr=spliti("鏉＄粨鏋?,$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}
//銮峰彇鎼灭嫍鏀跺綍
function GetSogou($url,$type){
	$fp = fsockopen("www.sogou.com", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /web?query=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /web?query=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.sogou.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp); 
		$data=mb_convert_encoding($data,"UTF-8","gb2312");
		if($type==3){
			$Arr=spliti("鎼灭嫍璇勭骇:",$data);
			$Arr=spliti("&nbsp;",$Arr[1]);
			if($Arr[0]!="")
				return $Arr[0];
			else
				return "-";
		}else{
			$Arr=spliti("<!--resultbarnum:",$data);
			$Arr=spliti("-->",$Arr[1]);
			if($Arr[0]!="")
				return $Arr[0];
			else
				return "-";
		}
	}
}
?>