PHP实现登录搜狐广告获取广告联盟数据的方法【附demo源码】


Posted in PHP onOctober 14, 2016

本文实例讲述了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);
// ?抽??URL浼?璇?
}
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 相关文章推荐
人大复印资料处理程序_补充篇
Oct 09 PHP
php cout<<的一点看法
Jan 24 PHP
mysqli_set_charset和SET NAMES使用抉择及优劣分析
Jan 13 PHP
利用phpexcel把excel导入数据库和数据库导出excel实现
Jan 09 PHP
CI框架自动加载session出现报错的解决办法
Jun 17 PHP
PHP统计目录中文件以及目录中目录大小的方法
Jan 09 PHP
PHP中模拟链表和链表的基本操作示例
Feb 27 PHP
thinkphp实现把数据库中的列的值存到下拉框中的方法
Jan 20 PHP
LaravelS通过Swoole加速Laravel/Lumen详解
Mar 02 PHP
Windows平台PHP+IECapt实现网页批量截图并创建缩略图功能详解
Aug 02 PHP
解决php写入数据库乱码的问题
Sep 17 PHP
Laravel 微信小程序后端实现用户登录的示例代码
Nov 26 PHP
php生成与读取excel文件
Oct 14 #PHP
基于PHP实现用户注册登录功能
Oct 14 #PHP
PHP基于curl后台远程登录正方教务系统的方法
Oct 14 #PHP
php车辆违章查询数据示例
Oct 14 #PHP
基于PHPexecl类生成复杂的报表表头示例
Oct 14 #PHP
php+jQuery递归调用POST循环请求示例
Oct 14 #PHP
php实现将HTML页面转换成word并且保存的方法
Oct 14 #PHP
You might like
php判断正常访问和外部访问的示例
2014/02/10 PHP
php实现的简单日志写入函数
2015/03/31 PHP
实例讲解PHP设计模式编程中的简单工厂模式
2016/02/29 PHP
PHP操作MongoDB实现增删改查功能【附php7操作MongoDB方法】
2018/04/24 PHP
php获取微信基础接口凭证Access_token
2018/08/23 PHP
PHP实现的策略模式示例
2019/03/20 PHP
laravel使用数据库测试注意事项
2020/04/10 PHP
ExtJS 2.0 实用简明教程之布局概述
2009/04/29 Javascript
jquery 单击li防止重复加载的实现代码
2010/12/24 Javascript
jQuery prev ~ siblings选择器使用介绍
2013/08/09 Javascript
浅析Node在构建超媒体API中的作用
2014/07/30 Javascript
JavaScript如何动态创建table表格
2020/08/02 Javascript
js实现异步循环实现代码
2016/02/16 Javascript
js实现从左向右滑动式轮播图效果
2017/07/07 Javascript
微信小程序注册60s倒计时功能 使用JS实现注册60s倒计时功能
2017/08/16 Javascript
Vue2.0父组件与子组件之间的事件发射与接收实例代码
2017/09/19 Javascript
基于JavaScript实现表格滚动分页
2017/11/22 Javascript
响应式框架Bootstrap栅格系统的实例
2017/12/19 Javascript
对vue.js中this.$emit的深入理解
2018/02/23 Javascript
JavaScript查看代码运行效率console.time()与console.timeEnd()用法
2019/01/18 Javascript
python 排列组合之itertools
2013/03/20 Python
巧用python和libnmapd,提取Nmap扫描结果
2016/08/23 Python
Python内置函数reversed()用法分析
2018/03/20 Python
使用python对文件中的数值进行累加的实例
2018/11/28 Python
深入理解Python异常处理的哲学
2019/02/01 Python
Python 安装第三方库 pip install 安装慢安装不上的解决办法
2019/06/18 Python
Python实现一个简单的毕业生信息管理系统的示例代码
2020/06/08 Python
pycharm 配置svn的图文教程(手把手教你)
2021/01/15 Python
世界首屈一指的在线男士内衣权威:HisRoom
2017/08/05 全球购物
几道Web/Ajax的面试题
2016/11/05 面试题
师德师风建设方案
2014/05/08 职场文书
文明上网主题班会
2015/08/14 职场文书
化工生产实习心得体会
2016/01/22 职场文书
100句拼搏进取的名言警句,值得一读!
2019/10/07 职场文书
MongoDB使用profile分析慢查询的步骤
2021/04/30 MongoDB
Docker下安装Oracle19c
2022/04/13 Servers