【问题标题】:Reading an XML file with PHP5 [closed]使用 PHP5 读取 XML 文件 [关闭]
【发布时间】:2012-10-14 10:00:51
【问题描述】:

所以我一直在尝试从 PlaystationNetwork API 获取一些数据,

http://www.psnapi.com.ar/ps3/api/psn.asmx/getPSNID?sPSNID=jameslfc19

所以我一直在使用这个代码

<?php
//Get Username
$username = $_GET["u"];

// Passing the XML
$psnxml = @simplexml_load_file('http://psnapi.com.ar/ps3/api/psn.asmx/getPSNID?sPSNID=' .$username);

$psnname = $psnxml->PSNId->Avatar;
echo $psnname;
?>

这绝对不会输出任何东西..

我使用 @ 是因为否则我会得到一个(是的,我知道,但我认为即使 XML 文档有 500 内部服务器错误,它仍然会获取数据)

Warning: simplexml_load_file(http://psnapi.com.ar/ps3/api/psn.asmx/getPSNID?sPSNID=jameslfc19) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in /Applications/XAMPP/xamppfiles/htdocs/Sigs/PSN2.php on line 6

最好的方法是什么?我假设我用@ 忽略的错误导致了问题。

【问题讨论】:

  • “500 内部服务器错误”在 psnapi.com.ar 方面。这是你问题的根源。
  • 所以...您正在抑制警告并且想知道为什么什么都没有发生...?
  • 我明白了,我应该如何解决这个问题?使用卷曲?我以前从未使用过 cURL。
  • 对不起,我还在学习很多关于 PHP 的知识,我知道在这里发帖是个坏主意。我去别的地方看看。
  • 问题是,您仍然可以在浏览器中阅读文档psnapi.com.ar/ps3/api/psn.asmx/getPSNID?sPSNID=jameslfc19

标签: php xml simplexml


【解决方案1】:

simlexml_load_file on php.net 的 cmets 中,我发现了以下内容。未经测试,但值得一试。

sean at aliencreations dot com 17-Mar-2011 10:59 如果你发现你 使用 simplexml_load_file() 收到 500 个错误,但您可以访问 通过浏览器手动输入 xml/rss,您的脚本可能是 被用户代理嗅探器阻止。

在您的 xml 调用之前添加此代码以解决此问题

<?php

ini_set("user_agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
ini_set("max_execution_time", 0);
ini_set("memory_limit", "10000M");

$rss = simplexml_load_file($feed_url);

?>

【讨论】:

  • 谢谢!我不能投票,因为我没有足够的声誉。不过非常感谢!
猜你喜欢
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多