1

So I have this private function:

private function curl_get($url)
{
    // Initiate the curl session
    $ch = curl_init();

    // Set the URL
    curl_setopt($ch, CURLOPT_URL, $url);

    // Removes the headers from the output
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // Return the output instead of displaying it directly
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    // Execute the curl session
    $output = curl_exec($ch);

    // Close the curl session
    curl_close($ch);

    return $output;
}

And I use if for example with this link: http://www.metacafe.com/api/item/cb-xuFyGC0jJqPfhMoFnewj4Da_ZhHCz4L2/

now the problem is that doesn't return all the data, all I get is this:

[data] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [version] => 2.0
                    [source] => Metacafe
                )

            [title] => Metacafe
            [channel] => SimpleXMLElement Object
                (
                    [title] => SimpleXMLElement Object
                        (
                        )

                    [link] => http://www.metacafe.com/watch/cb-GKeDVFevZxk_rNri_uz_K01azz3uV_ZZ/
                    [image] => SimpleXMLElement Object
                        (
                            [url] => http://s.mcstatic.com/Images/MCLogo4RSS.jpg
                            [link] => http://www.metacafe.com
                            [title] => Metacafe
                            [height] => 65
                            [width] => 229
                        )

                    [description] => SimpleXMLElement Object
                        (
                        )

                    [item] => SimpleXMLElement Object
                        (
                            [id] => cb-GKeDVFevZxk_rNri_uz_K01azz3uV_ZZ
                            [author] => CBS
                            [title] => Romney Concedes South Carolina Primary
                            [link] => http://www.metacafe.com/watch/cb-GKeDVFevZxk_rNri_uz_K01azz3uV_ZZ/romney_concedes_south_carolina_primary/
                            [rank] => 4.00
                            [category] => News & Events
                            [description] => SimpleXMLElement Object
                                (
                                )

                            [guid] => http://www.metacafe.com/watch/cb-GKeDVFevZxk_rNri_uz_K01azz3uV_ZZ/romney_concedes_south_carolina_primary/
                            [pubDate] => 18 hours ago +0000
                        )

                )

        )

Why isn't it returning the description and the tags, which are pretty important to me?

2 Answers 2

2

It's not due to curl but how SimpleXml handles CDATA:

$xml = simplexml_load_string($stringFromCurl, 'SimpleXMLElement', LIBXML_NOCDATA);

will interpret the CDATA as text nodes, see the XML constants on PNP.net

Sign up to request clarification or add additional context in comments.

2 Comments

oh, didn't know that... I am new to XML and just learned something new thanks to you and also got the solution.
I guess now all I have to do is preg_match to separate the data (tags, description, submitted by) in [description], right?
1

SimpleXML does have "all the data", it just is not visible via print_r().

echo $your_simplexml_object->channel->item->description;

4 Comments

No, it only worked around a "problem". All of the data is sitting right there, just not being shown by print_r() (without LIBXML_NOCDATA).
oh I get it, but this kinda makes my problem more difficult because now in order to get only the data I need I have to write some massive preg_match when with LIBXML_NOCDATA through DOMDocument I can make it more accessible the data needed
Your use of preg_match(), LIBXML_NOCDATA or DOMDocument have no bearing on the fact that you were incorrect in thinking that the SimpleXML object didn't have the description contents.
yes indeed @salathe, yes indeed. Thank you very much for teaching me one more new thing today.

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.