[英]PHP won't echo XML child
這是我的代碼
<form method="post">
<input name="hash" type="text" id="hash" style='width: 30%;'/>
<input name="Crack!" type="submit" value="Crack!" onfocus="if(this.blur)this.blur()"/>
</form>
<?php
if(isset($_POST['Crack!'])){
$hash = $_POST['hash'];
$xml = simplexml_load_file("http://gdataonline.com/qkhash.php?mode=xml&hash=$hash");
$status = $xml->data->status;
if ($status = "Success"){
$plain = $xml->data->result;
}elseif ($status = "Hash not found"){
$plain = "Not Found"; }
?>
<table>
<tr>
<td><?php echo "gdataonline.com: "; ?></td>
<td><?php echo "$plain"; ?></td>
</tr>
</table>
<?php
echo "<pre>";
var_dump($xml);
echo "</pre>";
} //if submit
?>
由於某種原因,我根本無法回顯$ plain。 就好像它甚至無法閱讀它。
Rob,如果您想讓人們甚至理解您的問題,您就必須付出努力,而不是僅僅發布大量無關的代碼,然后問“為什么這不起作用?”
因此,我完成了作業,弄清了腳本在做什么,並獲取了一個示例XML文檔 。 事實證明,您弄錯了層次結構。 同樣,這是無關的,但是您使用賦值運算符而不是比較運算符 。 換句話說, if
您不測試任何內容,則第一個只是將$status
設置為“成功”。
相關部分應該是這樣的:
$data = simplexml_load_file("http://gdataonline.com/qkhash.php?mode=xml&hash=$hash");
switch ($data->status)
{
case 'Success':
$plain = $data->result;
break;
case 'Hash not found':
$plain = "Not Found";
break;
}
您在哪里獲得“ $ xml->數據”? 根據php.net ,SimpleXMLElement對象中沒有名為“數據”的成員。 有關正確使用此功能的大量示例,請參見該鏈接或simplexml_load_file的文檔。
可能不是您的全部問題,但是一個明確的問題是您有兩個作業而不是測試:
if ($status = "Success")
和
}elseif ($status = "Hash not found"){
都將這些值分配給$ status而不是測試相等性。 您想要$status == "Success"
和$status == "Hash not found"
在這種情況下,您的第一個測試將始終成功(因為賦值的返回值是已賦值,因此$ status =“ Success”將返回“ Success”,在'if'測試中其結果為true,因此$ plain始終是$ xml-> data-> result,即使狀態不是很成功也是如此。
這為我工作:
<form method="post">
<input name="hash" type="text" id="hash" style='width: 30%;'/>
<input name="Crack!" type="submit" value="Crack!" onfocus="if(this.blur)this.blur()"/>
</form>
<?php
if(isset($_POST['Crack!'])){
$hash = $_POST['hash'];
<?php
$xml = simplexml_load_file("http://gdataonline.com/qkhash.php?mode=xml&hash=$hash")
if(!xml)
{
echo "hash not found";
// return false; // not function so cant return false ignore it
}
$plain = $xml->result;
?>
<table>
<tr>
<td><?php echo "gdataonline.com: "; ?></td>
<td><?php echo "$plain"; ?></td>
</tr>
</table>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.