PHP不会回应XML子

时间:2010-02-04 20:14:07

标签: php html xml simplexml

这是我的代码

    <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。它似乎甚至无法读取它。

4 个答案:

答案 0 :(得分:5)

Rob,如果你想让人们甚至理解你的问题,你必须付出努力,而不是只发布大量不相关的代码并问“为什么这个不起作用?”

所以我完成了你的作业,我弄清楚了剧本的作用并获得了example XML document。事实证明,你的层次结构错了。此外,这与您无关,但您使用的是assignment operators而不是comparison operators。换句话说,您的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;
}

答案 1 :(得分:1)

你在哪里获得“$ xml-&gt;数据”?根据{{​​3}},没有名为“data”的SimpleXMLElement对象的成员。有关正确使用此功能的大量示例,请参阅该链接或php.net的文档。

答案 2 :(得分:1)

这可能不是你的整个问题,但一个明确的问题是你有两个任务而不是测试:

if ($status = "Success")

}elseif ($status = "Hash not found"){

都将这些值分配给$ status而不是测试相等性。您需要$status == "Success"$status == "Hash not found"

在这种情况下,您的第一个测试将始终成功(因为赋值的返回值是指定的值,因此$ status =“Success”将返回“Success”,它将在'if'测试中评估为true,因此$ plain将永远是$ xml-&gt; data-&gt;结果,即使状态不是真的成功。

答案 3 :(得分:0)

这对我有用:

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