PHP实现登录搜狐广告获取广告联盟数据的方法【附demo源码】
程序员文章站
2024-03-01 23:27:04
本文实例讲述了php实现登录搜狐广告获取广告联盟数据的方法。分享给大家供大家参考,具体如下:
一直有一个想法,每次都要登录去看联盟昨天收益多少?每天都要登录和麻烦,能不能...
本文实例讲述了php实现登录搜狐广告获取广告联盟数据的方法。分享给大家供大家参考,具体如下:
一直有一个想法,每次都要登录去看联盟昨天收益多少?每天都要登录和麻烦,能不能做一个汇总发邮件的功能呢?
可惜了,验证码绕不过去,只能想一个办法。先在服务器手动打一次验证码,然后在通过定时器,每隔10分钟请求一个页面
这样的话cookies就不会失效,,然后每周只需要跟我汇总数据就ok了。。
远程提交表单的原理,可以参考:php基于curl后台远程登录正方教务系统的方法
参考的代码还是一样的如下
获取验证码code.php
define("site_path", $_server['document_root']); $loginurl = "http://union.sogou.com/"; $url = $loginurl."validatecode"; $filedir = site_path."/tmp/cookies"; $cookie_file = $filedir."/cookie.txt"; if(!mkdirs($filedir)) { echo "目录创建失败"; exit; } $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_header, 0); //不返回header部分 curl_setopt($ch, curlopt_useragent, "mozilla/4.0 (compatible; msie 6.0; windows nt 5.0)"); curl_setopt($ch, curlopt_cookiejar, $cookie_file); //curl_setopt($ch, curlopt_referer, "http://125.89.69.234"); curl_setopt($ch, curlopt_returntransfer,1); curl_setopt($ch, curlopt_timeout, "10"); $response = curl_exec($ch); curl_close($ch); header("content-type:image/gif"); echo $response; //创建目录 function mkdirs($dir) { if(!is_dir($dir)) { if(!mkdirs(dirname($dir))){ return false; } if(!mkdir($dir,0777)){ return false; } } return true; }
获取数据的页面,这里需要通过表单来提交手动的验证码
define("site_path", $_server['document_root']); require_once site_path.'/class/simplehtmldom.class.php'; class getdata{ private $url ; public function __construct(){ $this->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, curlopt_url, $this->loginurl); curl_setopt($ch, curlopt_header, 0); //不返回header部分 curl_setopt($ch, curlopt_useragent, "mozilla/4.0 (compatible; msie 6.0; windows nt 5.0)"); curl_setopt($ch, curlopt_cookiejar, $cookie_file); //curl_setopt($ch, curlopt_referer, "http://125.89.69.234"); curl_setopt($ch, curlopt_returntransfer,1); curl_setopt($ch, curlopt_timeout, "10"); $response = curl_exec($ch); curl_close($ch); // 鍏抽棴curl浼氳瘽 } private function curl($url,$url2,$fields,$cookie_file) { $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_header, 0); //不返回header部分 curl_setopt($ch, curlopt_useragent, "mozilla/4.0 (compatible; msie 6.0; windows nt 5.0)"); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $fields); curl_setopt($ch, curlopt_cookiefile, $cookie_file); curl_setopt($ch, curlopt_returntransfer,1); curl_setopt($ch, curlopt_followlocation,1); curl_setopt($ch, curlopt_httpheader, array("host: union.sogou.com" )); curl_setopt($ch, curlopt_referer,$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 curl用法总结》、《php数组(array)操作技巧大全》、《php排序算法总结》、《php常用遍历算法与技巧总结》、《php数据结构与算法教程》、《php程序设计算法总结》、《php数学运算技巧总结》、《php正则表达式用法总结》、《php运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。