<?php
 
/**
 * 访问百度************************************************************************************************
 * Enter description here ...
 * @var unknown_type
 */
$header[0] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header[] = "Host: www.baidu.com";
$header[] = "User-Agent: Mozilla/5.0 (Windows NT 6.2; rv:13.0) Gecko/20100101 Firefox/13.0.1";
$header[] = "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3";
$header[] = "Accept-Encoding: gzip, deflate";
$header[] = "Connection: keep-alive"; // browsers keep this blank.
 
$ckfile = tempnam ("./temp", "tem");
 
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.baidu.com");
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; rv:13.0) Gecko/20100101 Firefox/13.0.1');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($curl,CURLOPT_HEADER,1);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt ($curl, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
 
$html = curl_exec($curl); // execute the curl command
curl_close($curl); // close the connection
//echo  $html; // and finally, return $html
 
$cookie_str = file($ckfile);
foreach($cookie_str as $v){
    if(stripos($v,'BAIDUID')!=FALSE){
        $cook = preg_split("/[\s]+/", $v);
//      print_r($cook);
        $result = array_search("BAIDUID", $cook);
        if($result!=FALSE){
            $cookie['BAIDUID']=$cook[$result+1];
        }
    }
}
unset($header);
unset($curl);
//print_r($cookie);
 
/**
 * 关键词搜索 ""************************************************************************************************
 */
$cookir_str = "BAIDUID={$cookie['BAIDUID']}";
$header[0] = "Accept: */*";
$header[] = "Host: www.baidu.com";
$header[] = "User-Agent: Mozilla/5.0 (Windows NT 6.2; rv:13.0) Gecko/20100101 Firefox/13.0.1";
$header[] = "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3";
$header[] = "Accept-Encoding: gzip, deflate";
$header[] = "Connection: keep-alive";
$header[] = "Referer: http://www.baidu.com/";
$header[] = "Cookie: {$cookir_str}"; // browsers keep this blank.
 
$curl = curl_init();
$inputT = mt_rand(500, 10000);
$key_url = "http://www.baidu.com/s?wd=%E5%BF%AB%E4%B9%90%E6%A3%8B%E7%89%8C&rsv_bp=0&rsv_spt=3&inputT={$inputT}";
curl_setopt($curl, CURLOPT_URL, $key_url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; rv:13.0) Gecko/20100101 Firefox/13.0.1');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip, deflate');
//curl_setopt($curl, CURLOPT_HEADER,1);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
//curl_setopt($curl, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($curl, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
 
$html = curl_exec($curl); // execute the curl command
curl_close($curl); // close the connection
 
unset($header);
unset($curl);
 
//匹配百度推广广告链接
if(preg_match_all("/<table id=\"[\d]+\"(.*)<\/table>/Uis", $html, $e_area)){
    $e_url = array();
    foreach ($e_area[0] as $v){ //匹配推广区域
        preg_match_all("/href=\"(.*)\"/Uis", $v, $e_urls);  //匹配推广链接
        $e_url = array_merge($e_url,array_unique($e_urls[1]));
    }
    foreach($e_url as $k=>$v){   //过滤无效的url eg:http://e.baidu.com/?refer=666
        if(strpos($v,'e.baidu.com')!=false){
            unset($e_url[$k]);
        }
    }
}else{
    echo "this key has not ads";
    exit();
}
 
//print_r($e_url);  //广告列表
 
/**
 * 模拟点击广告*********************************************************************************************
 */
$cookir_str = "BAIDUID={$cookie['BAIDUID']}";
$header[0] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header[] = "Host: www.baidu.com";
$header[] = "User-Agent: Mozilla/5.0 (Windows NT 6.2; rv:13.0) Gecko/20100101 Firefox/13.0.1";
$header[] = "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3";
$header[] = "Accept-Encoding: gzip, deflate";
$header[] = "Connection: keep-alive";
$header[] = "Referer: {$key_url}";
$header[] = "Cookie: {$cookir_str}"; // browsers keep this blank.
 
$ckfile2 = tempnam ("./temp", "TEM");
 
$ad_url = $e_url[0];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $ad_url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; rv:13.0) Gecko/20100101 Firefox/13.0.1');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($curl, CURLOPT_HEADER,1);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_REFERER, $key_url);
curl_setopt ($curl, CURLOPT_COOKIEFILE, $ckfile2);  //发送cookie
curl_setopt($curl, CURLOPT_COOKIEJAR, $ckfile2);    //接收cookie
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
 
$html = curl_exec($curl); // execute the curl command
//$headers = curl_getinfo($curl, CURLINFO_HEADER_OUT);
curl_close($curl); // close the connection
 
$headers = header_format($html);
 
$cookie_str = file($ckfile2);
foreach($cookie_str as $v){
    if(stripos($v,'shifen')!=FALSE){
        $cook = preg_split("/[\s]+/", $v);
        foreach($cook as $kk=>$vv){
            if(stripos($vv,'hifen')!=FALSE){
                $cookie[$vv]=$cook[$kk+1];
            }
        }
 
    }
}
//print_r($cookie);
unset($header);
unset($curl);
 
//第二次提交
$cookie_str = '';
foreach ($cookie as $k=>$v){
    $cookie_str .= $k.'='.$v.'; ';
}
$cookie_str = substr($cookie_str, 0,-2);
//echo $cookie_str;
//die();
 
$cookir_str = $cookie_str;
$header[0] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header[] = "Host: www.baidu.com";
$header[] = "User-Agent: Mozilla/5.0 (Windows NT 6.2; rv:13.0) Gecko/20100101 Firefox/13.0.1";
$header[] = "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3";
$header[] = "Accept-Encoding: gzip, deflate";
$header[] = "Connection: keep-alive";
$header[] = "Referer: {$key_url}";
$header[] = "Cookie: {$cookir_str}"; // browsers keep this blank.
 
$ckfile2 = tempnam ("./temp", "TEM");
 
$ad_url = 'http://www.baidu.com'.trim($headers['Location']);
$ckfile3 = tempnam ("./temp", "TEM_");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $ad_url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; rv:13.0) Gecko/20100101 Firefox/13.0.1');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($curl, CURLOPT_HEADER,1);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_REFERER, $key_url);
curl_setopt ($curl, CURLOPT_COOKIEFILE, $ckfile);   //发送cookie
curl_setopt($curl, CURLOPT_COOKIEJAR, $ckfile3);    //接收cookie
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
 
$html = curl_exec($curl); // execute the curl command
//$headers = curl_getinfo($curl, CURLINFO_HEADER_OUT);
curl_close($curl); // close the connection
 
echo $html;
exit();
 
function header_format($str){
    $header_arr = array();
    $harr = explode("\n", $str);
    foreach($harr as $k=>$v){
        $tem_arr = explode(':', $v,2);
        if(count($tem_arr)==1){
            if(strpos($v, '200')){
                $header_arr['status code'] = 200;
            }else if(strpos($v, '302')){
                $header_arr['status code'] = 302;
            }
        }else{
            $header_arr[$tem_arr[0]] = $tem_arr[1];
        }
    }
    return $header_arr;
}
?>