无法使用simplexml_load_file()读取xml文件;

时间:2020-05-23 04:11:13

标签: php xml api

当我使用simplexml_load_file()函数时,从在线xml文件读取数据时遇到问题;我遇到一个错误。 我的代码PHP:

$url = "https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet";
$xml = simplexml_load_file($url);
echo '<pre>';
print_r($xml);
echo '</pre>';

输入错误

Warning: simplexml_load_file(https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in D:\All In One\xammp\htdocs\ty_gia_api\index.php on line 12
Warning: simplexml_load_file(): I/O warning : failed to load external entity "https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet" in D:\All In One\xammp\htdocs\ty_gia_api\index.php on line 12

1 个答案:

答案 0 :(得分:0)

尝试从以下网址获取内容:

$url = "https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet";
$xml = simplexml_load_string(shell_exec("curl -k '$url'"));
echo '<pre>';
print_r($xml);
echo '</pre>';

我看到您尝试获取的页面不是XML,而是JSON。 这将起作用:

$url = 'https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet';
$data = json_decode(shell_exec("curl -k '$url'"), 1);
echo '<pre>';
print_r($data);
echo '</pre>';

var_dump($data);
相关问题