<?php





//主函数





 

 //error_reporting(0);



function murl($qid,$config){



//echo $web_key;

$aaurl=$qid;

$cid3=$qid;

$cid6=substr(md5($qid),0,$config['web_key']);

 

//echo $aaurl;

if($config['key_true']){



$aaurl=md5($aaurl);

}else{



$aaurl=$aaurl.'.html';

}

 



if($config['url_index']){

$uurl="/read/".$aaurl;

}else{



$uurl="/index.php/read?q=".$aaurl;

}

      





return $uurl;

}









function ccu($qid,$config){



//echo $web_key;

 

 



if($config['url_index']){

$uurl="/".$qid.'/';

}else{



$uurl="/index.php/page?c=".$qid;

}

      





return $uurl;

}





function encrypt($data, $key) { 

     $prep_code = serialize($data); 

     $block = mcrypt_get_block_size('des', 'ecb'); 

     if (($pad = $block - (strlen($prep_code) % $block)) < $block) { 

     $prep_code .= str_repeat(chr($pad), $pad); 

     } 

     $encrypt = mcrypt_encrypt(MCRYPT_DES, $key, $prep_code, MCRYPT_MODE_ECB); 

     return base64_encode($encrypt); 

} 

  

 

 function decrypt($str, $key) { 

    $str = base64_decode($str); 

    $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB); 

    $block = mcrypt_get_block_size('des', 'ecb'); 

    $pad = ord($str[($len = strlen($str)) - 1]); 

    if ($pad && $pad < $block && preg_match('/' . chr($pad) . '{' . $pad . '}$/', $str)) { 

    $str = substr($str, 0, strlen($str) - $pad); 

    } 

    return unserialize($str); 

} 







function curl_https($url, $data=array(), $header=array(), $timeout=30){  

     $ch = curl_init();  

     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查  

     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在  

     curl_setopt($ch, CURLOPT_URL, $url);  

	

 $header =array("Host:zhidao.baidu.com","Content-Type:application/x-www-form-urlencoded","Connection: keep-alive",'Referer:http://www.baidu.com','User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; BIDUBrowser 2.6)');

  

  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);



     curl_setopt($ch, CURLOPT_POST, true);  



     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));  

     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   

     curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);  

   curl_setopt($ch,CURLOPT_COOKIE,'Hm_lpvt_6859ce5aaf00fb00387e6434e4fcc925=1528897780; IKUT=787; Hm_lvt_6859ce5aaf00fb00387e6434e4fcc925=1528895737,1528896942; BAIDUID=AB6A640364215C33934B0B48C061256D:FG=1; BIDUPSID=7141BE6AEA656AC574B137E3B2B509A9; PSTM=1528553489; FP_UID=a790709ad43dffbda49c004c49eee0eb');  



     $response = curl_exec($ch);  

   

     if($error=curl_error($ch)){  

         die($error);  

     }  

   

     curl_close($ch);  

   

     return $response;  

   

 }  





	



function auto_C($url){



$t=file_get_contents("http://".$url);



if($t==''){



//echo "1";



$t=file_get_contents("https://".$url);







if($t==''){







//echo "2";



$t=nanji_url("http://".$url);







if($t==''){



//echo "3";



$t=nanji_url("https://".$url);







if($t==''){



//echo "4";



$t=curl_https("http://".$url);







if($t==''){



//echo "5";



$t=curl_https("https://".$url);



if ($t==''){



//echo "6";



}







}











}















}







}







} 







return $t;







}







function nanji_url($url){







$ch = curl_init();  



$timeout = 5;  



curl_setopt ($ch, CURLOPT_URL, $url);  



curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  



curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  



$file_contents = curl_exec($ch);  



curl_close($ch);  



return $file_contents;  



}







function c_ji($url){











	$ch=curl_init();



	curl_setopt($ch,CURLOPT_URL,$url);



	$user_agent="Baiduspider+(+http://www.baidu.com/search/spider.htm)";



	curl_setopt($ch,CURLOPT_USERAGENT,$user_agent);



	curl_setopt($ch,CURLOPT_REFERER,"http://www.baidu.com/");



	curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);



	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);



	$nanji_tempz=curl_exec($ch);



	curl_close($ch);



	return $nanji_tempz;

















}



function curl($url){

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5000);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4'));

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        $contents = curl_exec($ch);

        curl_close($ch);//关闭一打开的会话

        return $contents;

    }





function nanji_cut($start,$end,$str){



	$temp=explode($start,$str,2);



	$content=explode($end,$temp[1],2);



	return $content[0];



}



function nanji_split($start,$end,$str){



	$temp=preg_split($start,$str);



	$content=preg_split($end,$temp[1]);



	return $content[0];



}





class Curlcontent{



protected function _GetContent( $url )

    {

   

        $this->ch = curl_init();

        $this->ip = '220.181.108.'.rand(1,255);  // 百度蜘蛛

        $this->timeout = 15;

        curl_setopt($this->ch,CURLOPT_URL,$url);

        curl_setopt($this->ch,CURLOPT_TIMEOUT,0);

        //伪造百度蜘蛛IP  

        curl_setopt($this->ch,CURLOPT_HTTPHEADER,array('X-FORWARDED-FOR:'.$this->ip.'','CLIENT-IP:'.$this->ip.'')); 

        //伪造百度蜘蛛头部

        curl_setopt($this->ch,CURLOPT_USERAGENT,"Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");

        curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1);

        curl_setopt($this->ch,CURLOPT_HEADER,0);

        curl_setopt($this->ch,CURLOPT_CONNECTTIMEOUT,$this->timeout);

        curl_setopt($this->ch,CURLOPT_SSL_VERIFYPEER,false);

        $content = curl_exec($this->ch);



        if($content === false)

        {//输出错误信息

            $no = curl_errno($this->ch);

            switch(trim($no))

            {

                case 28 : $this->error = '访问目标地址超时'; break;

                default : $this->error = curl_error($this->ch); break;

            }

            echo $this->error;

        }

        else

        {

            $this->succ = true;

            return $content;

        }

    }

    public  function getcurl($url){

    return $this->_GetContent($url);

    }

}







class ComBaike{

    private $o_String=NULL;

    public function __construct(){

        include('cls.StringEx.php');

        $this->o_String=new StringEx();

    }

    public function getItem($word){

        $url = "http://www.baidu.com/s?wd=".$word;

        // 构造包头，模拟浏览器请求

        $header = array (

            "Host:www.baidu.com",

            "Content-Type:application/x-www-form-urlencoded",//post请求

            "Connection: keep-alive",

            'Referer:http://www.baidu.com',

            'User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; BIDUBrowser 2.6)'

        );

        $ch = curl_init ();

        curl_setopt ( $ch, CURLOPT_URL, $url );

        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );

        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );

        $content = curl_exec ( $ch );

        if ($content == FALSE) {

        echo "error:" . curl_error ( $ch );

        }

        curl_close ( $ch );

        //输出结果echo $content;

        $this->o_String->string=$content;

        $s_begin='<div id="rs">';

        $s_end='</div>';

        $summary=$this->o_String->getPart($s_begin,$s_end);

        $s_begin='<div class="tt">相关搜索</div><table cellpadding="0"><tr><th>';

        $s_end='</th></tr></table></div>';

        $content=$this->o_String->getPart($s_begin,$s_end);

        return $content;

    }

    public function __destruct(){

        unset($this->o_String);    

    }

}





?>