ดึงข้อมูล xml จำนวนมาก แต่ต้องการตัวที่ใช้ ต้้องดึงยังไงครับ

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา ดึงข้อมูล xml จำนวนมาก แต่ต้องการตัวที่ใช้ ต้้องดึงยังไงครับ

ดึงข้อมูล xml จำนวนมาก แต่ต้องการตัวที่ใช้ ต้้องดึงยังไงครับ
สวัสดีครับ มีเรื่องสอบถามหน่อยครับ จากบทความ ดึงไฟล์ xml ถ้าผมจะดึงจาก

http://ews.dwr.go.th/website/webservice/rain_daily.php?uid=xxxxx&upass=yyyy&dmode=1&dtype=1&ondate=dd/mm/yyyy

ซึ่งมีข้อมูลเยอะมาก แต่ตองการแค่ที่กำหนดต้องเขียนยังไงครับ


ได้ลองเขียน ตามนี้ครับ
<html>
    <head>
        <title>index</title>
        <meta http-equiv="content-type">
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="jquerymobile/jquery.mobile-1.4.5.min.css"/>
        <script src="jquerymobile/jquery.js"></script>
        <script src="jquerymobile/jquery.mobile-1.4.5.min.js"></script>
    </head>
    <body>
        <div data-role="page" id="page1">
            <div data-role="header" data-theme="b">
                <h1>หน้าที่1</h1>
            </div>
            <div data-role="content" data-theme="a">
                <h1>สถานการณ์น้ำ</h1>
                <?php
                $file = "http://ews.dwr.go.th/website/webservice/rain_daily.php?uid=s57122201100&upass=123456789&dmode=1&dtype=1&ondate=dd/mm/yyyy";
                $ews = simplexml_load_file($file);
                $title = $ews->title;
                $department = $ews->date->department;
                $numstation = $ews->numstation;
                $date = $ews->date;
                $station = $ews->station;
                $village = $ews->stationid->village;
                $subdistrict = $ews->stationid->subdistrict;
                $district = $ews->stationid->district;
                $province = $ews->stationid->province;
                $rain12h = $ews->stationid->rain12h;
                $rain07h = $ews->stationid->rain07h;
                $temp = $ews->stationid->temp;
                echo "<b><a href='$numstation' target='_blank'>$title</a></b><br>";
                echo "<small>$department<br><i>$date</i></small>";
                echo "<hr size=1>";
                echo "$station<br>";
                ?>
                <a href="#page2" data-transition="slide">Next</a>
            </div>
            <div data-role="footer" data-theme="b">
                <h3>copyright panmile</h3>
            </div>
        </div>
</body>
</html>


Jack Panmile 58.137.217.xxx 04-12-2017 13:28:16

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1
อะไรคือแค่ที่กำหนด 


ninenik 183.88.78.xxx 04-12-2017
 ความคิดเห็นที่ 2
กำหนดแค่ station id  อะครับ
แล้วให้แสดงแค่ข้อมูลของสถานีนั้นๆครับ
เช่น

หมู่บ้าน: บ้านหน้าถ้ำ
ตำบล: นาเหรง
อำเภอ: นบพิตำ
จังหวัด: นครศรีธรรมราช
ปริมาณฝน(12 ชม) mm : 0.0
ฝนรายวัน(07:00) mm: 158.5
อุณหภูมิ : 23.40 
ระดับน้ำ: N/A
ความชื้น: N/A

โดยไม่ต้องดึงมาทั้งหมด 


ขอบคุณครับ


Jack Panmile 202.29.38.xxx 04-12-2017 14:06
 ความคิดเห็นที่ 3
ข้อมูลมีขนาดใหญ่เกินไป ถ้าจะเรียกไปที่ url ทุกครั้ง ลองใช้วิธีโหลดมาเก็บเป็นไฟล์ แล้วเรียงผ่านไฟล์จะเร็วกว่า
ถ้า api ของเขา ทำแบบส่งค่าไป แล้วแสดงเฉพาะรายการที่ต้องการ ก็จะดีกว่านี้ แต่ถ้าในที่นี้ไม่มี ยังไงก็ต้องดึงข้อมูลมาทั้งหมดก่อน
แล้วแปลงเป็น array ไว้อ้างอิงใช้งาน
ตัวอย่างเรียกแบบผ่าน url จะใช้เวลาโหลดนาน พอสมควร

<?php
$url = "sssssss";
$reader = new XMLReader();
$reader->open($url);
$station = array();
$i = 0;
while( $reader->read() ) {
	if($reader->name === 'station'  && $reader->nodeType==1){
//		echo $reader->nodeType;
		$stationID = $reader->getAttribute('id');
		$stationOrder = $reader->getAttribute('order');		
		$i++;
		$data = explode("\n",$reader->readString());
		$station[$stationID] = array_filter($data);
//		echo $i." -- ";
//		echo $stationID." -- ".$stationOrder." -- ";
//		echo	 $reader->name;
	}

}
$reader->close();
print_r($station);


จะได้ตัวแปร $station เป็น array ออกมา เวลาอ้างอิงเฉพาะค่าที่อต้องการ ก็จะเป็น

print_r($station['STN0289']);


หรือกรณี save เป็นไฟล์ก่อนเรียกใช้งาน

<?php
$url = "sssssss";
$file = 'rain_daily.xml';
$content = file_get_contents($url);
file_put_contents($file,$content);

$reader = new XMLReader();
$reader->open($file);
$station = array();
$i = 0;
while( $reader->read() ) {
	if($reader->name === 'station'  && $reader->nodeType==1){
//		echo $reader->nodeType;
		$stationID = $reader->getAttribute('id');
		$stationOrder = $reader->getAttribute('order');		
		$i++;
		$data = explode("\n",$reader->readString());
		$station[$stationID] = array_filter($data);
//		echo $i." -- ";
//		echo $stationID." -- ".$stationOrder." -- ";
//		echo	 $reader->name;
	}

}
$reader->close();
print_r($station);


แต่ต้องเอาไปแรยะุกต์ตอนการบันทึกไฟล์อีกที ว่า ให้ดึงข้อมูลมาบันทึกเป็นไฟล์ไว้ใช้ ในช่วงเวลาไหน โดยไม่ให้ดึงข้อมูลทุกครั้ง
ตัวอย่างเช่น ดาวน์โหลดไฟล์ใหม่ทุกวันละครั้ง

$url = "sssssss";
$file = 'rain_daily.xml';
if(file_exists($file)) {
	if(date("Y-m-d")!=date("Y-m-d",filemtime($file))){
		$content = file_get_contents($url);
		file_put_contents($file,$content);		
	}
}else{
	$content = file_get_contents($url);
	file_put_contents($file,$content);
}







ninenik 183.88.78.xxx 04-12-2017
 ความคิดเห็นที่ 4
ขอบคุณครับ 


Jack Panmile 202.29.38.xxx 04-12-2017 18:19
1






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ