php怎么抓取天气预报?

来源:百度知道 编辑:UC知道 时间:2024/06/30 17:08:46

可以借由php的api或者preg_match_all偷偷撷取去达成目的

这里给你一段我给台湾朋友有一段源码

<?php

header(\"Content-Type: text/html; charset=utf-8\");
function getWeather($city){

$toURL = \"
$city.htm\";
$post = array();
$ch = curl_init();
$options = array(
CURLOPT_REFERER=>'',
CURLOPT_URL=>$toURL,
CURLOPT_VERBOSE=>0,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_USERAGENT=>\"Mozilla/4.0 (compatible;)\",
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>http_build_query($post),
);
curl_setopt_array($ch, $options);

$result = curl_exec($ch); 
curl_close($ch);
//连接中央气象局
echo '<pre>';
preg_match_all('/<table class=\"FcstBoxTable01\" [^>]*[^>]*>(.*)<\/div>/si',$result, $matches,&n