kimsufi 4.99欧监控脚本 (php)

kimsufi 的机器很畅销,因为是不定时放货,人工监控很麻烦,转载一篇PHP监控脚本。
<?php
#!/usr/bin/php –q

//使用方法计划任务 每分钟执行一次  * * * * * /usr/bin/php /var/www/html/sms/kimsufi.php en > /dev/null 2>&1

$type = $_SERVER['argv'][1];

if($type == 'en'){
        $json_url = 'https://ws.ovh.com/dedicated/r2/ws.dispatcher/getAvailability2';
}else{
        $type = 'ca';
        $json_url = 'https://ws.ovh.ca/dedicated/r2/ws.dispatcher/getAvailability2';
}

function _curl_data($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        $return_data = curl_exec($curl);
        curl_close($curl);

        return $return_data;
}

$data = _curl_data($json_url);
$obj = json_decode($data);

if(!is_object($obj)){
        echo '返回值不正确';
        exit;
}

$check_ava = array('142cask9','150sk10');

if(!isset($obj->answer)){
        exit;
}

$answer = $obj->answer;
$availability = $answer->availability;

if(count($availability) <= 0){
        exit;
}

$ava_array = array();

foreach($availability AS $ava){
        $reference = $ava->reference;

        if(!in_array($reference,$check_ava)){
                continue;
        }

        $zones = $ava->zones;

        foreach($zones as $zone){
                if($zone->availability == 'unavailable' || $zone->availability == 'unknown'){
                        continue;
                }

                //有存货
                $ava_array[] = $type.' - '.$reference;
        }
}

if(count($ava_array) == 0){
        exit;
}

$message = implode(',',$ava_array);

//判断是否需要发送短信 每小时只发送一次,避免短信用的太快
$sms_log = '/var/www/html/sms/'.$type.'.sms.log';
$need_sms = false;
if(!is_file($sms_log)){
        $fp = fopen($sms_log,'w');
        fwrite($fp,time());
        fclose($fp);
        //需要发送
        $need_sms = true;
}else{
        $time = file_get_contents($sms_log);
        if(time() - $time > 3600){
                //需要发送
                $need_sms = true;
                $fp = fopen($sms_log,'w');
                fwrite($fp,time());
                fclose($fp);
        }
}

if($need_sms){
        /*
        //你可以给自己发送短信 或者邮件
        $phone = '13800138000';
        include('/var/www/html/sms/api/sms.inc.php');
        $mobile_phone = trim($phone);
        $mobile_code = rand(100000,999999);
        $today = date('Y-m-d');
        $create_time = time();
        $sms = new SMS();
        $msg = 'kim有货.'.$message.', 30分钟内有效.';
        $msg = iconv('UTF-8','GBK',$msg);
        $sms->sendSMS($mobile_phone,$msg,'',2);
        if($sms->getCode() == 2000) {
                echo 'Done';
        }else{
                echo 'Faild';
        }
        */
}

发表评论

您的电子邮箱地址不会被公开。