PHP parsing XML from URL

北城余情 提交于 2019-12-13 06:08:23

问题


I'm using this code to get some XML from a URL:

$url = 'http://shelly.ksu.ru/e-ksu/get_schedulle_aud?p_id=563';
$xml = simplexml_load_file($url);
print_r($xml);

This is giving me an empty result, but the following link gives me the correct results:

$url = 'http://habrahabr.ru/rss';

I can't seem to see the difference between these links and I don't know how to find the answer on Google. Any assistance is greatly appreciated.


回答1:


This xml file charset isn't UTF8 charset, but you can see the StdObject with this :

    <?php
    header('Content-type: text/html; charset=utf-8');

    $url = 'http://shelly.ksu.ru/e-ksu/get_schedulle_aud?p_id=563';
    $data = file_get_contents($url);
    $data = iconv(mb_detect_encoding($data, mb_detect_order(), true), "UTF-8", $data);
    $xml = simplexml_load_string($data);

    print_r($xml);



回答2:


Okay, this work perfectly:

header("Content-Type: text/plain; charset=utf8");
$url = 'http://shelly.ksu.ru/e-ksu/get_schedulle_aud?p_id=563';
$xml_string = file_get_contents($url);

$utf_xml_string = iconv("cp1251", "utf8", $xml_string);
print_r(" *** iconv() ***\n");
print_r($utf_xml_string);   
print_r("\n\n");

I am using iconv to convert string.



来源:https://stackoverflow.com/questions/19103326/php-parsing-xml-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!