正则抓取某浪星座,根据星座查询当日星座运势。
调用方式:XXX.php?action=值
代码示例
<?php
/*
author:蓝优
星座对应
白羊座=Aries
金牛座=Taurus
双子座=Gemini
巨蟹座=Cancer
狮子座=leo
处女座=Virgo
天秤座=Libra
天蝎座=Scorpio
射手座=Sagittarius
魔羯座=Capricorn
水瓶座=Aquarius
双鱼座=Pisces
*/
header('Content-type: application/json; charset=utf-8');
!empty($_REQUEST['action']) ? $action = $_REQUEST['action'] : exit(json_encode(['code'=>201, "msg"=>"请输入网址"],JSON_UNESCAPED_UNICODE));
$json = output($action);
echo json_encode($json,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
function output($id){
$api = file_get_contents('http://astro.sina.com.cn/fate_day_'.$id.'/index.html');
$str = str_replace(array("\n","\t"," "),"",$api);
preg_match_all('/<tr><td class="tb_td1">今日幸运值<\/td><td>(.*)<\/td><td class="tb_td1">爱情指数<\/td><td>(.*)<\/td><\/tr>/Us',$str,$One);
preg_match_all('/<tr><td class="tb_td1">工作指数<\/td><td>(.*)<\/td><td class="tb_td1">财运指数<\/td><td>(.*)<\/td><\/tr>/Us',$str,$Two);
preg_match_all('/<tr><td class="tb_td1">健康指数<\/td><td>(.*)<\/td><td class="tb_td1">幸运数字<\/td><td>(.*)<\/td><\/tr>/Us',$str,$Three);
preg_match_all('/<tr><td class="tb_td1">幸运颜色<\/td><td>(.*)<\/td><td class="tb_td1">贵人星座<\/td><td>(.*)<\/td><\/tr>/Us',$str,$Four);
preg_match_all('/<td colspan="3" style="line-height: 25px;">(.*)<\/td>/Us',$str,$Detail);
$value = array('lucky'=>$One[1][0],'love'=>$One[2][0],'work'=>$Two[1][0],'finance'=>$Two[2][0],'health'=>$Three[1][0],'number'=>$Three[2][0],'color'=>$Four[1][0],'noble'=>$Four[2][0],'Detail'=>$Detail[1][0]);
if($value){
$item = array('coede'=>200,'msg'=>'成功','title'=>$id,'data'=>$value);
}else{
$item = array('coede'=>202,'msg'=>'失败');
}
return $item;
}
返回对应值说明
幸运值=lucky
爱情指数=love
工作指数=work
财运指数=finance
健康指数=health
幸运数字=number
幸运颜色=color
贵人星座=noble
今日详情=Detail