dounaidu0204 2015-03-05 21:35
浏览 65

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

I recently purchased the WS-1400-IP weather station from Ambient Weather that links up to Weather Underground. I am attempting to do a DOM query on the HTML of the IP link device. Here is my code:

(I know its very crude code, it's just a preliminary thing).

<?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);

And this is what I am seeing when I echo the HTML I am getting from the device:

<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>[

]

And here is what I should be seeing (as this is what I see when I use a regular browser):

<!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>

How do I get past the meta refresh? Any ideas?

EDIT: After trying curl, it works as expected.. Why would it make a difference?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 易康econgnition精度验证
    • ¥15 线程问题判断多次进入
    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致