0x00
接上篇,既然已经有了故事,那么就应带获取对应时间,为了跟原获取图片对应,所以继续使用
idx
这个值来确定获取idx天前的图片,由于必应的网页用的是年月日的结构来获取的,比如20170701
获取今天的是日期是$time= date("20ymd");
,昨天的是$time= date("20ymd",strtotime('-1 day'));
那就很简单了 $time= date("20ymd",strtotime('-'.$_GET['idx'].'day'));
然后把$text=file_get_contents('http://cn.bing.com/cnhp/life');
改成 $text=file_get_contents('http://cn.bing.com/cnhp/life?currentDate='.$time);
即可 也就是说获取代码为
<?php
date_default_timezone_set (PRC);
if($_GET['idx']==null){
$_GET['idx']=0;
}
$time= date("20ymd",strtotime('-'.$_GET['idx'].'day'));
$text=file_get_contents('http://cn.bing.com/cnhp/life?currentDate='.$time);
preg_match('$(?<=\<div\sclass=\"hplatt\"\>)(.*?)(?=\<\/div\>)$',$text,$title);
$bing['title']=$title[0];
preg_match('$(?<=\<span\sclass=\"hplaAttr\"\>)(.*?)(?=\<\/span\>)$',$text,$add);
$bing['add']=$add[0];
preg_match('$(?<=\<div\sclass=\"hplats\"\>)(.*?)(?=\<\/div\>)$',$text,$titles);
$bing['titles']=$titles[0];
preg_match('$(?<=\<div\sclass=\"hplaTtl\"\>)(.*?)(?=\<\/div\>)$',$text,$hpla);
$bing['hpla']=$hpla[0];
preg_match('$(?<=\<div\sid=\"hplaSnippet\"\>)(.*?)(?=\<\/div\>)$',$text,$more);
$bing['more']=$more[0];
?>
0x01
既然已经获取了故事,就把获取的东西存入同目录的bing.txt文件里 通过
fopen()
与fwrite()
可以很轻松的实现这一项,通过类似html标签的方法写入,通过id说明日期,所以这一区域的代码如下
$myfile = fopen("./bing.txt", "a") or die("Unable to open file!");
$all ="<date>".$time."</date>\n"."<match id=".$time.">".$match['0']."</match>\n"."<url id=".$time.">".$bingimg['imgurl']."</url>\n"."<hpla id=".$time.">".$bing['hpla']."</hpla>\n"."<add id=".$time.">".$bing['add']."</add>\n"."<title id=".$time.">".$bing['title']."</title>\n"."<titles id=".$time.">".$bing['titles']."</titles>\n"."<more id=".$time.">".$bing['more']."</more>\n";
fwrite($myfile, $all);
fclose($myfile);