PHP爬虫和微信自动投票



layout: post
title: “PHP爬虫和微信自动投票”
date: 2016-05-17 23:38:25 +0800
comments: true
categories: [php]


今天接触了PHP的模拟登陆和爬虫实战,涉及到了PHP获取远程文件的几种方式,防采集的一些方法,以及微信自动投票实战。

PHP获取远程文件

PHP获取远程文件主要有以下几种方式:

  • fopen
  • file_get_centents
  • socket
  • curl

过几天我们在结合实际代码分析这几种方式

防采集的一些方法

防采集的方法有很多,这里主要列出几种比较有效的

  • 短时间访问次数限制
  • ip校验
  • 机器特征校验
  • 复杂加密
  • 混淆代码

微信自动投票工具

这次主要分析这个。

首先,我们需要将微信投票界面分析到PC端,并用浏览器F12里的功能抓取到投票的请求地址和返回数据。如下:


http://wx.asplay.cn/index.php?g=Wap&m=Vote&a=index&token=jdfsam1432614294&wecha_id=oQIrdjjmrAZXwKOh3Y6-1HeuwvEc&id=27&sgssz=mp.weixin.qq.com

http://wx.asplay.cn/index.php?
g=Wap&
m=Vote&
a=index& //ThinkPHP里面一些参数
token=jdfsam1432614294& //当前主用户标示
wecha_id=oQIrdjjmrAZXwKOh3Y6-1HeuwvEc&
id=27& //当前投票ID
sgssz=mp.weixin.qq.com
下面是请求的地址:
http://wx.asplay.cn/index.php?g=Wap&m=Vote&a=add_vote&token=jdfsam1432614294&wecha_id=oQIrdjjArAZYwKOh3Y6-1HeuwvEc
发送的数据:
wecha_id=oQIrdjjArAZYwKOh3Y6-1HeuwvEc&tid=27&chid=53%2C&token=jdfsam1432614294&action=add_vote、
投票成功返回的数据:
{“success”:1,”token”:”jdfsam1432614294″,”wecha_id”:”oQIrdjjArAZYwKOh3Y6-1HeuwvEc”,”tid”:”27″,”chid”:”53″,”arrpre”:{“53″:33.33,”54″:0,”55″:0,”56”:66.67}}


基于以上的这些信息,我们就能自己模拟POST的数据,和用户标示了。但是仅仅做到这些是不够的,我们还需要模拟微信的内核。

微信内核的ua://网上能找到
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.158888800.95 Safari/537.36 SE 2.X MetaSr 1.0
debug = 0;
        $this->ua = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.158888800.95 Safari/537.36 SE 2.X MetaSr 1.0'; //这里模拟微信内核
        $this->cookiejar = 'cookie.txt';
        $this->cookiefile = 'cookie.txt';
    }
    
    /****************************
    *get 请求资源
    *@param string 地址
    *@param string referer
    *@param boolen 是否返回头部
    *@param array 头部附加cookie
    ****************************/
    function httpget($url,$referer='',$withhead=0){
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_COOKIEJAR,$this->cookiejar);
        curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookiefile);
        curl_setopt($ch,CURLOPT_HEADER,$withhead);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        //curl_setopt($ch,CURLOPT_AUTOREFERER,1);
        curl_setopt($ch,CURLOPT_USERAGENT,$this->ua);
        curl_setopt($ch,CURLOPT_REFERER,$referer);
        $r = curl_exec($ch);
        if($this->debug){
            echo '
';
            var_dump(curl_getinfo($ch));
        }
        curl_close($ch);
        return $r;
    }
    
    /****************************
    *post 请求资源
    *@param string 地址
    *@param string referer
    *@param array 提交数据
    *@param boolen 是否返回头部
    ****************************/
    function httppost($url,$referer='',$postdata=array(),$withhead=0){
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_HEADER,$withhead);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        //curl_setopt($ch,CURLOPT_AUTOREFERER,1);
        curl_setopt($ch,CURLOPT_COOKIEJAR,$this->cookiejar);
        curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookiefile);
        curl_setopt($ch,CURLOPT_USERAGENT,$this->ua);
        curl_setopt($ch,CURLOPT_REFERER,$referer);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$postdata);
        $r = curl_exec($ch);
        if($this->debug){
            echo '
';
            var_dump(curl_getinfo($ch));
        }
        curl_close($ch);
        return $r;
    }


}

网上的cURL方法,我们这里有要用到






微信自动投票工具



这是微信自动投票工具";
$jkxy = new curlajax();
$jkxy->debug = 0;
$jkxy->cookiejar = dirname(__FILE__).'\cookie1.txt';
$jkxy->cookiefile = dirname(__FILE__).'\cookie1.txt';
/*模拟提交信息*/
$username="oQIrdjj".rand(500,50000)."wKOh3Y6-1HeuwvEc";//用随机数来产生不同的用户ID
$data = "wecha_id=".$username."&tid=27&chid=53%2C&token=jdfsam1432614294&action=add_vote";//模拟不同的用户提交的数据
$url = 'http://wx.asplay.cn/index.php?g=Wap&m=Vote&a=add_vote&token=jdfsam1432614294&wecha_id='.$username;
$r = $jkxy->httppost($url,$url,$data,0);//获取返回数据

if(checkstr($r,'"success":1')==true){ //根据返回的数据判断是否投票成功
    echo "投票成功";
    }
else{
    echo "投票失败";
    }
    /****************************
    *检查$needle中是否含有$findstr
    *@param string needle 
    *@param string findstr
    ****************************/
function checkstr($needle,$findstr){ 
    $tempstr=explode($findstr,$needle);
    if($tempstr>1){
        return true;
        }
    else{
        return false;
        }
    }
?>

 

通过这个实战,感觉突然打开了一扇大门。要仔细研究下爬虫的相关知识了。