PHP實(shí)現(xiàn)登錄搜狐廣告獲取廣告聯(lián)盟數(shù)據(jù)的方法【附demo源碼】
本文實(shí)例講述了PHP實(shí)現(xiàn)登錄搜狐廣告獲取廣告聯(lián)盟數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
一直有一個(gè)想法,每次都要登錄去看聯(lián)盟昨天收益多少?每天都要登錄和麻煩,能不能做一個(gè)匯總發(fā)郵件的功能呢?
可惜了,驗(yàn)證碼繞不過去,只能想一個(gè)辦法。先在服務(wù)器手動(dòng)打一次驗(yàn)證碼,然后在通過定時(shí)器,每隔10分鐘請(qǐng)求一個(gè)頁面
這樣的話Cookies就不會(huì)失效,,然后每周只需要跟我匯總數(shù)據(jù)就Ok了。。
遠(yuǎn)程提交表單的原理,可以參考:PHP基于curl后臺(tái)遠(yuǎn)程登錄正方教務(wù)系統(tǒng)的方法
參考的代碼還是一樣的如下
獲取驗(yàn)證碼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 "目錄創(chuàng)建失敗";
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;
//創(chuàng)建目錄
function mkdirs($dir)
{
if(!is_dir($dir))
{
if(!mkdirs(dirname($dir))){
return false;
}
if(!mkdir($dir,0777)){
return false;
}
}
return true;
}
獲取數(shù)據(jù)的頁面,這里需要通過表單來提交手動(dòng)的驗(yàn)證碼
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); //存入內(nèi)容
fclose($fp); //關(guān)閉文件
}
//創(chuàng)建目錄
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 "目錄創(chuàng)建失敗";
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浼?xì)璇?
}
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']);
}
完整實(shí)例代碼點(diǎn)擊此處本站下載。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php排序算法總結(jié)》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php正則表達(dá)式用法總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP基于curl后臺(tái)遠(yuǎn)程登錄正方教務(wù)系統(tǒng)的方法
- php 使用curl模擬登錄人人(校內(nèi))網(wǎng)的簡(jiǎn)單實(shí)例
- PHP Curl模擬登錄微信公眾平臺(tái)、新浪微博實(shí)例代碼
- PHP curl模擬登錄帶驗(yàn)證碼的網(wǎng)站
- PHP使用CURL模擬登錄的方法
- PHP讀取CURL模擬登錄時(shí)生成Cookie文件的方法
- PHP使用CURL實(shí)現(xiàn)對(duì)帶有驗(yàn)證碼的網(wǎng)站進(jìn)行模擬登錄的方法
- PHP中使用CURL模擬登錄并獲取數(shù)據(jù)實(shí)例
- PHP CURL獲取cookies模擬登錄的方法
- php使用curl模擬登錄后采集頁面的例子
- PHP CURL模擬登錄新浪微博抓取頁面內(nèi)容 基于EaglePHP框架開發(fā)
相關(guān)文章
rephactor 優(yōu)秀的PHP的重構(gòu)工具
從PHP5開始,提供了強(qiáng)大的面向?qū)ο蠊δ堋J沟肞HP能夠完全按設(shè)計(jì)模式編程。2011-06-06
利用PHP+JS實(shí)現(xiàn)搜索自動(dòng)提示(實(shí)例)
本篇文章對(duì)利用PHP+JS實(shí)現(xiàn)搜索自動(dòng)提示的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP實(shí)現(xiàn)RSA簽名生成訂單功能【支付寶示例】
這篇文章主要介紹了PHP實(shí)現(xiàn)RSA簽名生成訂單功能,涉及php隨機(jī)字符串及編碼相關(guān)操作技巧,以及支付寶公鑰文件讀取與使用方法,需要的朋友可以參考下2017-06-06
Laravel5.5+ 使用API Resources快速輸出自定義JSON方法詳解
這篇文章主要介紹了Laravel5.5+ 使用API Resources快速輸出自定義JSON方法詳解,需要的朋友可以參考下2020-04-04
PHP刪除特定數(shù)組內(nèi)容并且重建數(shù)組索引的方法.
我們知道.PHP沒有提供專門刪除一個(gè)特定數(shù)組元素的方法.但是可以通過unset()函數(shù)來完成.2011-03-03

