【问题标题】:Get current URL and save to XML file获取当前 URL 并保存到 XML 文件
【发布时间】:2019-02-12 16:13:04
【问题描述】:

提前感谢您提供的任何帮助。

我正在尝试获取页面的当前 URL 并将其保存在 XML 表单中。如果我在代码中使用回显,它会显示 URL,但表单不会替换 XML 中的 url。我错过了什么?

我正在使用的代码:

 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
 <?php
 $xml = new DOMDocument('1.0', 'utf-8');
 $xml->formatOutput = true; 
 $xml->preserveWhiteSpace = false;
 $xml->load('data.xml');

 //Get item Element
 $element = $xml->getElementsByTagName('person')->item(0);  

 //Load child elements
 $name = $element->getElementsByTagName('name')->item(0);
 $comment = $element->getElementsByTagName('comment')->item(0);
 $currentUrl = $element->getElementsByTagName('currentUrl')->item(0);

 //Replace old elements with new
 $element->replaceChild($name, $name);
 $element->replaceChild($comment, $comment);
 $element->replaceChild($currentUrl, $currentUrl);

 

 ?>
<?php
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
                === FALSE ? 'http' : 'https';
$host     = $_SERVER['HTTP_HOST'];
$script   = $_SERVER['SCRIPT_NAME'];
$params   = $_SERVER['QUERY_STRING'];
$currentUrl = $protocol . '://' . $host . $script . '?' . $params;
echo $currentUrl;
?>

 <?php
 if (isset($_POST['submit']))
 {
$name->nodeValue = $_POST['namanya'];
$comment->nodeValue = $_POST['commentnya'];
$currentUrl->nodeValue = $_POST['currentUrlya'];
htmlentities($xml->save('data.xml'));

 }

 ?>

 <form method="POST" action=''>
  name <input type="text-name" value="<?php echo $name->nodeValue  ?>" name="namanya" />
comment  <input type="text-comment" value="<?php echo $comment->nodeValue  ?>"  name="commentnya"/>
<input type="hidden" name="currentUrlya" value="<?php echo $currentUrl->nodeValue  ?>">

 <input name="submit" type="submit" />
 </form>

对于 XML:

<?xml version="1.0" encoding="UTF-8"?>
<inventors>
  <person>
    <name>david</name>
    <comment> yes, so powwow</comment>
    <currentUrl>http://google.com</currentUrl>
  </person>
</inventors>

根据 Joe 的 cmets,我将其更改为:

 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
 <?php
 $xml = new DOMDocument('1.0', 'utf-8');
 $xml->formatOutput = true; 
 $xml->preserveWhiteSpace = false;
 $xml->load('data.xml');



$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
                === FALSE ? 'http' : 'https';
$host     = $_SERVER['HTTP_HOST'];
$script   = $_SERVER['SCRIPT_NAME'];
$params   = $_SERVER['QUERY_STRING'];
$currentUrl = $protocol . '://' . $host . $script . '?' . $params;


 //Get item Element
 $element = $xml->getElementsByTagName('person')->item(0);  

 //Load child elements
 $name = $element->getElementsByTagName('name')->item(0);
 $comment = $element->getElementsByTagName('comment')->item(0);
 $currentUrl2 = $element->getElementsByTagName('currentUrl')->item(0);

 //Replace old elements with new
 $element->replaceChild($name, $name);
 $element->replaceChild($comment, $comment);
 $element->replaceChild($currentUrl2, $currentUrl2);

 ?>
 
<?php

?>

 <?php
 if (isset($_POST['submit']))
 {
$name->nodeValue = $_POST['namanya'];
$comment->nodeValue = $_POST['commentnya'];
$currentUrl2->nodeValue = $_POST['currentUrlya'];
htmlentities($xml->save('data.xml'));

 }

 ?>

 <form method="POST" action=''>
  name <input type="text-name" value="<?php echo $name->nodeValue  ?>" name="namanya" />
comment  <input type="text-comment" value="<?php echo $comment->nodeValue  ?>"  name="commentnya"/>
<input type="hidden" name="currentUrlya" value="<?php echo $currentUrl->nodeValue  ?>">

 <input name="submit" type="submit" />
 </form>

不过,它仍然不保存 URL。有关如何解决此问题的任何想法?

【问题讨论】:

  • $element-&gt;replaceChild($currentUrl, $currentUrl); 看起来很奇怪。
  • 我不知道这是什么意思..
  • 您正在用自己替换子节点。我猜你的意思是$currentUrl-&gt;nodeValue = $currentUrlString,其中$currentUrlString = $protocol . '://' . …
  • 嗨乔,谢谢..我只是这方面的初学者,我认为其他子节点是相似的(例如 $name)并且它适用于那些,所以它会是当前 URL 也是如此。我如何构建它以便实际保存 URL?

标签: php xml


【解决方案1】:

您的代码发出Notice: Trying to get property 'nodeValue' of non-object in …

原因是,您将使用字符串值$currentUrl = $protocol . '://' . $host . … 覆盖$currentUrl DOM 节点变量。 因此,稍后使用$currentUrl-&gt;nodeValue = … 设置新的 DOM 节点值将失败。

总是为不同的事物使用不同的名称。并继续查看您的错误日志——它通常应该保持空白。

【讨论】:

  • 如果我改变“$currentUrl2 = $element->getElementsByTagName('currentUrl')->item(0);”和“ $element->replaceChild($currentUrl2, $currentUrl2);”然后,“$currentUrl2->nodeValue = $_POST['currentUrlya'];” ..代码如何将日期引用回xml?我在这里错过了一块..
  • 您的错误日志是一个文本文件——您可以在 Linux/Unix 上使用 tail 命令或在 Windows 上使用 Get-Content 简单地查看它。 $xml-&gt;save(…) 写入当前 DOM 文档的 XML 表示。一切都很好。如果您需要 XML 作为字符串,请使用 $xml-&gt;saveXML()
  • 嗨乔,再次感谢,我编辑了原始问题以显示更改..它现在的工作方式只是删除了 xml 中的 currentUrl 字符串并且不保存新 URL.. 可以请再看一遍?
  • 您仍在 HTML 表单中使用 $currentUrl-&gt;nodeValue 而不是 $currentUrl2(例如,更好的名称是 $currentUrlElement,因为它描述了它的含义)。同样,错误日志可能会提示您正确的方向。
  • 是的,我在发布编辑后注意到了这一点,但即便如此,我如何让 $currentUrl2 等于提取的 URL ($currentUrl) 并保存在 xml 中?我是自学成才的,所以我所做的一切都是反复试验..
猜你喜欢
  • 1970-01-01
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多