PHP实现登录搜狐广告获取广告联盟数据的方法【附demo源码】
发布时间:2020-12-12 21:47:01 所属栏目:PHP教程 来源:网络整理
导读:本篇章节讲解PHP实现登录搜狐广告获取广告联盟数据的方法。供大家参考研究具体如下: 一直有一个想法,每次都要登录去看联盟昨天收益多少?每天都要登录和麻烦,能不能做一个汇总发邮件的功能呢? 可惜了,验证码绕不过去,只能想一个办法。先在服
本篇章节讲解PHP实现登录搜狐广告获取广告联盟数据的方法。分享给大家供大家参考,具体如下: 一直有一个想法,每次都要登录去看联盟昨天收益多少?每天都要登录和麻烦,能不能做一个汇总发邮件的功能呢? 可惜了,验证码绕不过去,只能想一个办法。先在服务器手动打一次验证码,然后在通过定时器,每隔10分钟请求一个页面 这样的话Cookies就不会失效,,然后每周只需要跟我汇总数据就Ok了。。 远程提交表单的原理,可以参考: 参考的代码还是一样的如下 获取验证码Code.php 获取数据的页面,这里需要通过表单来提交手动的验证码 url = "http://union.sogou.com/index.action?searchBean.timeSegment=yestoday";
$this->LoginUrl = "http://union.sogou.com/";
$this->PostData = $this->LoginUrl."loginauth.action";
$this->table = "dwz_union";
}
public function post($code)
{
$POST['loginFromPage'] = "homePage";
$POST['username'] = "xxxxxx";
$POST['password'] = "xxxxx";
$POST['activecode'] = $code;
$POST['button.x']="14";
$POST['button.y']="16";
foreach($POST as $key=>$value)
{
$tmp[] = $key."=".$value;
}
$postStr = implode("&",$tmp);
$filedir = SITE_PATH."/TMP/Cookies";
$cookie_file = $filedir."/cookie.txt";
$result = $this->curl($this->PostData,"http://union.sogou.com/loginauth.action",$postStr,$cookie_file);
$url = "http://union.sogou.com/index.action";
$postArr = "searchBean.timeSegment=yestoday";
$response = $this->curl($url," http://union.sogou.com/index.action?pid=dengwz7788",$postArr,$cookie_file);
$this->saveData($response);
}
private function saveData($response)
{
$dom = str_get_html($response);
$tmp = $dom->find('div.rtable table tbody tr',1)->plaintext;
$data = preg_split("/s+/i",$tmp);
$this->link();
$date = date('Y-m-d',strtotime('-1 day'));
$datetime = date('Y-m-d H:i:s');
$money = $data['4'];
$shows = $data['2'];
$times = $data['3'];
$sql = "select sum(money) as total from {$this->table}";
$query = mysql_query($sql);
$totaTmp = mysql_fetch_row($query);
var_dump($totalTmp);
if(empty($totaTmp['0']))
{
$total = $money;
}else{
$total = $totaTmp['0']+$money;
}
$sql = "insert into {$this->table}(date,datetime,money,shows,times,total) values('{$date}','{$datetime}','{$money}','{$shows}','{$times}','{$total}')";
mysql_query($sql);
}
private function link()
{
$link = mysql_connect('localhost','root','');
mysql_select_db('dblog',$link);
mysql_query('SET NAMES utf8');
}
private function saveHtml($infomation,$filedir,$filename)
{
if(!$this->mkdirs($filedir))
{
return 0;
}
$sf = $filedir."/".$filename;
$fp=fopen($sf,"w"); //写方式打开文件
fwrite($fp,$infomation); //存入内容
fclose($fp); //关闭文件
}
//创建目录
private function mkdirs($dir)
{
if(!is_dir($dir))
{
if(!$this->mkdirs(dirname($dir))){
return false;
}
if(!mkdir($dir,0777)){
return false;
}
}
return true;
}
public function login()
{
$filedir = SITE_PATH."/TMP/Cookies";
if(!$this->mkdirs($filedir))
{
echo "目录创建失败";
exit;
}
$cookie_file = $filedir."/cookie.txt";
$ch = curl_init();
curl_setopt($ch,$this->LoginUrl);
curl_setopt($ch,0); //不返回header部分
curl_setopt($ch,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt($ch,$cookie_file);
//curl_setopt($ch,"http://125.89.69.234");
curl_setopt($ch,1);
curl_setopt($ch,"10");
$response = curl_exec($ch);
curl_close($ch);
// 鍏抽棴CURL浼氳瘽
}
private function curl($url,$url2,$fields,$cookie_file)
{
$ch = curl_init();
curl_setopt($ch,$url);
curl_setopt($ch,CURLOPT_POST,CURLOPT_POSTFIELDS,$fields);
curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,CURLOPT_HTTPHEADER,array("Host: union.sogou.com" ));
curl_setopt($ch,$url2);
$response = curl_exec($ch);
//echo curl_error($ch);
curl_close($ch);
return $response;
}
}
$GetData = new GetData();
if(isset($_POST['code']))
{
$GetData->POST($_POST['code']);
}
完整实例代码点击此处本站下载。 更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》、《》、《》及《》 希望本文所述对大家PHP程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |