需要通过HTML表单提交,使用PHP在网站上显示API结果(XML)


Need to display API results (XML) on site using PHP through HTML form submission

我正在努力让它发挥作用,但我一直很难把它整合起来。在实现了代码并在我看到建议的地方进行了小的更改之后,这并没有帮助。这与工作或学校无关。只是为了让我的爱好更轻松。所以我在网页上有一个表单,我可以向它提交数据。API,我用XML将数据提交给回复。这是有效的。我可以将数据从表单传递到API,并获得原始XML。但是它不容易阅读。因此,我创建了一个php文件,希望能够提取XML并以更易于阅读的方式对其进行排列。

该系统的工作原理如下:

HTML文件

<form method="post" action="search.php">
value0:<input type="text" size="12" maxlength="12" name="0"><br />
value1:<input type="text" size="12" maxlength="36" name="1"><br />
value2:<input type="checkbox" value="2" name="2"><br />
<input type="submit" value="submit" name="submit">
</form>

PHP文件

<?php
$zero = $_POST['0'];
$one = $_POST['1'];
$two = $_POST['2'];
$url = "http://www.website.com/file.asmx/p?apikey=XXXXXX&two='. $two .'&zero='. $zero .'"
$xml = json_decode($url);
$three = $xml->attribute1;
$four = $xml->attribute2;
$five = $xml->attribute3;
/*Form Debug*/
echo "<br>"
echo $_POST['0'];
echo "<br>"
echo $_POST['1'];
echo "<br>"
echo $_POST['2'];
echo "<br>"
/*/Form Debug*/
echo "V1: " . $three . "<br>";
echo "V2: " . $four . "<br>";
echo "V3: " . $five . "<br>";
?>

以UTF-8 XML V1 从API返回的结果

<products>
<item>
<attribute0>11407</attribute0>
<attribute1>12345</attribute1>
<attribute2>abcde</attribute2>
<attribute3>!@#$%</attribute3>
<link>http://www.website.com</link>
</item>
</products>

关于如何使用SimpleXML(PHP)创建所需的XML格式,还有一个SO问题的答案非常相似。

看看这里:

PHP XML如何输出漂亮的格式