经典,完美抓取页面程序原理

<?php
/*
描述:从0a上抓取站点信息
作者:xuduowei
日期:2011.02.12
*/
class getInfoController extends tcoaBaseController{

protected $getInfoModel;
public function set_getInfoModel($getInfoModel){
$this->getInfoModel=$getInfoModel;
}
//载入checkSessionModel模型,方便后面调用里面的方法,如:域名列表显示
protected $checkSessionModel;
public function set_checkSessionModel($checkSessionModel){
$this->checkSessionModel=$checkSessionModel;
}

//jquery调用方法
public function getInfo(){
$domain=trim($_POST[‘domain’]);
$domainarr=$this->getInfoModel->listsite($domain);
$old_timeout=ini_get(‘default_socket_timeout’);
ini_set(‘default_socket_timeout’,15); //设置file_get_contents 取值超时,当前是15秒

foreach($domainarr as $domainname){
$ftpdomain=$domainname[‘ftp_domain’];
$siteipold=$domainname[‘siteip’];
$ftp_userold=$domainname[‘ftp_user’];
$ftp_passold=$domainname[‘ftp_pass’];
$db_hostold=$domainname[‘db_host’];
$db_userold=$domainname[‘db_user’];
$db_passold=$domainname[‘db_pass’];
$serveripold=$domainname[‘serverip’];

$domainarr=explode(“www.”,$ftpdomain);
$begainurl=Project::singleton()->getConfig(‘customConfig.getInterfaceURL’);//其中getInterfaceURL为tcoaBaseController中的一个自定义变量(接口路径)。
$url=$begainurl.$domainarr[1];

$jsoncon=””;
$i=0;
if(function_exists(“file_get_contents”)){
while($i<3&&empty($jsoncon)){
$i++;
$jsoncon=file_get_contents($url);
}
}elseif(function_exists(‘curl_init’)){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$jsoncon = curl_exec ($ch);
curl_close ($ch);
}
if(!empty($jsoncon)){
// $jsoncon=file_get_contents($url);//若直接这样写的话,不是很完美。参看行28-39
$ans = json_decode($jsoncon,true);
$siteip=$ans[“ip”];
$ftpuser=$ans[“ftpuser”];
$ftppwd=$ans[“ftppwd”];

$dbuser=$ans[“dbuser”];
$dbpwd=$ans[“dbpwd”];
$db=$ans[“db”];
$siteip2=$siteip;
$serverip=$ans[“server”];
//从oa上获得信息后,按域名更新相应的信息。
$this->getInfoModel->updateinfo($siteip2,$ftpuser,$ftppwd,$siteip2,$dbuser,$dbpwd,$db,$serverip,$ftpdomain);
if(!empty($ftpdomain)){
if($siteipold==$siteip){
$str.=”成功更改:”.$ftpdomain.”–原子站ip:”.$siteipold.”–<font color=red>更改后:”.$siteip.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原ftp用户名:”.$ftp_userold.”–<font color=red>更改后:”.$ftpuser.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原ftp密码:”.$ftp_passold.”–<font color=red>更改后:”.$ftppwd.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原数据库用户名:”.$db_userold.”–<font color=red>更改后:”.$dbuser.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原数据库密码:”.$db_passold.”–<font color=red>更改后:”.$dbpwd.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原服务器ip:”.$serveripold.”–<font color=red>更改后:”.$serverip.”</font><br/>”;
echo $str;
}else{
$str.=”成功更改:”.$ftpdomain.”–原子站ip:<font color=blue>”.$siteipold.”</font>–<font color=red>更改后:”.$siteip.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原ftp用户名:<font color=blue>”.$ftp_userold.”</font>–<font color=red>更改后:”.$ftpuser.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原ftp密码:<font color=blue>”.$ftp_passold.”</font>–<font color=red>更改后:”.$ftppwd.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原数据库用户名:<font color=blue>”.$db_userold.”</font>–<font color=red>更改后:”.$dbuser.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原数据库密码:<font color=blue>”.$db_passold.”</font>–<font color=red>更改后:”.$dbpwd.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原服务器ip:<font color=blue>”.$serveripold.”</font>–<font color=red>更改后:”.$serverip.”</font><br/>”;
echo $str;
}
}
}else{
$str=”<font color=red>更改失败!!!!!!!</font>”.$ftpdomain.”–原ip:”.$siteipold.”<br/>”;
echo $str;
}
if(!empty($ftpdomain)){
$this->insertLog($ftpdomain,$str);
}
}//结束foreach
ini_set(‘default_socket_timeout’,$old_timeout);
}//结束function

//视图方法
public function showGetInfo(){
$this->getInfo();
$View=ViewDepository::singleton()->imLoad(‘./tcoaView/getInfoView.php’);
$View->showtypename=$this->checkSessionModel->listtypename();
echo $View->render();
}

//写日志方法
public function insertLog($domain,$str){
$time=time();
$group_id= $this->userName.”_”.date(“YnjHis”);
//$tj=” group_id=’$group_id’,domain=’$domain’,description=’$str’,checktime=’$time'”;
$arrdomain=$this->getInfoModel->insertlog($group_id,$domain,$str,$time);
}
}
?>


关注公众号,了解更多it技术(it问答网

发表评论

电子邮件地址不会被公开。