Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
Like Tree1Likes

Thread: Help Parsing bookmakers XML feed

  1. #1
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    Hi,

    Im trying to extract the betting odds from an XML feed provided by an online bookmaker. I've been following this tutorial to begin with; http://ditio.net/2008/06/19/using-ph...-rss-feed-xml/

    I seem to be able to read the entire XML feed into an object but I cant quite figure out how to extract the results from it, I seem to have a complete black spot in my head when it comes to objects and arrays.

    Heres what Ive got so far:

    Code:
    $ch = curl_init("http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=MR&filterBIR=N");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    $data = curl_exec($ch);
    curl_close($ch);
    
    $doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
    
    print_r($doc);
    
    if(isset($doc->channel))
    {
        parseRSS($doc);
    }
    if(isset($doc->entry))
    {
        parseAtom($doc);
    }
    function parseRSS($xml)
    {    
    echo "RSS";
        echo "<strong>".$xml->channel->title."</strong>";
        $cnt = count($xml->channel->item);
        for($i=0; $i<$cnt; $i++)
        {
        $url     = $xml->channel->item[$i]->link;
        $title     = $xml->channel->item[$i]->title;
        $desc = $xml->channel->item[$i]->description;
     
        echo '<a href="'.$url.'">'.$title.'</a>'.$desc.'';
        }
    }
    
    function parseAtom($xml)
    {
    echo "Atom";
    
        echo "<strong>".$xml->author->name."</strong>";
        $cnt = count($xml->entry);
        for($i=0; $i<$cnt; $i++)
        {
        $urlAtt = $xml->entry->link[$i]->attributes();
        $url    = $urlAtt['href'];
        $title     = $xml->entry->title;
        $desc    = strip_tags($xml->entry->content);
     
        echo '<a href="'.$url.'">'.$title.'</a>'.$desc.'';
        }
    }
    I think half of that is redundant as I dont think the feed is either RSS or ATOM so I added in an extra function just to check it was doing something:
    Code:
    parseOther($doc);
    
    function parseOther($xml)
    {    
    echo "Niether";
        echo "<strong>".$xml->market->participant ."</strong>";
    }
    This then gave an error.

    Can anyone help me with the loop to extract the participent, odds and link from the object?

    Thanks

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    Can you post the actual XML result from that feed?
    I can't access it behind the firewall I'm at (because it flags as gambling).


  4. #3
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    Sorry, I can't even get to the print_r without a fatal error.
    I don't think my webhost supports SimpleXmlElement.
    Someone else will have to answer one.


  5. #4
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    Cheers for trying mate. Ive attached the XML file incase someone else can have a look

  6. #5
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    Try this:

    PHP Code:
    <?php

    class William_Hill_WHDN {

        private 
    $_serviceUri 'http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=MR&filterBIR=N',
                
    $_xmlElement,
                
    $_classes;

        
    /**
        * Takes in the URI if there is one and then builds the object
        */
        
    public function __construct($service_uri NULL) {

            if(
    $service_uri)
                
    $this->_serviceUri $service_uri;

            
    $this->_xmlElement $this->_fetchXMLElement();

            
    $this->_formatXML();

        }

        
    /**
        * Returns the classes array
        */
        
    public function getClasses() {

            return (array) 
    $this->_classes;

        }

        
    /**
        * Fetches the XML data and returns it as a SimpleXmlElement
        */
        
    private function _fetchXMLElement() {

            if(
    stristr($this->_serviceUri'http://')) {
                
    $ch curl_init($this->_serviceUri);
                
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
                
    curl_setopt($chCURLOPT_HEADER0);
                
    $data curl_exec($ch);
                
    curl_close($ch);
            } else {
                
    $data file_get_contents($this->_serviceUri);
            }

            if(
    $data)
                return new 
    SimpleXmlElement($data);
            else
                throw new 
    ErrorException("No data returned from URL");

        }

        
    /**
        * Formats all of the XML classes
        */
        
    private function _formatXML() {

            
    $williamhill $this->_xmlElement->response->williamhill;

            
    $classes = array();

            foreach(
    $williamhill->children() as $key => $class) {

                
    $classes[(int) $class->attributes()->id] = $this->_createTypes($class);

            }

            if(
    count($classes) > 0)
                
    $this->_classes $classes;
            else
                throw new 
    ErrorException("No classes found");

        }

        
    /**
        * Neatly formats the types into an array of stdClass
        */
        
    private function _createTypes(SimpleXmlElement $class) {

            
    $types = array();

            foreach(
    $class->children() as $type) {

                
    $type_formatted = (object) array(
                    
    'name' => (string) $type->attributes()->name,
                    
    'url'  => (string) $type->attributes()->url,
                    
    'lastUpdateDate' => (string) $type->attributes()->lastUpdateDate,
                    
    'markets' => (array) $this->_createMarkets($type)
                );

                
    $types[(int) $type->attributes()->id] = $type_formatted;

            }

            return (array) 
    $types;

        }

        
    /**
        * Neatly formats the markets into an array of stdClass
        */
        
    private function _createMarkets(SimpleXmlElement $type) {

            
    $markets = array();

            foreach(
    $type->children() as $market) {

                
    $market_formatted = (object) array(
                    
    'id' => (string) $market->attributes()->id,
                    
    'name' => (string) $market->attributes()->name,
                    
    'betTillTime' => (string) $market->attributes()->betTillTime,
                    
    'participants' => (array) $this->_createParticipants($market)
                );

                
    $markets[(int) $market->attributes()->id] = $market_formatted;

            }

            return (array) 
    $markets;

        }

        
    /**
        * Neatly formats the participants into an array of stdClass
        */
        
    private function _createParticipants(SimpleXmlElement $market) {

            
    $participants = array();

            foreach(
    $market->children() as $participant) {

                
    $participant_formatted = (object) array(
                    
    'name' => (string) $participant->attributes()->name,
                    
    'id' => (int) $participant->attributes()->id,
                    
    'odds' => (string) $participant->attributes()->odds,
                    
    'oddsDecimal' => (double) $participant->attributes()->oddsDecimal,
                );

                
    $participants[(int) $participant->attributes()->id] = $participant_formatted;

            }

            return (array) 
    $participants;

        }
    }
    ?>

    <?php
    /* Here we use the class */
    $whdn = new William_Hill_WHDN('whdn.williamhill.com.pricefeed.xml');
    $classes $whdn->getClasses();
    ?>

    <ul>
      <?php /*For the first class (there's only one anyway)*/ ?>
      <?php foreach(reset($classes) as $class) { ?>
      <li>
        <p><?php echo $class->name?></p>
        <ul>
          <?php /*For every market within the class*/ ?>
          <?php foreach($class->markets as $market) { ?>
          <li>
            <p>Market Name: <?php echo $market->name ?>
              <ul>
                <?php /*For every participant within the market*/ ?>
                <?php foreach($market->participants as $participant) { ?>
                    <li>
                        <p><?php echo $participant->name?> (<?php echo $participant->odds?>)</p>
                    </li>
                <?php ?>
              </ul>
            </li>
            <?php ?>
          </ul>
        </li>
        <?php ?>
    </ul>
    A zip is attached with the php and xml file as well.
    medlington and sw1tchmx like this.
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  7. #6
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    Thanks for that its a great help, the only issue I have is that it seems to work with the xml file but not with a URL?

    I tried changing this line:
    Code:
    $whdn = new William_Hill_WHDN('whdn.williamhill.com.pricefeed.xml');
    ]

    to this:
    Code:
    $whdn = new William_Hill_WHDN('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=MR&filterBIR=N');
    But it throws these errors:

    Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Start tag expected, '<' not found in /homepages/30/d377292671/htdocs/footy/temp/index.php on line 48

    Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]:  in /homepages/30/d377292671/htdocs/footy/temp/index.php on line 48

    Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /homepages/30/d377292671/htdocs/footy/temp/index.php on line 48

    Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /homepages/30/d377292671/htdocs/footy/temp/index.php:48 Stack trace: #0 /homepages/30/d377292671/htdocs/footy/temp/index.php(48): SimpleXMLElement->__construct('????????????ms$...') #1 /homepages/30/d377292671/htdocs/footy/temp/index.php(17): William_Hill_WHDN->_fetchXMLElement() #2 /homepages/30/d377292671/htdocs/footy/temp/index.php(152): William_Hill_WHDN->__construct('http://whdn.wil...') #3 {main} thrown in /homepages/30/d377292671/htdocs/footy/temp/index.php on line 48

    Any ideas?

    and thanks again

  8. #7
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    ah I think Im just having Monday morning issues.

    I swapped it so it has double quotes around the URL and it seems to be working now.

    Code:
    $whdn = new William_Hill_WHDN("http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=MR&filterBIR=N");
    Is that how I should be using it?

    Now just to figure out the other 10 or so booky feeds and Ill be set to go.

    Cheers

  9. #8
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    Yeah that looks right. There are a few levels you should add, you should make it cache the XML and update the cache at scheduled times for example. These things have a habit of failing every now and then!

    Alternatively you could cache the data you want rather than the XML. So you could pull out all the data and store it in a few SQL tables for example. Whichever works best for you!

    This stuff has a tendency to get messy and long in the code, so I'd try and keep it as tidy as possible. Split it out to multiple levels of inheritance if possible etc.
    medlington likes this.
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  10. #9
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    Thanks again mate,

    Yeah Im intending to rip out the relevant data and store it all in a MySQL database for use in a comparison script later on

  11. #10
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    Hi,

    Im so close now but have run into one last issue. Im writing the info into a database which I have no problem with but I cant seem to get the URL, the date or the time into variables so that I can write them.

    I added the following code:
    Code:
                    'date'  => (string) $type->attributes()->date,
                    'time'  => (string) $type->attributes()->time
    into the block starting:
    private function _createTypes(SimpleXmlElement $class) {

    but then was unsure if I needed to add it to the next block too? I did but it doesnt seem to work.

    I also tried to read the url into a variable with the following code:

    Code:
                $url = $market->url;
    but again it doesnt seem to work, I tried
    Code:
                $url = $participant->url;
    and that threw an error but it might have been a connection error rather than a code error.

    My new full code is here:
    Code:
                <?php
    
    class William_Hill_WHDN {
    
        private $_serviceUri = "http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=MR&filterBIR=N",
                $_xmlElement,
                $_classes;
    
        /**
         * Takes in the URI if there is one and then builds the object
         */ 
        public function __construct($service_uri = NULL) {
    
            if($service_uri)
                $this->_serviceUri = $service_uri;
    
            $this->_xmlElement = $this->_fetchXMLElement();
    
            $this->_formatXML();
    
        }
    
        /**
         * Returns the classes array
         */ 
        public function getClasses() {
    
            return (array) $this->_classes;
    
        }
    
        /**
         * Fetches the XML data and returns it as a SimpleXmlElement
         */ 
        private function _fetchXMLElement() {
    
            if(stristr($this->_serviceUri, 'http://')) {
                $ch = curl_init($this->_serviceUri);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                $data = curl_exec($ch);
                curl_close($ch);
            } else {
                $data = file_get_contents($this->_serviceUri);
            }
    
            if($data)
                return new SimpleXmlElement($data);
            else
                throw new ErrorException("No data returned from URL");
    
        }
    
        /**
         * Formats all of the XML classes
         */ 
        private function _formatXML() {
    
            $williamhill = $this->_xmlElement->response->williamhill;
    
            $classes = array();
    
            foreach($williamhill->children() as $key => $class) {
    
                $classes[(int) $class->attributes()->id] = $this->_createTypes($class);
    
            }
    
            if(count($classes) > 0)
                $this->_classes = $classes;
            else
                throw new ErrorException("No classes found");
    
        }
    
        /**
         * Neatly formats the types into an array of stdClass
         */ 
        private function _createTypes(SimpleXmlElement $class) {
    
            $types = array();
    
            foreach($class->children() as $type) {
    
                $type_formatted = (object) array(
                    'name' => (string) $type->attributes()->name,
                    'url'  => (string) $type->attributes()->url,
                    'lastUpdateDate' => (string) $type->attributes()->lastUpdateDate,
                    'markets' => (array) $this->_createMarkets($type),
                    'date'  => (string) $type->attributes()->date,
                    'time'  => (string) $type->attributes()->time
                );
    
                $types[(int) $type->attributes()->id] = $type_formatted;
    
            }
    
            return (array) $types;
    
        }
    
        /**
         * Neatly formats the markets into an array of stdClass
         */ 
        private function _createMarkets(SimpleXmlElement $type) {
    
            $markets = array();
    
            foreach($type->children() as $market) {
    
                $market_formatted = (object) array(
                    'id' => (string) $market->attributes()->id,
                    'name' => (string) $market->attributes()->name,
                    'betTillTime' => (string) $market->attributes()->betTillTime,
                    'date'  => (string) $type->attributes()->date,
                    'time'  => (string) $type->attributes()->time,
                    'participants' => (array) $this->_createParticipants($market)
                );
    
                $markets[(int) $market->attributes()->id] = $market_formatted;
    
            }
    
            return (array) $markets;
    
        }
    
        /**
         * Neatly formats the participants into an array of stdClass
         */ 
        private function _createParticipants(SimpleXmlElement $market) {
            
            $participants = array();
    
            foreach($market->children() as $participant) {
    
                $participant_formatted = (object) array(
                    'name' => (string) $participant->attributes()->name,
                    'id' => (int) $participant->attributes()->id,
                    'odds' => (string) $participant->attributes()->odds,
                    'oddsDecimal' => (double) $participant->attributes()->oddsDecimal
                );
    
                $participants[(int) $participant->attributes()->id] = $participant_formatted;
    
            }
    
            return (array) $participants;
    
        }
    }
    ?>
    
    <?php
    
    /* Here we use the class */
    $whdn = new William_Hill_WHDN("http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=MR&filterBIR=N");
    $classes = $whdn->getClasses();
    
    
    include '../admin/sql/config.php';
    include '../admin/sql/opendb.php';
    
    // testing writing to the DB ?>
      <?php foreach(reset($classes) as $class) { ?>
        <?php echo $class->name; ?>
     
          <?php /*For every market within the class*/ ?>
          <?php foreach($class->markets as $market) { ?>
    
                <?php /*For every participant within the market*/ ?>
                <?php foreach($market->participants as $participant) { ?>
                
                <? 
                // write to DB
                 $marketname = $market->name; 
                 $participantname = $participant->name; 
                 $participantodds = $participant->odds; 
                 $participantoddsdecimal = $participant->oddsDecimal; 
                $url = $market->url; 
                
                        mysql_query("INSERT INTO williamhill_odds
                    (market,
                    participant,
                    odds_decimal ,
                    odds_fractional,
                    date,
                    url
                    ) VALUES(
                    '$marketname',
                    '$participantname',
                    '$participantodds',
                    '$participantoddsdecimal',
                    '$date',
                    '$url'
                    ) ")
                    or die(mysql_error());         
                
                        echo $marketname . " - " . $participant->name; ?> (<?php echo $participant->odds . ")<br />"; ?>
                <?php } ?>
            <?php } ?>
        <?php } ?>
    Any ideas?


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search tags for this page

bookie parser
,
bookmaker xml feed
,
bookmakers xml feed
,
get william hill xml to database
,
parser odds xml feed
,
rss parsers php compare
,

william hill xml

,
william hill xml odds
,
williamhill xml parser
,
xml files from bookmakers
Click on a term to search for related topics.
All times are GMT -6. The time now is 04:41 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com