iT邦幫忙

0

請問如何使用php正確的讀取xml?!

您好
看了這個教學 http://blog.davidou.org/archives/704
進行操作但是出現錯誤訊息:
Warning: preg_match_all(): Unknown modifier '(' in /var/www/html/accuweather.php on line 17

Warning: preg_match_all(): Unknown modifier '(' in /var/www/html/accuweather.php on line 18

Warning: preg_match_all(): Unknown modifier '(' in /var/www/html/accuweather.php on line 19

Warning: preg_match_all(): Unknown modifier '?' in /var/www/html/accuweather.php on line 20

Warning: preg_match_all(): Unknown modifier '?' in /var/www/html/accuweather.php on line 21

請問如何修正?!

weiclin iT邦高手 4 級 ‧ 2018-08-15 12:49:35 檢舉
你的程式碼呢?
myubuntu iT邦新手 4 級 ‧ 2018-08-15 13:22:26 檢舉
<html>
<title>抓天氣</title>
<style type="text/css">
<!--
body {
color:#3233EF;
margin: 0px;
}
-->
</style>
<html>
<body width="717" height="186"><font size="4">

<?php
$contents = file_get_contents("http://realtek.accu-weather.com/widget/realtek/weather-data.asp?location=cityId:315040");
$result = array();
eregi("<temperature>(.*)</temperature>", $contents, $temperature); //溫度
eregi("<realfeel>(.*)</realfeel>", $contents, $realfeel); //體感溫度
eregi("<weathertext>(.*)</weathertext>", $contents, $weathertext); //天氣敘述
preg_match_all("/</?windspeed>(.*?)</?windspeed>/", $contents, $windspeed, PREG_SET_ORDER);//風速
preg_match_all("/</?weathericon>(.*?)</?weathericon>/", $contents, $weathericon, PREG_SET_ORDER);//天氣小圖

echo "溫度:".$temperature[1];
echo "<br>";
echo "體感溫度:".$realfeel[1];
echo "<br>";
echo "敘述:".$weathertext[1];
echo "<br>";
echo "風速:".$windspeed[0][1];
echo "<hr>";
echo "<img src='http://api.accuweather.com/developers/Media/Default/WeatherIcons/".$weathericon[0][1]."-s.png'>";
echo "<br>";
?>

</font>
</body>
</html>
myubuntu iT邦新手 4 級 ‧ 2018-08-15 15:14:28 檢舉
想要抓取weathericon可以抓得出來嗎?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
echochio
iT邦高手 1 級 ‧ 2018-08-15 13:45:47
最佳解答
<?php
function getTextBetweenTags( $string, $tagname ) {
    preg_match( "/\<{$tagname}\>(.*)\<\/{$tagname}\>/", $string, $matches );
    return $matches[1];
}

$contents = file_get_contents("http://realtek.accu-weather.com/widget/realtek/weather-data.asp?location=cityId:315040");
var_dump( getTextBetweenTags( $contents, 'temperature' ) );
?>

應該你看的懂 ..... 自己改吧 ....

哈我抓到 ......

string(2) "86"

你還要改 ... 應該是 86°F ==> 30°C

看更多先前的回應...收起先前的回應...
myubuntu iT邦新手 4 級 ‧ 2018-08-15 15:32:35 檢舉

想請教您
我可以抓出weathericon的值
var_dump( getTextBetweenTags( $contents, 'weathericon' ) );

應該如何搭配天氣編號顯示圖示
https://developer.accuweather.com/weather-icons

echochio iT邦高手 1 級 ‧ 2018-08-15 16:14:04 檢舉

我抓到

string(2) "07"

那圖是https://developer.accuweather.com/sites/default/files/07-s.png

https://developer.accuweather.com/sites/default/files/07-s.png

就用字串 07 就可以了吧 ?

php 字串相加 .... 就有 07-s.png 圖了 ....

myubuntu iT邦新手 4 級 ‧ 2018-08-15 16:21:23 檢舉

是的

myubuntu iT邦新手 4 級 ‧ 2018-08-15 16:34:21 檢舉

請問您實際的做法是?

echochio iT邦高手 1 級 ‧ 2018-08-16 10:12:14 檢舉

隨便寫一下 .....
建議還是用 MVC 架構

<html>
<title>抓天氣</title>
<style type="text/css">
<!--
body {
color:#3233EF;
margin: 0px;
}
-->
</style>
<html>
<body width="717" height="186"><font size="4">
<?php
function getTextBetweenTags( $string, $tagname ) {
    preg_match( "/\<{$tagname}\>(.*)\<\/{$tagname}\>/", $string, $matches );
    return $matches[1];
}

$contents = file_get_contents("http://realtek.accu-weather.com/widget/realtek/weather-data.asp?location=cityId:315040");
$pic = getTextBetweenTags( $contents, 'weathericon' ) ;
$out_pic = "https://developer.accuweather.com/sites/default/files/".$pic."-s.png";
?>
<img src="<?php echo $out_pic ?>">

</font>
</body>
</html>
myubuntu iT邦新手 4 級 ‧ 2018-08-16 15:35:55 檢舉

敢問什麼是MVC 架構

echochio iT邦高手 1 級 ‧ 2018-08-16 16:39:58 檢舉

我要發表回答

立即登入回答