使用file_get_contents()时在网页上获取重定向

时间:2015-03-05 21:35:46

标签: php dom metadata

我最近从Ambient Weather购买了WS-1400-IP气象站,该气象站连接到Weather Underground。我试图在IP链接设备的HTML上进行DOM查询。这是我的代码:

(我知道它非常粗糙的代码,这只是一个初步的事情。)

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$filename = "http://10.175.3.28/bscsetting.htm";
$html = file_get_contents($filename);
echo "<input type=text value='{$html}'></input>";
$dom = new DOMDocument();
@$dom->loadHTML($html);
$x = new DOMXPath($dom); 
$data = array();
foreach($x->query("//input") as $node) 
{
    $data[][$node->getAttribute("name")] = $node->getAttribute("value");
} 
echo json_encode($data, JSON_PRETTY_PRINT);

当我回应我从设备获取的HTML时,这就是我所看到的:

<input type=text value='ction: Keep-Alive
Content-type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<meta http-equiv="refresh" content="0;URL=./bscsetting.htm">
<title></title>
<body leftmargin="80" topmargin="80" marginwidth="80" marginheight="0" bgcolor="#333">
<font size="+3"><strong><p align="center"></p></strong></font>
</body>
</html>
'></input>[

]

以下是我应该看到的内容(因为这是我在使用常规浏览器时看到的内容):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>LiveData</title>
        <link href="axcss0.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
                <td colspan="2" align="right" bgcolor="#0088F7">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="2" bgcolor="#FFFFFF"><table border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="20" height = "80">&nbsp;</td>                                             
                            <td ><img src="img/1.jpg" width="74" height="80" ></td>
                            <td width="10">&nbsp;</td>
                            <td class="txtstyle_1" >ambient weather observerIP 2.0 </td>                                   
                        </tr>
                </table></td>
            </tr>
            <tr> 
                <td colspan="2" align="right" bgcolor="#60B7FF"><table border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="120" align="center"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif"><em>Version:2.0.0</em></font></td>
                        </tr>
                </table></td>
            </tr>
            <tr>
                <td colspan="2" align="left" bgcolor="#C0C0C0">
                    <table width="20" border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td bgcolor="#C0C0C0"><div class="menuitem_1"><a href="bscsetting.htm">Local Network</a></div></td>
                            <td bgcolor="#C0C0C0"><div class="menuitem_1"><a href="weather.htm">Weather Network</a></div></td>
                            <td bgcolor="#C0C0C0"><div class="menuitem_1"><a href="station.htm">Station Settings</a></div></td>
                            <td bgcolor="#EDEFEF"><div class="menuitem_1"><a href="livedata.htm">Live Data</a></div></td>
                            <td bgcolor="#C0C0C0"><div class="menuitem_1"><a href="correction.htm">Calibration</a></div></td>     
                        </tr>
                    </table>
                </td>
            </tr>
            <form name="livedata" method="POST" onsubmit="return chkForm(0);">  

                <tr>
                    <td colspan="2" bgcolor="#EDEFEF"><div class="subitem_1">Live Data</div></td>
                </tr>   
                 <tr>
                    <td width="533" bgcolor="#EDEFEF"><div class="item_1">Receiver Time:</div></td>
                    <td width="307" bgcolor="#EDEFEF">
                    <input name="CurrTime" disabled="disabled" type="text" class="item_2" style="WIDTH: 120px" value="16:34 03/04/2015" maxlength="16"/></td>
                </tr>
                <tr>
                    <td width="533" bgcolor="#EDEFEF"><div class="item_1">Indoor Sensor ID</div></td>
                    <td width="307" bgcolor="#EDEFEF"><input name="IndoorID" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="0x19" maxlength="4" /></td>
                </tr>
                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Outdoor Sensor1 ID</div></td>
                    <td bgcolor="#EDEFEF"><input name="Outdoor1ID" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="0x94" maxlength="4" /></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Outdoor Sensor2 ID</div></td>
                    <td bgcolor="#EDEFEF"><input name="Outdoor2ID" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="0x--" maxlength="4" /></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Indoor Temperature</div></td>
                    <td bgcolor="#EDEFEF"><input name="inTemp" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="71.2" maxlength="5" /></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Indoor Humidity</div></td>
                    <td bgcolor="#EDEFEF"><input name="inHumi" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="48" maxlength="3" /></td>
                </tr>
                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Absolute Pressure </div></td>
                    <td bgcolor="#EDEFEF"><input name="AbsPress" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="29.23" maxlength="6" /></td>
                </tr>   
                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Relative Pressure </div></td>
                    <td bgcolor="#EDEFEF"><input name="RelPress" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="30.29" maxlength="6" /></td>
                </tr>   
                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Outdoor Temperature</div></td>
                    <td bgcolor="#EDEFEF"><input name="outTemp" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="34.2" maxlength="5" /></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Outdoor Humidity </div></td>
                    <td bgcolor="#EDEFEF"><input name="outHumi" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="97" maxlength="3" /></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Wind Direction </div></td>
                    <td bgcolor="#EDEFEF"><input name="windir" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="228" maxlength="5" /></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Wind Speed </div></td>
                    <td bgcolor="#EDEFEF"><input name="avgwind" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="10.3" maxlength="5" /></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Wind Gust </div></td>
                    <td bgcolor="#EDEFEF"><input name="gustspeed" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="12.3" maxlength="5" /></td>
                </tr>         
                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Solar Radiation </div></td>
                    <td bgcolor="#EDEFEF"><input name="solarrad" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="17.30" maxlength="12" /></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">UV </div></td>
                    <td bgcolor="#EDEFEF"><input name="uv" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="68" maxlength="12"></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">UVI </div></td>
                    <td bgcolor="#EDEFEF"><input name="uvi" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="0" maxlength="2"></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Hourly Rain Rate</div></td>
                    <td bgcolor="#EDEFEF"><input name="rainofhourly" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="0.07" maxlength="7" /></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Daily Rain</div></td>
                    <td bgcolor="#EDEFEF"><input name="rainofdaily" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="1.81" maxlength="7" /></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Weekly Rain</div></td>
                    <td bgcolor="#EDEFEF"><input name="rainofweekly" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="2.10" maxlength="7" /></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Monthly Rain</div></td>
                    <td bgcolor="#EDEFEF"><input name="rainofmonthly" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="2.10" maxlength="7" /></td>
                </tr>

                <tr>
                    <td bgcolor="#EDEFEF"><div class="item_1">Yearly Rain</div></td>
                    <td bgcolor="#EDEFEF"><input name="rainofyearly" disabled="disabled" type="text" class="item_2" style="WIDTH: 80px" value="2.10" maxlength="7" /></td>
                </tr>

                <tr>
                    <td colspan="2" bgcolor="#EDEFEF">&nbsp;</td>  
                </tr>

                <tr>
                    <td colspan="2" align="center" bgcolor="#EDEFEF">
                        <input name="rain_Default" type="button" class="item_2" style="WIDTH: 110px; HEIGHT: 24px;"  onclick="raindef()" value="Rain Reset">
                        <input  id="refresh_st" type="button" class="item_4" style="WIDTH: 110px; HEIGHT: 24px;"  onclick="uiCtrl(this.value);" value="Refresh" />
                        <input  id="refresh_sp" type="button" class="item_2" style="WIDTH: 110px; HEIGHT: 24px;"  onclick="uiCtrl(this.value);" value="Stop Refresh"  />
                    </td>
                </tr>
            </form>
            <tr>
                <td colspan="2" bgcolor="#EDEFEF">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="2" bgcolor="#C0C0C0">&nbsp;</td>
            </tr>
        </table>

<script src="axjs0.js" type="text/javascript"></script>
        <script language="JavaScript" type="text/javascript">
            <!--
            var rfTaskRuningFlag = 0, rfTimeId;
            uiCtrl('Refresh');

            function uiCtrl(uiCmd)
            {
                if (uiCmd=='Refresh'){
                    document.getElementById('refresh_st').disabled = true;
                    document.getElementById('refresh_sp').disabled = false;
                    rfTaskRuningFlag = 1;
                    rfTaskState = 0;
                    rfTimeId = setInterval("ss()",8000);
                }else if (uiCmd=='Stop Refresh'){
                    document.getElementById('refresh_st').disabled = false;
                    document.getElementById('refresh_sp').disabled = true;
                    rfTaskRuningFlag = 0;
                    clearInterval(rfTimeId);
                }
            }

            function ss()
            {
                if (rfTaskRuningFlag)
                {
                    window.top.location.reload();
                }
            }
            //-->
</script>
    </body>
</html>

如何通过元刷新?有什么想法吗?

编辑:尝试卷曲后,它按预期工作..为什么会有所作为?

1 个答案:

答案 0 :(得分:1)

他们有一个API,并通过RESTful协议使用JSON。您需要在请求中发送api密钥,以使用服务器的api密钥对服务器进行身份验证。全部免费,然后可以指定您要查找的过去和现在的数据。然后,您可以在网页上显示或保存到数据库