0

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

$url = 'http://***';
$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://***/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.

5
  • 1
    Start here php.net/manual/en/simplexml.examples-basic.php Commented Sep 30, 2013 at 20:37
  • 1
    Your first URL contains XML validation issues, it is not valid XML code. If you can't change that source or you cannot contact who is creating that source you will better code yourself your own PHP parser. Commented Sep 30, 2013 at 20:42
  • 2
    @Martin what isn't valid about it? (PHP won't care about the warnings). Commented Sep 30, 2013 at 20:45
  • I've tested both and they could be parsed by simple xml. What's the problem? Commented Sep 30, 2013 at 20:46
  • hek2mgl: really? Can you share your code with us? Commented Sep 30, 2013 at 20:57

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

No, it's not working for me... Secondly link give xml with UTF-8 charset
The http response is: charset=WINDOWS-1251
0

Okay, this work perfectly:

header("Content-Type: text/plain; charset=utf8");
$url = 'http://***';
$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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.