天气数据来自weather.com.cn的接口 $id = "101010100";//此处是北京的城市ID,可以去weather.com.cn找到对应的weather city ID //http://m.weather.com.cn/data/101070201.html-->大连 $url = "http://m.weather.com.cn/data/".$id.".html";//接口URL $f = new SaeFetchurl();//SAE fetchURL类 $f->setMethod('get'); $data = $f->fetch($url); $json = json_decode($data, TRUE);//解析json $data = $json["weatherinfo"]; $date_y = mb_substr($data["date_y"], mb_strpos($data['date_y'], "年") + 1, mb_strlen($data['date_y']), 'utf-8'); $date = mb_substr($data["date"], 3, mb_strlen($data['date']), 'utf-8'); $week = str_replace("星期", "周", $data['week']); $data['index48_d']=matchSTR($data['index48_d']); $data['index_d']=matchSTR($data['index_d']); $str = $sug."{$data['city']}({$date_y},{$date},{$week}):{$data['weather1']},{$data['temp1']},{$data['wind1']},紫外线{$data['index_uv']},{$data['index_d']}明天{$data['weather2']},{$data['temp2']},{$data['wind2']},紫外线{$data['index48_uv']},{$data['index48_d']}后天{$data['weather3']},{$data['temp3']}"; echo $str; function matchSTR($str){ if(preg_match_all('/([^/]]*)。年老体弱者([^/]]*)。([^/]]*)。/s',$str,$matchResult,PREG_SET_ORDER)){ $str =$matchResult[0][1].','.$matchResult[0][3].'。'; }elseif(preg_match_all('/([^/]]*)。年老体弱者([^/]]*)。/s',$str,$matchResult,PREG_SET_ORDER)){ $str = $matchResult[0][1].'。'; } return $str; }