views:

35

answers:

3

I'm using SimpleXML to parse a data file from an external source. I'm trying to pull a thumbnail from the result, which looks like this:

    <entry>
        <title>Ball_Punch</title>
        <author>
            <name>burningcandle2010</name>
            <uri>https://www.mochimedia.com/community/profile/burningcandle2010&lt;/uri&gt;
        </author>
        <link href="http://www.mochimedia.com/games/allout-offsite" rel="alternate" />
        <link href="http://games.mochiads.com/c/g/allout-offsite/Ball_Punch.swf" rel="enclosure" type="application/x-shockwave-flash" />
        <id>urn:uuid:bf720e45-7ca0-34c7-a63a-0f6f20a4c267</id>
        <media:player height="470" url="http://games.mochiads.com/c/g/allout-offsite/Ball_Punch.swf" width="798" />
        <media:thumbnail height="100" url="http://thumbs.mochiads.com/c/g/allout-offsite/_thumb_100x100.png" width="100" />
        <media:title>Ball_Punch</media:title>
        <media:description>punch your ball</media:description>
        <media:keywords>other, rhythm</media:keywords>
        <category term="Puzzles" />
        <updated>2010-07-04T08:13:22.571963-08:00</updated>
        <published>2010-07-04T06:58:55.577826-08:00</published>
        <summary type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml"&gt;
            <a href="http://www.mochimedia.com/games/allout-offsite"&gt;
                <img class="thumbnail" src="http://thumbs.mochiads.com/c/g/allout-offsite/_thumb_100x100.png" />
            </a>
            <dl>
                <dt>Tag</dt>
                <dd class="tag">cdb41e529fbe39bd</dd>
                <dt>Description</dt>
                <dd class="description">punch your ball</dd>
                <dt>Resolution</dt>
                <dd class="resolution">798x470</dd>
                <dt>Instructions</dt>
                <dd class="instructions" />
                <dt>Key Mappings</dt>
                <dd class="key_mappings" />
                <dt>Control Scheme</dt>
                <dd class="control_scheme">{"fire": "left_mouse", "jump": "space", "movement": "mouse"}</dd>
                <dt>Categories</dt>
                <dd class="categories">Puzzles</dd>
                <dt>Keywords</dt>
                <dd class="keywords">other, rhythm</dd>
                <dt>Rating</dt>
                <dd class="rating">Everyone</dd>
                <dt>Leaderboards</dt>
                <dd class="leaderboards">False</dd>
                <dt>Embed</dt>
                <dd>
                    <code class="embed">&lt;embed src="http://games.mochiads.com/c/g/allout-offsite/Ball_Punch.swf" menu="false" quality="high" width="798" height="470" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /&gt;</code>
                </dd>
                <dt>Slug</dt>
                <dd class="slug">allout-offsite</dd>
                <dt>Featured</dt>
                <dd class="recommended">False</dd>
                <dt>Zip File</dt>
                <dd class="zip_url">http://games.mochiads.com/c/g/allout-offsite.zip&lt;/dd&gt;
                <dt>SWF file size</dt>
                <dd class="swf_file_size">184374</dd>
            </dl>
        </div>
    </summary>
</entry>

My code is here:

$thumbnail = $game->summary->div->a->img->attributes()->src;

However, when I run this through print_r($thumbnail), I get:

SimpleXMLElement Object
(
    [0] => DATA_I_WANT
)

No matter what I do, it always winds up being this or an empty SimpleXMLElement Object. I've tried ->src[0], ->src->{'0'}, etc. to no avail.

+1  A: 

Just cast to a string. :]

$thumbnail = (string)$game->summary->div->a->img->attributes()->src;
Philippe Gerber
A: 

try strval(game->summary->div->a->img->attributes()->src);

kgb
+2  A: 

Try simply typecasting to a string:

print_r((string) $thumbnail)

SimpleXML has a habit of being a little shady when you try to actually use the data that it collects. I'm in a habit of just typecasting everything it spits out.

Good luck!

mattbasta