PHP implementation of the login Sohu ads to obtain the Advertising alliance data Method "with demo source" _php Skills

Source: Internet
Author: User
Tags create directory curl datetime mkdir php programming php regular expression

This article describes the PHP implementation of the login Sohu ads to obtain the Advertising alliance data method. Share to everyone for your reference, specific as follows:

Always have an idea, every time to log in to see how much the alliance yesterday? Every day to log in and trouble, can do a summary of the function of the email?

Unfortunately, the verification code around, can only think of one way. First in the server to manually play the verification code, and then in the timer, every 10 minutes to request a page

In this way the cookies will not fail, and then the weekly only need to collect data with me OK.

The principle of the remote submission form, you can refer to: PHP based on curl background remote login Square teaching System method

The reference code is the same as the following

Get Authentication Code 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 "Directory creation failed";
 Exit
 } $ch = Curl_init ();
 curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_header, 0); does not return header part 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;
   Create a directory function mkdirs ($dir) {if (!is_dir ($dir)) {if (!mkdirs (DirName ($dir))) {return false;
 } if (!mkdir ($dir, 0777)) {return false;
} return true; }

The page that gets the data, where you need to submit the manual verification code through the table forms

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"; The Public Function post ($code) {$POST [' loginfrompage '] = ' homepage '; $POST [' username '] = ' xxxxxx '; $POST [' password '] = ' xx
XXX ";
$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 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"); Write Way Open File fwrite ($fp, $infomation); Deposit Content fclose ($FP); 
Close file}//Create directory Private Function mkdirs ($dir) {if (!is_dir ($dir)) {if (! $this->mkdirs (dirname ($dir))) {return false;}
if (!mkdir ($dir, 0777)) {return false;}}
return true; The Public Function login () {$filedir = Site_path.
/tmp/cookies "; if (! $this->mkdirs ($filedir)) {echo directory creation failed; exit} $cookie _file = $filedir. "
/cookie.txt ";
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $this->loginurl); curl_setopt ($ch, Curlopt_header, 0); does not return header part 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 Innocent 氳 瘽} Private Function Curl ($url, $url 2, $fields, $cookie _file) {$ch = Curl_init (); curl_setopt ($ch, Curlopt_url,
$url); curl_setopt ($ch, Curlopt_header, 0); does not return header part 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, $url 2);
$response = curl_exec ($ch);
echo Curl_error ($ch);
Curl_close ($ch);
return $response;
}} $GetData = new GetData ();

 if (isset ($_post[' code ')) {$GetData->post ($_post[' Code ')}

Full instance code click here to download the site .

More about PHP Interested readers can view the site topics: "Php Curl Usage Summary", "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.