1

I use simplexml_load_file and expect it can be use on a url, strangely some of the data is missing.

my php

$url = "http://www.thestar.com.my/RSS/Lifestyle/Women/";
$xml = simplexml_load_file($url);
echo "<pre>";
print_r($xml);

partial result

    [item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [guid] => {936842CF-550D-4CAB-A840-23BB4E766B8E}
                    [link] => http://www.thestar.com.my/Lifestyle/Women/Fashion/2014/07/02/Jamy-Yang-Mingjie-Shared-travelling-values
                    [title] => SimpleXMLElement Object
                        (
                        )

                    [description] => SimpleXMLElement Object
                        (
                        )

                    [pubDate] => Wed, 02 Jul 2014 00:00:00 +08:00
                    [enclosure] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [url] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/JamyYangMingjie020714.ashx?crop=1&w=460&h=345&
                                    [length] => 
                                    [type] => image/jpeg
                                )

                        )

                )

item->title became SimpleXMLElement Object?

user3522729
  • 55
  • 1
  • 4
  • Yes, and that's **no problem at all**. It's all always a SimpleXMLElement, it's just that you can't use `print_r` with SimpleXMLElement in a useful manner here. See all answers in the linked duplicate. – hakre Jul 06 '14 at 18:57

2 Answers2

1

When I view the source to that url, all the strings are wrapped with CDATA tags, including the title. According to "wouter at code-b dot nl" at php:

"To correctly extract a value from a CDATA just make sure you cast the SimpleXML Element to a string value by using the cast operator"

Greg K
  • 434
  • 4
  • 7
-1

You forgot to add an additional parameters to get the CDATA Character Data. Try this:

$url = "http://www.thestar.com.my/RSS/Lifestyle/Women/";
$xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>";
print_r($xml);
user1978142
  • 7,946
  • 3
  • 17
  • 20