一个简单的数据采集,这里用的方法是API数据采集
//api地址,读取文本
$result = file_get_contents("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2509&k=&num=50&page=1"); if(!$result){
die;
}
//json数据解析
$result = json_decode($result,true); if(count($result['result']['data'])==0){
die;
}
$test1 = 0;
foreach($result['result']['data'] as $k=>$v){
$sdata['add_time'] = $v['ctime'];
$sdata['title'] = $v['title'];
$sdata['pic'] = !empty($v['images'][0]['u']) ? $v['images'][0]['u'] : ''; $sdata['source'] = $v['media_name']; $sdata['source_url'] = $v['url'];
$sdata['content'] = $v['intro']; //存到redis里
$db = $redis->LPUSH('news',json_encode($sdata));
$redis->expire('news',60);//设置失效时间[true | false] 单位:秒
}