http://www.otokaze.cn/post/535.html
v2ex.png
PHP代码稍有修改几处
v2ex_sign.php

<?php
$loginUrl = 'https://www.v2ex.com/signin'; //登录入口 
$v2ex = new v2ex('username','password'); //你的账号密码,请符合v2ex的用户名规范 
if(!is_dir("cookie")){mkdir("cookie",0755);} //检查cookie目录是否存在 
if(!is_file($v2ex->CookiePath)){ //对已有登录状态cookie的账号,程序会自动跳过登陆 
file_put_contents($v2ex->CookiePath,''); 
$v2ex->getOnceAndSession($loginUrl); //获取初始状态的once值以及SESSIONID 
$v2ex->login(); 
} 
$v2ex->getOnce('https://www.v2ex.com',$v2ex->CookiePath); //获得最新Once值,用于签到 
$v2ex->sign('https://www.v2ex.com/mission/daily/redeem',$v2ex->CookiePath); //执行签到步骤 
$v2ex->update(); //更新过期cookie

function __autoload($className){  //自动引入类库
    include $className.'.class.php';
}

v2ex_sign.class.php

<?php
class v2ex{
    public $once;
    public $user;
    public $password;
    public $session;
    public $CookiePath;

    //初始化变量
    public function __construct($user,$password){
        if(!preg_match('/^\w+$/',$user.$password)) exit('请输入合法用户名及密码');
        $this->user = $user;
        $this->password = $password;
        $this->CookiePath = dirname($_SERVER['SCRIPT_FILENAME']).'/cookie/'.$this->user.'.cookie';

    }

    //如果cookie文件的创建时间已经超过12小时,则自动删除
    public function update(){
        $ctime = filectime($this->CookiePath);
        if(($ctime + 12*60*60) < time()) unlink($this->CookiePath);
    }

    //获取初始状态的Once值SESSIONID
    public function getOnceAndSession($url){
        $session = curl_init($url);
        curl_setopt($session, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($session,CURLOPT_HEADER,1);
        curl_setopt($session,CURLOPT_RETURNTRANSFER,1);
        $str = curl_exec($session);
        $result = preg_match('/PB3_SESSION="(.*?)";/',$str,$sessionArr);
        preg_match_all("/class=\"sl\"\sname=\"(.*?)\"/is", $str, $hashArr);
        if(!empty($hashArr)) {
            $this->username_hash = $hashArr[1][0];
            $this->password_hash = $hashArr[1][1];
        }
        if($result){
            preg_match('/<input\stype="hidden"\svalue="(\d+)"\sname="once"/',$str,$onceArr);
            $this->session = $sessionArr[1];
            $this->once = $onceArr[1];
            curl_close($session);
        }

    }

    //模拟header头,执行登陆并更新cookie文件
    public function login(){
        $url = 'https://www.v2ex.com/signin';
        $post = "{$this->username_hash}={$this->user}&{$this->password_hash}={$this->password}&once={$this->once}&next=%2F";
        $header = array(
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36',
            'Content-Type: application/x-www-form-urlencoded;charset=utf-8',
            'Referer: https://www.v2ex.com/signin'
        );
        $login = curl_init($url);
        curl_setopt($login,CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($login,CURLOPT_HTTPHEADER,$header);
        curl_setopt($login,CURLOPT_HEADER,1);
        curl_setopt($login,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($login,CURLOPT_POST,1);
        curl_setopt($login,CURLOPT_POSTFIELDS,$post);
        curl_setopt($login,CURLOPT_COOKIE,'PB3_SESSION="'.$this->session.'"');
        curl_setopt($login,CURLOPT_COOKIEJAR,$this->CookiePath);
        curl_exec($login);
        curl_close($login);
    }

    //更新登陆成功状态下的once值,以用于签到
    public function getOnce($url,$CookiePath){
        $once = curl_init($url);
        curl_setopt($once,CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($once,CURLOPT_HEADER,1);
        curl_setopt($once,CURLOPT_COOKIEFILE,$CookiePath);
        curl_setopt($once,CURLOPT_COOKIEJAR,$CookiePath);
        curl_setopt($once,CURLOPT_RETURNTRANSFER,1);
        $str = curl_exec($once);
        curl_close($once);
        preg_match('/once=(\d+)/',$str,$onceArr);
        $this->once = $onceArr[1];
    }

    //模拟header头,携带上cookie以及once值进行两个GET操作。
    public function sign($url,$path){
        $sign = curl_init($url.'?once='.$this->once);
        $header = array(
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36',
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Referer: https://www.v2ex.com/mission/daily'
        );
        curl_setopt($sign, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($sign,CURLOPT_HEADER,1);
        curl_setopt($sign,CURLOPT_HTTPHEADER,$header);
        curl_setopt($sign,CURLOPT_COOKIEFILE,$path);
        curl_setopt($sign,CURLOPT_RETURNTRANSFER,0);
        curl_exec($sign);
        curl_setopt($sign,CURLOPT_URL, 'https://www.v2ex.com/mission/daily');
        curl_exec($sign);
        curl_close($sign);
    }
}

v2ex_sign.sh

#!/bin/bash

export TZ="Asia/Shanghai"

time="`date +%H%M`"
if [ "$time" = "0815" ]
then
  echo "Signing ..." >&2
  (
  #php ${OPENSHIFT_REPO_DIR}www/v2ex_sign.php 2>&1 /dev/null
  curl -I ${OPENSHIFT_APP_DNS}/v2ex_sign.php 2>&1 /dev/null
  echo "Sign in to complete ..." >&2
  ) &
  exit
fi

标签: OpenShift

添加新评论