【问题标题】:(php) XML 2 TEXT is returning a 500 error(php) XML 2 TEXT 返回 500 错误
【发布时间】:2012-08-13 16:13:26
【问题描述】:

我正在编写一个简单的小脚本来读取 xml 文件并将内容打印到文本文件中。但是在某处它不断向我抛出 500 错误,我找不到它。

XML:

<calibredb>
  <record>
    <uuid>b32a07fd-dd70-4b00-acf4-395f2e69df72</uuid>
    <publisher>Boom! Studios</publisher>
    <title sort="Seven Warriors 2">Seven Warriors 2</title>
  </record>
</calibredb>

PHP 文件:

<?php

// Encryption Key
$key = "Ex6wCoVjh80Iu7ZAraanEEUyJmPHjCIt";


// Function To Generate A Unique ID
function asc2hex ($temp) {
    $data = "";
    $len = strlen($temp);
    for ($i=0; $i<$len; $i++) $data.=sprintf("%02x",ord(substr($temp,$i,1)));
    return $data;
}

// Function To Generate A Unique ID
function encrypt($string, $key) {
    $result = '';
    for($i=1; $i<=strlen($string); $i++) {
        $char = substr($string, $i-1, 1);
        $keychar = substr($key, ($i % strlen($key))-1, 1);
        $char = chr(ord($char)+ord($keychar));
        $result .= $char;
    }
    return asc2hex($result);
}

// Location For Text File
$file_text = fopen("comics.txt","w+");

// Initialize Reading Of XML File
$dom = new DOMdocument();
$dom->load('comics.xml');

foreach ($dom->getElementByTagName('record') as $entry) {

// Comic Publisher
$publisher = $entry->getElementsByTagName('publisher')->item(0)->textContent; 

// Comic Title
$title = $entry->getElementsByTagName('publisher')->item(0)->textContent;

// Comic ID
$id = $title.$publisher;
$id = encrypt($id, $key); 

// Text To Write
$text_to_write = $title." (".$publisher.") (".$id.")\r";

fwrite($file_text,$text_to_write);
echo $text_to_write." Added!<br/>";

}


fclose($file_text);

?>

也许一双新的眼睛可以指出我哪里出错了!

【问题讨论】:

  • 您的服务器日志中有什么?错误的详细信息将记录在那里。
  • 是同一目录中的 XML,您是否也有写入权限。你不检查 fopen() 是否成功
  • 500 错误不应与 PHP 有任何关系,而是通常表示服务器配置问题。
  • @MikeBrant 500 错误意味着执行脚本时存在内部问题。可能是服务器问题,也可能是代码问题。
  • 把它放在你的 php 脚本的开头并再次运行 - 告诉我们你看到了什么:ini_set('display_errors', 1); error_reporting(E_ALL);

标签: php xml text


【解决方案1】:
foreach ($dom->getElementByTagName('record') as $entry) { 

应该是:

foreach ($dom->getElementsByTagName('record') as $entry) { 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-23
    • 2011-11-01
    • 1970-01-01
    • 2011-01-14
    • 2018-06-01
    • 1970-01-01
    • 2019-03-30
    相关资源
    最近更新 更多