PHP Curl模拟登录签到

<?php

/** 
*    curl登录签到
*     201704
*     W3C
*     qjd
*/ 


$arr['cookstr'] = "";
$arr['url'] = "http://www.w3cschool.cn/checklogin";
$arr['url2'] = "http://www.w3cschool.cn/index/checkHeader";
$arr['pstr'] = "refer=&username=***&password=***&scode=&remember=1";
$arr['name'] = strtotime(date("Y-m-d H:00:00"));
$arr['ua'] = "客户端";

$strs = getw3c($arr);
$v = json_decode($strs)->statusCode;

// 登录失败删除缓存的cookie
if($v == '300'){
    unlink($arr['name']);
    $strs = getw3c($arr);
}

// 签到
$arr['url2'] = "http://www.w3cschool.cn/my/checkin";    
// 返回状态
echo json_decode(getw3c($arr))->message;

// w3c模拟登录签到
function getw3c($arr='') {
    if(empty($arr)) return 'value is null';     
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $arr['url']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);              // 不直接输出数据
    curl_setopt($ch, CURLOPT_POST, 1);                        // post
    curl_setopt($ch, CURLOPT_POSTFIELDS, $arr['pstr']);      // post字符串
    curl_setopt($ch, CURLOPT_COOKIE, $arr['cookstr']);       // cookie
    curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['name']);       // 读取cookie文件
    curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['name']);        //保存响应头cookie 
    curl_setopt($ch, CURLOPT_USERAGENT, $arr['ua']);         //模拟客户端
    // curl_setopt($ch, CURLOPT_HEADER, 1);                    
    if(!file_exists($arr['name'])) curl_exec($ch);            // 缓存cookie
    curl_setopt($ch, CURLOPT_URL, $arr['url2']);              // 访问要抓取的页面
    $res = curl_exec($ch);
    curl_close($ch);
    return $res;    
}

评论

暂无评论