0

I want to parse the following XML file.

What I have so far is:

$xml = new SimpleXMLElement('http://smarkets.s3.amazonaws.com/oddsfeed.xml', LIBXML_NOCDATA, true);

foreach ($xml->odds->event as $item) {
    echo (string)$item->market;
}

But this does not work. Can you help me?

1
  • 1
    could you more accurately describe the issue; does it throw an exception, is xml null, is it an empty object etc Commented Jul 18, 2015 at 11:52

2 Answers 2

6

You can try with php CURL:

$ch = curl_init();
$url = 'http://smarkets.s3.amazonaws.com/oddsfeed.xml';

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');

$data = curl_exec($ch);
curl_close($ch);

$xml = simplexml_load_string($data);

print_r($xml);
Sign up to request clarification or add additional context in comments.

Comments

5

I have no idea which information you want to extract so here is an example how to get the attributes 'id' and 'slug' from all market nodes.

Just add compress.zlib:// to your url to get the xml, for PHP 4.3.0 and up

<?php
$xml = simplexml_load_file('compress.zlib://http://smarkets.s3.amazonaws.com/oddsfeed.xml') or die("Error: Cannot create object");

foreach ($xml->event as $item) {
    echo $item->market['id'] . "<br>" . $item->market['slug'] . "<br><br>";
}
?>

Comments

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.