【问题标题】:Load the xml in php and return the data在php中加载xml并返回数据
【发布时间】:2016-02-04 16:22:41
【问题描述】:

我正在寻找如何在 PHP 中加载 XML 文件并返回不重定向的数据。最好尽可能对最终用户隐藏 xml 内容。

我见过这样的东西,但我不能让它工作,请如果你能用链接示例写出完整的代码..

     public function sendResponse($type,$cause) {

    $response = '<?xml version="1.0" encoding="utf-8"?>';
    $response .= '<response><status>'.$type.'</status>';

            $response = $response.'<remarks>'.$cause.'</remarks></response>';
            return $response;
 }

 ....
 ....

 header("Content-type: text/xml; charset=utf-8");
 echo sendResponse($type,$cause);

如果可以,请提供帮助。 提前致谢, SX

【问题讨论】:

  • 是否要解析外部xml文件并使用php显示?

标签: php html xml kodi


【解决方案1】:

我不确定你的要求,但首先你不能调用 sendResponse() 因为它不是一个函数,而是一个类中的方法

您需要实例化您的类,然后调用该方法。

例子:

$yourObject = new YourClass();

$yourObject->sendResponse();

See the manual

对于您的情况,see the manual for simpleXML 并尝试一下:

    function sendResponse($type,$cause) {

    $response = '<?xml version="1.0" encoding="utf-8"?>';
    $response .= '<response><status>'.$type.'</status>';

            $response = $response.'<remarks>'.$cause.'</remarks></response>';
            return $response;
 }

$type="type";
$cause="cause";
var_dump(simplexml_load_string(sendResponse($type,$cause)));

如果你的脚本是外部的,你可以通过file_get_contents获取它

$xml = file_get_contents('http://yourTarget.com');

var_dump($xml);

【讨论】:

  • 可能的问题是我需要一个 pHp 文件来调用 XML 文件并返回数据,我需要一个 kodi 插件...所以用户无法获取我的 XML 文件...我了解需要 php 来返回数据,然后我需要 XML 文件来限制它在服务器上...对不起混淆...只是上面的例子只是一个论坛的复制粘贴,如果有更好的方法请尽可能让我知道。谢谢。
  • 那么,你有一个外部 xml,你想得到它并用 php 解析 XML 吗?您可以使用 file_get_contents (php.net/manual/en/function.file-get-contents.php) 获取外部文件并使用 simplexml_load_string 解析它:)
  • 我想要这个代码WEBSITEHERE.com/getxml.php // 这是代码; somewhere.xml'); } else{ header("HTTP/1.1 302 Found"); header('位置:http:somewhere.xmll'); } ?>
  • 但是...当我通过 WEBSITE.com/getxml.php?username=newyork&pwd=newyork1 调用它时...我不希望它显示 .XML 文件...如果您将此代码放入浏览器将直接转到 .XML 并且任何人都可以查看它。,,我尝试了 IndexIgnore *.php 甚至整个页面进入 .htaccess 但是当你将该代码放入浏览器时直接进入 .XML 并且它是可行...有没有办法我可以禁用它? ……?谢谢
【解决方案2】:

您的问题在某种程度上误导了我,请确保您是否要加载外部 xml 文件并让您的 PHP 代码对其进行解析。试试下面的代码

Assume the following is your xml content fo text.xml 

<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>"

<-- PHP -->

$file = 'http://exmp.com/text.xml';     // give the path of the external xml file
if(!$xml = simplexml_load_file($file))    // this checks whether the file exists
exit('Failed to open '.$file);           // exit when the path is wrong 
print_r($xml);                          // prints the xml format in the form of array 

你的输出将是这个

SimpleXMLElement Object ( 
[to] => Tove 
[from] => Jani 
[heading] => Reminder 
[body] => Dont forget me this weekend!
 ) 

希望这会有所帮助...

【讨论】:

    猜你喜欢
    • 2012-09-09
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    相关资源
    最近更新 更多