我如何使用 SimpleXML 在 PHP 中回应这一点

发布于 2024-08-20 14:46:29 字数 1387 浏览 4 评论 0原文

Response example for MD5 hash found, for example http://md5.noisette.ch/md5.php?hash=2a0231531bc1a7fc29e2fa8d64352ae9 :

<md5lookup>
  <hash>2a0231531bc1a7fc29e2fa8d64352ae9</hash>
  <string>noisette</string>
</md5lookup>

Response for MD5 hash *not* found, for example http://md5.noisette.ch/md5.php?hash=11111111111111111111111111111111 :

<md5lookup>
  <error>
    No value in MD5 database for this hash.
  </error>
</md5lookup>

Response for MD5 hash *not* found, for example http://md5.noisette.ch/md5.php?hash=1 :

<md5lookup>
  <error>
    The string provided is not a true MD5 hash. Please try again.
  </error>
</md5lookup>

好的,我正在学习如何使用 SimpleXML。我正在运行一个脚本来从不同的站点运行类似的 API,但这是不同的。我不确定如何使用 PHP 来回显错误(如果是错误)或字符串(如果成功)。我现在使用的 API 只有 true 或 false,但无论结果如何,它仍然是相同的层次结构。

例如

http://gdataonline.com/qkhash.php?mode=xml&哈希=notanactualhashandwillnotbefound 将找不到该哈希值。 http://gdataonline.com/qkhash.php?mode=xml&hash= 098f6bcd4621d373cade4e832627b4f6 该哈希将返回“test”,

正如您所看到的,层次结构将是相同的,因此易于解析和回显

Response example for MD5 hash found, for example http://md5.noisette.ch/md5.php?hash=2a0231531bc1a7fc29e2fa8d64352ae9 :

<md5lookup>
  <hash>2a0231531bc1a7fc29e2fa8d64352ae9</hash>
  <string>noisette</string>
</md5lookup>

Response for MD5 hash *not* found, for example http://md5.noisette.ch/md5.php?hash=11111111111111111111111111111111 :

<md5lookup>
  <error>
    No value in MD5 database for this hash.
  </error>
</md5lookup>

Response for MD5 hash *not* found, for example http://md5.noisette.ch/md5.php?hash=1 :

<md5lookup>
  <error>
    The string provided is not a true MD5 hash. Please try again.
  </error>
</md5lookup>

Okay I'm just learning how to use SimpleXML. I'm running a script to run similar API's from different sites, but this is different. I'm not sure how I would use PHP to echo the error if it were an error or the string if it were a success. The API's I'm using now have just have true or false but its still the same hierarchy no matter the result.

For example

http://gdataonline.com/qkhash.php?mode=xml&hash=notanactualhashandwillnotbefound
That hash will not be found.
http://gdataonline.com/qkhash.php?mode=xml&hash=098f6bcd4621d373cade4e832627b4f6
That hash will return "test"

As you can see the hierarchy will be the same, and thus easy to parse and echo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

臻嫒无言 2024-08-27 14:46:29

我不确定我明白你在问什么,但你只需将 URL 加载到 SimpleXml 并通过常规对象表示法访问节点,例如

$parentNode->childNode

下面的示例将从 URL 加载 XML 并输出错误(如果存在),如果不存在则输出错误它将输出字符串节点。

$baseUrl = 'http://md5.noisette.ch/md5.php?hash=';
$hashes  =  array('2a0231531bc1a7fc29e2fa8d64352ae9',
                  '11111111111111111111111111111111',
                  'not a hash');

foreach($hashes as $hash) {

    // load the XML from the URL
    $dom = simplexml_load_file($baseUrl . $hash);

    if($dom->error) {
        echo $dom->error;
    } else {
        echo $hash, ' : ', $dom->string;
    }

    echo PHP_EOL; // linebreak
}

I'm not sure I understand what you are asking, but you simply load the URL to SimpleXml and access the nodes by regular object notation, e.g.

$parentNode->childNode

The example below will load the XML from the URL and output the error if it exists and if not it will output the string node.

$baseUrl = 'http://md5.noisette.ch/md5.php?hash=';
$hashes  =  array('2a0231531bc1a7fc29e2fa8d64352ae9',
                  '11111111111111111111111111111111',
                  'not a hash');

foreach($hashes as $hash) {

    // load the XML from the URL
    $dom = simplexml_load_file($baseUrl . $hash);

    if($dom->error) {
        echo $dom->error;
    } else {
        echo $hash, ' : ', $dom->string;
    }

    echo PHP_EOL; // linebreak
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文