<?php
/**
* Description
* @authors Your Name (you@example.org)
* # 依据常常彩的最新一期的号码, 推断假如为首尾同号则发送短信
* - phpQuery 剖析网页, 取得网页数据, 取得html数据
* - curl_init 模仿实在浏览器接见, 合营 PHPquery
* - 依据手机号, 连系前端 get与post 要求发送短信关照
* - 运用的 php 的正则, 替代字符串
* - 运用 sleep 与 while 重复实行代码
* - 重复实行代码的同时, 还能够在浏览器输出
* # 只要革新 wamp 才停止历程`
* # 现实在云效劳中会报错: FastCGI 历程超过了设置的运动超常常限的解决方法
* - 无解
* - 所以只能在本机运用wamp开启
* @date 2018-07-26 17:13:39
* @version 1.0.0
*/
header("Content-Type: text/html;charset=utf-8");
require('phpQuery/phpQuery.php');
// $eg1=phpQuery::newDocumentFile("http://www.whu.edu.cn/tzgg.htm");
// $eg2=phpQuery::newDocumentFile("https://www.baidu.com/");
// $eg3=phpQuery::newDocumentFile("http://caipiao.163.com/award/cqssc/");
// echo pq("title",$eg1->getDocumentID())->html()."<br>";//$eg1与$eg1->getDocumentID()结果同等
// echo pq("title")->html()."<br>";//就近婚配 $eg2
// phpQuery::selectDocument($eg1); //默许会运用选定的文档
// $mes=pq("ul")->html();//猎取一切的ul标签中的html内容
// $mes=pq("ul,li")->html();//猎取一切的ul以及li标签中的html内容
// $t=pq("ul[class='article']")->html();//猎取ul class="article"的html内容
/***
* 依据 手机号, 第几期, 当期号码 发送短信关照
*/
function fnSendMessageNotifications($mobile, $lotteryNumber1, $lotteryNumber2)
{
echo '<br>****[' . $mobile . ']****<br>' ;
echo '<br>****[' . $lotteryNumber1 . ']****<br>' ;
echo '<br>****[' . $lotteryNumber2 . ']****<br>' ;
$host = "http://cowsms.market.alicloudapi.com";
$path = "/intf/smsapi";
$method = "GET";
$appcode = "你的appcode";
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$querys = "mobile=" . $mobile . "¶s=" . $lotteryNumber1 . "%2C" . $lotteryNumber2 . "&sign=%E6%B6%88%E6%81%AF%E9%80%9A&tpid=155";
echo '<br>****[' . $querys . ']****<br>' ;
$bodys = "";
$url = $host . $path . "?" . $querys;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$".$host, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
var_dump(curl_exec($curl));
}
/**
* 增加 USERAGENT , 模仿实在浏览器接见
* */
function get_fcontent($url, $timeout = 5 ) {
$url = str_replace( "&", "&", urldecode(trim($url)) );
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url);//须要猎取的URL地点,也能够在 curl_init()函数中设置。
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );//衔接完毕后保留cookie信息的文件。
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );//启用时会将效劳器效劳器返回的"Location: "放在header中递归的返回给效劳器,运用CURLOPT_MAXREDIRS能够限制递归返回的数目。
curl_setopt( $ch, CURLOPT_ENCODING, "" );//HTTP要求头中"Accept-Encoding: "的值。支撑的编码有"identity","deflate"和"gzip"。假如为空字符串"",要求头会发送一切支撑的编码范例。在cURL 7.10中被到场。
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );//将 curl_exec()猎取的信息以文件流的情势返回,而不是直接输出。
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );//当依据Location:重定向时,自动设置header中的Referer:信息。
//禁用后cURL将停止从效劳端举行考证。运用CURLOPT_CAINFO选项设置证书运用CURLOPT_CAPATH选项设置证书目次 假如CURLOPT_SSL_VERIFYPEER(默许值为2)被启用,CURLOPT_SSL_VERIFYHOST须要被设置成TRUE不然设置为FALSE。自cURL 7.10最先默许为TRUE。从cURL 7.10最先默许绑定装置。
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); // # required for https urls, 在提议衔接前守候的时候,假如设置为0,则无穷守候。
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout ); // 设置cURL许可实行的最长秒数。
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 ); //指定最多的HTTP重定向的数目,这个选项是和CURLOPT_FOLLOWLOCATION一同运用的。
$content = curl_exec( $ch );
curl_close ( $ch );
return $content;
}
// set_time_limit(0); // Maximum execution time , 设置成0的话,就是永不逾期。
$lastQi = ''; // foreach保留的末了一个 期号
$lastNumber = ''; // foreach保留的末了一个 号码
$nTheLastTraversalPeriod = ''; // 缓存上一个foreach的首尾雷同的期号, 用以比对是不是为最新一期, 也用来当作末了一个相符前提的期号
$nTheLastTraversalNumber = '';
$sessionStorage_cacheQi = ''; // 缓存上一个发送了短信的期号
/**
* 取得最新一期期号, 取得最新一期号码
* */
function fnGetTheLatestPhase(){
//细致每一个curlopt意义.转http://www.jb51.net/article/39331.htm
global $lastQi;
global $lastNumber;
global $nTheLastTraversalPeriod;
global $nTheLastTraversalNumber;
global $sessionStorage_cacheQi;
$url = 'http://caipiao.163.com/award/cqssc/';
$content = get_fcontent($url);
phpQuery::newDocumentHTML($content);
// $main = pq('title')->html();
// $table = pq('.award-winNum')->html();
$table_tr = pq('tr');
$nCurrentTerm = '';
foreach( pq(".award-winNum") as $table_tr ) {
$nCurrentTerm = pq($table_tr) -> prev()-> html(); // 当前的期号
$nCurrentNumber = pq($table_tr) -> html();// 当前号码
if($nCurrentNumber[0] === $nCurrentNumber[strlen($nCurrentNumber)-1] && intval($nTheLastTraversalPeriod)<intval($nCurrentTerm)){ // 假定当前号码的首尾雷同, 且上一个遍历的首尾雷同的期号小于当前期号(是foreach末了一个遍历的最大的期号)
$nTheLastTraversalPeriod = $nCurrentTerm;
$nTheLastTraversalNumber = $nCurrentNumber;
echo '<br>****[' . $nTheLastTraversalPeriod . ']****<br>' ;
}
if($nCurrentTerm === '001' && !is_numeric(preg_replace('/ /', '', $nCurrentNumber))){
// 假定重新最先, 天天新一期, 旧的还在缓存, 怎样办?
// 假定当前的期号为001, 示意的常常彩从头最先, 此时须要:
// - 删除缓存的上一个发送短信的期号
// - 删除缓存的上一个首尾雷同的期号
$nTheLastTraversalPeriod = '';
$sessionStorage_cacheQi = '';
}
// // echo pq($table_tr);
// $text = pq($table_tr) -> html();// 号码
// $_lastQi = pq($table_tr) -> prev()-> html(); // 当前元素的上一个元素
// echo '<br>****[' . $nTheLastTraversalPeriod . ']****<br>' ;
// if($text[0] === $text[strlen($text)-1] && intval($sessionStorage_cacheQi||'')<intval($_lastQi) && intval($nTheLastTraversalPeriod)<intval($_lastQi) ){
// if($text[0] === $text[strlen($text)-1] && intval($nTheLastTraversalPeriod)<intval($_lastQi) ){
echo '<br>';
}
echo '<hr>';
if($nTheLastTraversalPeriod !== $sessionStorage_cacheQi){
// 假定没有值得发送的期号, 假定当前发送的期号与之前发送的期号差别(不能雷同), 则不实行相干代码(发送短信)
$sessionStorage_cacheQi = $nTheLastTraversalPeriod;
$object = (object) [
'nPeriodNumber' => $nTheLastTraversalPeriod,
'nNumber' => preg_replace('/ /', '', $nTheLastTraversalNumber),
];
return $object;
}else{
$object = (object) [
'nPeriodNumber' => '',
'nNumber' => '',
];
return $object;
}
}
// var_dump(fnGetTheLatestPhase());
// $options = fnGetTheLatestPhase();
// fnSendMessageNotifications('185********', $options->nPeriodNumber, $options->nNumber);
while (1) {
$options = fnGetTheLatestPhase();
// 二者必需同时运用来革新输出缓冲
ob_flush(); //将数据从php的buffer中释放出来
flush(); //将释放出来的数据发送给浏览器, 如果没有 flush 和 ob_flush , 浏览器就会堕入一种假卡死的状况
var_dump($options);
if($options->nPeriodNumber){
fnSendMessageNotifications('185****', $options->nPeriodNumber, $options->nNumber);
}
sleep(5);
}