<html>
<head>


<style>
body {
  font-weight : normal;
  font-size : 12px;
  font-family : helvetica;
  text-decoration : bold;
  background : #f3f3f3;
}

a:hover {
  font-weight : normal;
  font-size : 12px;
  font-family : helvetica;
  background : #989898;
  text-decoration : bold;
}

a:visited, a:link, a:active {
  font-weight : normal;
  font-size : 12px;
  font-family : helvetica;
  color : #000022;
  text-decoration : normal;
}  
</style>


</head>

<body>

<b>Bing</b>
<br>Keyword: <?php echo $_GET["keyword"];?>
<br>Showing <b>10</b> results 
<br><hr>
<?php

function getBaseURL($url){
   list($part1, $part2) = split("://", $url);
   list($part3, $part4) = split("/", $part2);   
#   $baseurl =  $part1 . "://" . $part3;  
   $baseurl =  $part3;  
   return $baseurl;
}


function getBingSERP($mykeyword){
    $reg_ex = "[[:space:]]";
    $replace_word = "+"; 
    $str = $mykeyword;
    $mykeyword = ereg_replace($reg_ex, $replace_word, $str);

    $url = "http://m.bing.com/search/search.aspx?A=webresults&Q=$mykeyword&D=Web&SI=0&PN=0&SCO=0";
            
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "c:\cookie.txt");
    $client = $_SERVER['HTTP_USER_AGENT'];
    curl_setopt($ch, CURLOPT_USERAGENT, "$client");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = curl_exec($ch);
    curl_close($ch); 
    return $output;
}


function processSERP($serp, $masterCnt) {
   $dom = new DOMDocument();
   @$dom->loadHTML($serp);

   $xpath = new DOMXPath($dom);
   $hrefs = $xpath->evaluate("/html/body//a");
   $sofar = "";

   for ($i = 0; $i < $hrefs->length; $i++) {


      $href = $hrefs->item($i);
      $url = $href->getAttribute('href');
      //print "$url"; 
      

      $anchortext = $href->nodeValue; 


      //<div class="s15">
      //<a class="Link" 
      //   href="/search/search.aspx?A=redirect&Q=test&D=Web&EXP=&ID=0&REDIRURL=http%3a%2f%2fwww.test.com%2f&RC=1&RID=4752f4ba79f24d73946554926184d87e&MOBWEB=0&HM=ihPaFOVr">
      //  /search/search.aspx?A=redirect&Q=test&D=Web&EXP=&ID=0&REDIRURL=http://www.test.com/&RC=1&RID=e9fd1f0953a040e0a89e8d7a3fcdfed9&MOBWEB=0&HM=ihPaFOVr
      // :%3a /%2f 
      // REDIRURL=
      // &RC


      if (preg_match("/http/i", "$url")) {
         $splitdata = explode('http', $url);
         $splitdata = explode('RC', $splitdata[1]);
         $splitdata[0]= substr_replace($splitdata[0] ,"",-1);
         $splitdata[0] = str_replace('%3a',':',$splitdata[0]);
         $splitdata[0] = 'http'.str_replace('%2f','/',$splitdata[0]);


         $baseurl = getBaseURL($splitdata[0]);
         $urlChunks = spliti (" ", $_GET["keyword"]);

         foreach ($urlChunks as $chunk) {
            $highChunk = '<B>'.$chunk.'</B>';
            $baseurl = str_replace("$chunk", "$highChunk", $baseurl);
         }
  

         if (preg_match("/bing.com/i", "$splitdata[0]")) { 
         } else {
            if (preg_match("/live.com/i", "$splitdata[0]")) { 

            } else {



         ?>

          <a target=detailsframe href='kw.php?url=<?php echo $splitdata[0]?>&keyword=<?php echo $_GET['keyword']?>' title='<?php echo $anchortext?>'><?php echo $baseurl?>
          </a><br>

      <?php     

           }
        }  
      
     }  



   }

   return $masterCnt;
}

$masterCnt = 0;

$keyword = $_GET["keyword"];
$serpRes = getBingSERP($keyword);

$masterCnt = processSERP($serpRes, $masterCnt);
flush();

if($masterCnt<$rowLimit) {
   sleep(rand(1, 3));
   $next = $first+10;
   sleep(rand(2, 6));
   $masterCnt = processSERP($serpRes, $masterCnt, $rowLimit);
   flush();
}


?>

</body>
</html>