Forums

We want to share an important update with our Betfair Community users.

Over the coming months, the Betfair Community platform will be winding down. The final active day for the platform will be December 21st. From December 22nd, users will no longer be able to access the platform.

Your discussions, insights, engagement, and, most importantly, feedback have helped shape the platform and guided innovation. Your feedback continues to be important to us, and we will continue to listen via our dedicated feedback channels, as well as through product testing and innovations.

To share your feedback and experience, visit: https://support.betfair.com/app/home

General Betting

Welcome to Live View – Take the tour to learn more
Start Tour
There is currently 1 person viewing this thread.
scrooge_mcduck
05 Nov 10 00:17
Joined:
Date Joined: 24 Apr 08
| Topic/replies: 899 | Blogger: scrooge_mcduck's blog
I've finally decided to learn how to use the betfair APi using php and all was going quite well until I tried using the getMUbets function to return a listof markets I have bets on. I just can't seem to get it work! :(

I'd really appreciate any pointers on where I'm going wrong please. Here is my code:

function GetBets ($session){
    global $betfairlibrary;
     
    $response = $betfairlibrary->betfair_exchange_api-> betfair_get_MUbets_lite ($session); 
    return $response;
}

    $existing_bets = GetBets($session);
     
    foreach ($existing_bets as $existing_bet ){
      foreach($existing_bet->Result->betLites as $e) {
        print"Existing bets ".$e->marketId." \n ";
      }
    }
Pause • Switch to Standard View API php help needed
Show More
Loading...
Report FINE AS FROG HAIR • November 4, 2010 11:35 PM GMT
Cue Escapee.
Report scrooge_mcduck • November 4, 2010 11:45 PM GMT
Ah, he's the man to help me is he?!

I'm struggling with looping through the arrays returned but as far as I can see this is set up the same way the placebets function is and this works. I di have parameters passed in but it looks from the code that these are automatically set if not specified in this function. Really appreciate any help from those in te know on tis as I've been trying for days now trying to get this to work! :(
Report Escapee • November 5, 2010 12:53 AM GMT
Well I've never seen PHP before let alone understand it's syntax so I don't think I'm the person you want really.... Cue Fred! ( as FAFH puts it )


So bearing in mind I no nothing about PHP..... I think there are a number of thing wrong with your code.

1) Header ?
you do not appear to have assigned an api header to $session, ( you probably did this outside the code shown )

2) The betfair_get_MUbets_lite() call requires 5 mandatory parameters set before it returns any data, namely:
a) betStatus, try "MU" for matched and unmatched bets
b) orderBy, try BET_ID but there are others
c) recordCount, try 100
d) sortOrder, try DESC or ASC
e) startRecord, try 0

You don't appear to be setting these and that is the main cause of your problem.


3) Your loop to extract and print the data seems incorrect in that it is refering to structures not returned by betfair_get_MUbets_lite
This is where you really need Fred! for the specifics as the naming conventions for PHP are different to other languages ( I know that much about PHP ) but I'll outline a guesstimate of what I think it should be you maybe able to work it out yourself.

$existing_bets = GetBets($session);
/* test header->errorCode here */
foreach ($existing_bets->betLites as $betLite )
{

    print"MUBetLite BetId  ".$betLite->betId." \n ";

}

4) You really should be testing and checking the header->errorCode after every api call to confirm the call succeeded or not.




Hope this helps, but if not then you'll have to wait for a knowledgeable PHP bod to come along.
Report scrooge_mcduck • November 5, 2010 1:49 AM GMT
Thanks for taking the time to look at this Escapee, really appreciate it.

The session variable is set outside this code and is being passed through ok for my other functions so I don't think that's the problem.

I did set all the mandatory params but when I checked the function declaration it looked like these were set by default but I've added these back in now. I think it is the looping through the structures that is causing a problem. Maybe I don't need the 'result' bit but the other working functions all seem to have this in their output. I definitely need to read up a bit more on these structures but they're proving pretty tough to understand for a novice like me!

Thanks again for your suggestions, The code is still not working  but has now been updated to the following in case anyone else thinks they may be able to help:


function GetBets ($session){
    global $betfairlibrary;
   
        $betparams =  array(
        array(     
                       'betStatus' => 'MU',
                    'orderBy' => 'BET_ID',
                    'recordCount' => 100,
                    'sortOrder' => 'DESC',
                    'startRecord' => 0,

                    )
                );
   
    return $betfairlibrary->betfair_exchange_api-> betfair_get_MUbets_lite ($session,$betparams);


  $existing_bets = GetBets($session);

  foreach ($existing_bets->betLites as $betLite )
  {
    print"MUBetLite BetId  ".$betLite->betId." \n ";
  }

I am testing this over a web page andknow little about debugging either so if anyone has any tips about how I can best do this too taht would be great!

Cheers
Report FINE AS FROG HAIR • November 5, 2010 2:09 AM GMT
Mr mc duck
Escapee is no scrooge when it comes to giving free, voluntary advice on coding problems.
Report Escapee • November 5, 2010 2:47 AM GMT
This bit can't be right surely ?

       $betparams =  array(
        array(     
                       'betStatus' => 'MU',
                    'orderBy' => 'BET_ID',
                    'recordCount' => 100,
                    'sortOrder' => 'DESC',
                    'startRecord' => 0,

                    )
                );



I've seen some mad languages and syntax in my time but if PHP were like this then I can't see how it would've been let out of the mental hospital.

I think your best bet is to go to the PHP section of the developers forum and trawl that for some examples.



You mention that you had placebets working, if you post all the parts for that where you assign the header, session token, bet to be placed, call to placebet and how you deals with its response then I might be able to extrapolate a solution from the working bit(s).


or wait and hope someone appears tommorow who knows PHP & the api calls
Report Escapee • November 5, 2010 3:36 AM GMT
try This......
Place this code after bit where you log on



global $betfairlibrary;  /* Maybe you all ready have betfairlibrary declared previously ? */



$Mybets =  $betfairlibrary->betfair_exchange_api->betfair_get_MUbets_lite($session, '' );

foreach ($Mybets->betLites as $betLite )
{

    print"MUBetLite BetId  ".$betLite->betId." \n ";

}


This should give you the first 5 of your existing matched or unmatched bets.


let me know if I've cracked PHP
Report Escapee • November 5, 2010 3:38 AM GMT
didn't like this line when I posted it


$Mybets = $betfairlibrary->
betfair_exchange_api->
betfair_get_MUbets_lite($session, '' );



Join it up to one line in your code
Report Escapee • November 5, 2010 3:39 AM GMT
($session, '' ); this is two single quotes
Report FINE AS FROG HAIR • November 5, 2010 3:46 AM GMT
Give up escapee, you're out of your depth.
Please, please believe me when I say I'm just joking.
Report Escapee • November 5, 2010 4:10 AM GMT
You're right though FAFH, I know faq all about PHP and well out of my depth.

I had nothing to do during this current session of insomnia so I thought I'd give it a spin to take my mind off other things.

It appears mcDuck has gone to bed, so I'll have to wait to find out if I've managed to shelock my way into understanding and remedying the problem.

aah well...
Report FINE AS FROG HAIR • November 5, 2010 5:08 AM GMT
Escapee when you come back on after your most recent fight to try to get some decent sleep, please could you answer the following.
As you know I'm no coder ( in fact I'm essentially computer illiterate) but when I read a book about say the Facebook founder or see the film just made, I ask myself how briliant are these types of people at writing code ?.
Are they off the wall creative, really uniquey clever individuals, or are they just in the right place at the right time with the right idea.
I can't judge any of this for myself, as the world they are involved is totally alien to me.
But to you? I would expect not.
Report scrooge_mcduck • November 5, 2010 9:49 AM GMT
Solved it!You're a legend Escapee, thanks very much!

Your call to betfairlibrary worked and although I still didn't have the structure correct (still needed the result arrray and an extra pointer to MUBetLite, I was able to dump out the whole array and see how it was stuctured. Here is the working code now...

    $Mybets = $betfairlibrary->betfair_exchange_api-> betfair_get_MUbets_lite($session,'');

    print_r($Mybets);

    foreach ($Mybets->Result->betLites->MUBetLite as $betLite)
    {
        print" session ".$session."\n ";
        print"Bet: ".$betLite->marketId;
    }
Report scrooge_mcduck • November 5, 2010 9:50 AM GMT
Very pleased with this. I urge anyone to give the API a try, even if you don't have much coding experince as it's great fun!

We can all get some more sleep now :)
Report Zola's Back Heel • November 5, 2010 10:55 AM GMT
scrooge - if you are serious about getting into the API it would be worth your while investigating java.
Download the netbeans dev environment and the java examples off the bdp. google "java betfair examples" too.

if you can get your head around php, then java should not too much more effort. and you will get A LOT MORE out of it.
Report scrooge_mcduck • November 5, 2010 1:35 PM GMT
Thanks for the advice Zola, I reckon that'll be my next step.

Not done any Java since my uni days but I'm sure I can master it!
Report Zola's Back Heel • November 5, 2010 2:02 PM GMT
well if you've done java before, i wouldn't slum it with php.
get cracking on your own desktop app with client-servlet arch!
Report Escapee • November 6, 2010 2:26 AM GMT
mcDuck, wha hey..... Bingo. [:)]


Fafh, I just managed to lose a long and wordy post on the subject you raised Sad, At the point of finishing it, betfair forum crapped out and it disappeared.... I don't think I have the heart at the moment to do it all again, another time and thread maybe. Interesting topic though and I'd love to know more about your own personal expreiences and journey from employee to professor pat pending ( as it were ).
Report Fred! • November 6, 2010 3:08 AM GMT
Sorry Escapee, I missed my cue. You did a fine job :)

Yes indeed scrooge, it's fun, mucking around with the API can be more fun than gambling.
Post Your Reply
<CTRL+Enter> to submit
Please login to post a reply.

Wonder

Instance ID: 13539
www.betfair.com