获取当前网址并保存到XML文件

时间:2019-02-11 11:09:38

标签: php xml

在此先感谢您提供的任何帮助。

我正在尝试获取页面的当前URL并将其保存为XML表单。如果我在代码中使用echo,它会显示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>

根据乔的评论,我将其更改为:

 <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。有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

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

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

对于不同的事物,始终使用不同的名称。并继续查看您的错误日志-它通常应该留空。