sahap
sahap

Reputation: 389

PHP parsing XML from URL

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.

Upvotes: 0

Views: 1363

Answers (2)

sahap
sahap

Reputation: 389

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.

Upvotes: 0

Dalım Çepiç
Dalım Çepiç

Reputation: 513

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);

Upvotes: 2

Related Questions