xml - Php Code (Get/Fetch Kongregate Games)

980

Can you help me there is a problem that is detect by php when execute but I think my code is okay.

The error say "Catchable fatal error: Object of class stdClass could not be converted to string in C:\Program Files\wamp\www\getkongregate.php on line 46"

Website of kongregate xml: here

This is the code I have:

    <?php
        $xml = simplexml_load_string(file_get_contents('http://www.kongregate.com/games_for_your_site.xml'));
        $json = json_encode($xml);
        $games = json_decode($json);
        $count = 0;
        foreach ($games->game as $game) {
            echo $game->id . "\n";
            echo $game->title . "\n";
            echo $game->thumbnail . "\n";
            echo $game->launch_date . "\n";
            echo $game->category . "\n";
            if (array_key_exists('screenshot',$game)) {
                for($i=0;$i<=(count($game->screenshot)-1);$i++) {
                    echo $game->screenshot[$i] . "\n";
                }
            }
            echo $game->flash_file . "\n";
            echo $game->width . "\n";
            echo $game->height . "\n";
            echo $game->description . "\n";
            echo $game->instructions . "\n";
            echo $game->gameplays . "\n";
            echo $game->rating . "\n\n------\n\n";
        if ($count == 100) {break;}
        $count++;
    }
?>
440

Answer

Solution:

You are retreiving xml.

Why are you parsing it as json?

Use SimpleXmlElement instead and then iterate through xml elements to retreive your data.

$xml = file_get_contents($map_url);
$simpleXmlElement = simplexml_load_string($xml);
var_dump($simpleXmlElement);

Here: :)

$url = 'http://www.kongregate.com/games_for_your_site.xml';
$content = file_get_contents($url)
$xml = simplexml_load_string($content);

$count = 0;
foreach ($xml as $game) {

    echo strval($game->id) . PHP_EOL;
    echo strval($game->title) . PHP_EOL;
    echo strval($game->thumbnail) . PHP_EOL;
    echo strval($game->launch_date) . PHP_EOL;
    echo strval($game->category) . PHP_EOL;

    if (isset($game->screenshot) && is_array($game->screenshot)) {
        foreach ($game->screenshot as $screenshot) {
            echo strval($screenshot) . "\n";
        }
    }

    echo strval($game->flash_file) . PHP_EOL;
    echo strval($game->width) . PHP_EOL;
    echo strval($game->height) . PHP_EOL;
    echo strval($game->description) . PHP_EOL;
    echo strval($game->instructions) . PHP_EOL;
    echo strval($game->gameplays) . PHP_EOL;
    echo strval($game->rating) . PHP_EOL;

    if ($count >= 100) {
        break;
    }
    $count++;
}

People are also looking for solutions to the problem: How to pass intergers as values from an array into an IF statement in PHP?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.