SimpleXML_Load_File returns 一个 URL 没有结果,另一个可以
SimpleXML_Load_File returns no results for one URL, OK for another
我有一个第三方服务,我正在查询以获取返回的 XML 文件。如果我在浏览器中访问 URL,我会看到 XML 数据,但 SimpleXML_Load_File 会崩溃,而且我也无法让它显示任何错误。
第一张 DVD URL 工作和加载正常,但游戏没有。
任何人都可以在这里看到我的代码有什么问题吗,或者 XML 被返回以指示为什么 simplexml_load_file 会失败并阻止页面上执行更多 php ?
<?php
######DVDs
$dvdURL = 'http://dvd.find-services.co.uk/dvdSearch.aspx?sort=popular&site=sample&pagesize=1';
$dvdfeed = simplexml_load_file($dvdURL);
var_dump($dvdfeed);
echo '<hr>';
###########Games
$gameURL = 'http://game.find-services.co.uk/gameSearch.aspx?order=popular&site=sample&pagesize=1';
$gamefeed = simplexml_load_file($gameURL);
var_dump($gamefeed);
?>
好的,在 Grim 发表评论后...关于在 PHPfiddle 上运行的代码,我在我的服务器上只使用了以下代码:
<?php
$gameURL = 'http://game.find-services.co.uk/gameSearch.aspx?order=popular&site=sample&pagesize=1';
$gamefeed = simplexml_load_file($gameURL);
var_dump($gamefeed);
?>
这会报错!
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster@finditcheapest.com to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
但是,访问我的 cPanel 中的错误日志显示没有任何条目。有什么想法吗?
@Grim...建议使用cURL,所以这里是新代码:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://game.find-services.co.uk/gameSearch.aspx?order=popular&site=sample&pagesize=1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($ch);
$xml = utf8_encode($xml);
var_dump($xml);
$simpleXml = simplexml_load_string($xml);
var_dump($simpleXml);
curl_close($ch);
?>
第二个var_dump还是空白,第一个给出:
string(541) "B00ZG1S834ps4PlayStation 446927No Mans Sky11148490"
查看源显示:
string(541) "<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?><games><game><id>B00ZG1S834</id><platform>ps4</platform><platformName>PlayStation 4</platformName><category><![CDATA[Strategy]]></category><title><![CDATA[No Man's Sky (PS4)]]></title><titleRefNo>46927</titleRefNo><groupTitle>No Mans Sky</groupTitle></game><ResultsInfo><Message></Message><SearchString></SearchString><SearchPlatform></SearchPlatform><Page>1</Page><PageSize>1</PageSize><MoreResultsAvailable>1</MoreResultsAvailable><RowCount>48490</RowCount></ResultsInfo></games>"
后者 URL (game.find-services...) 使用 ISO-8859-1 编码,但 PHP 需要 UTF-8。最好使用 CURL
加载文件,使用 utf8_encode
将其转换为 UTF-8,然后使用 simplexml_load_string
获取您的数据。
几个月后,事实证明这很简单。从原始代码,我更改了行:
$dvdfeed = simplexml_load_file($dvdURL);
至:
$dvds= GetPage($dvdfeed );
$dvdfeed = @simplexml_load_string($dvds);
其中 GetPage()
是一个 PHP 函数,如下所示:
function GetPage ($URL, $PostParams = ''){
// Set params...
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
if ($PostParams <> ''){
curl_setopt($ch, CURLOPT_POSTFIELDS, $PostParams);
};
$Page = curl_exec($ch);
curl_close($ch);
return $Page;
};
现在可以正确加载和解析 XML,而不管编码如何。
我有一个第三方服务,我正在查询以获取返回的 XML 文件。如果我在浏览器中访问 URL,我会看到 XML 数据,但 SimpleXML_Load_File 会崩溃,而且我也无法让它显示任何错误。 第一张 DVD URL 工作和加载正常,但游戏没有。
任何人都可以在这里看到我的代码有什么问题吗,或者 XML 被返回以指示为什么 simplexml_load_file 会失败并阻止页面上执行更多 php ?
<?php
######DVDs
$dvdURL = 'http://dvd.find-services.co.uk/dvdSearch.aspx?sort=popular&site=sample&pagesize=1';
$dvdfeed = simplexml_load_file($dvdURL);
var_dump($dvdfeed);
echo '<hr>';
###########Games
$gameURL = 'http://game.find-services.co.uk/gameSearch.aspx?order=popular&site=sample&pagesize=1';
$gamefeed = simplexml_load_file($gameURL);
var_dump($gamefeed);
?>
好的,在 Grim 发表评论后...关于在 PHPfiddle 上运行的代码,我在我的服务器上只使用了以下代码:
<?php
$gameURL = 'http://game.find-services.co.uk/gameSearch.aspx?order=popular&site=sample&pagesize=1';
$gamefeed = simplexml_load_file($gameURL);
var_dump($gamefeed);
?>
这会报错!
Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at webmaster@finditcheapest.com to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
但是,访问我的 cPanel 中的错误日志显示没有任何条目。有什么想法吗?
@Grim...建议使用cURL,所以这里是新代码:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://game.find-services.co.uk/gameSearch.aspx?order=popular&site=sample&pagesize=1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($ch);
$xml = utf8_encode($xml);
var_dump($xml);
$simpleXml = simplexml_load_string($xml);
var_dump($simpleXml);
curl_close($ch);
?>
第二个var_dump还是空白,第一个给出:
string(541) "B00ZG1S834ps4PlayStation 446927No Mans Sky11148490"
查看源显示:
string(541) "<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?><games><game><id>B00ZG1S834</id><platform>ps4</platform><platformName>PlayStation 4</platformName><category><![CDATA[Strategy]]></category><title><![CDATA[No Man's Sky (PS4)]]></title><titleRefNo>46927</titleRefNo><groupTitle>No Mans Sky</groupTitle></game><ResultsInfo><Message></Message><SearchString></SearchString><SearchPlatform></SearchPlatform><Page>1</Page><PageSize>1</PageSize><MoreResultsAvailable>1</MoreResultsAvailable><RowCount>48490</RowCount></ResultsInfo></games>"
后者 URL (game.find-services...) 使用 ISO-8859-1 编码,但 PHP 需要 UTF-8。最好使用 CURL
加载文件,使用 utf8_encode
将其转换为 UTF-8,然后使用 simplexml_load_string
获取您的数据。
几个月后,事实证明这很简单。从原始代码,我更改了行:
$dvdfeed = simplexml_load_file($dvdURL);
至:
$dvds= GetPage($dvdfeed );
$dvdfeed = @simplexml_load_string($dvds);
其中 GetPage()
是一个 PHP 函数,如下所示:
function GetPage ($URL, $PostParams = ''){
// Set params...
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
if ($PostParams <> ''){
curl_setopt($ch, CURLOPT_POSTFIELDS, $PostParams);
};
$Page = curl_exec($ch);
curl_close($ch);
return $Page;
};
现在可以正确加载和解析 XML,而不管编码如何。