Some rants about technology, PHP, people, tools, web etc from a developers perspective. Feel free to copy ideas, code or anything from here.

Saturday, November 26, 2005

Accessing latest Betfair API through PHP

4 comments
Got succesful in accessing betfairs latest api in php. At first i tried using NuSoap webservice package but couldnt pull it off.

Then i saw someone do it in Perl by sending (Post?) Raw XML to
http://www.betfair.com/publicapi/BFService/

This is when i gave it a try in php and voila success! Here is what i did:


$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, API_URL ); // set url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // times out after 1 min
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml ); // add POST fields

curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

$header[] = "SOAPAction: ". API_URL;
$header[] = "MIME-Version: 1.0";
$header[] = "Content-type: text/xml; charset=utf-8";

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch); // run the whole process


if (curl_errno($ch))
{
print curl_error($ch);
return 0;
}
else
{

//now that we have got a result, parse this soap packet and return the XML doc
$parser = new soap_parser($result,'UTF-8','',TRUE);
$result = $parser->get_response();

//print_R($result);
curl_close($ch);

if($result["Result"]["header"]["errorCode"] != "NO_SESSION" )
{
update_session_key($result["Result"]["header"]["sessionToken"] );
}

return $result;
}
To this end i used the PatTemplate system to stuff values into the XML templates, and got the raw XML back. If anyone needs it i can supply the soap parser class i stole ;-). It was one of the class in NuSoap and just changed a few bits here and there.

4 comments :

roflman79 said...

I am having trouble even starting to look at using the Betfair API to grab pricing on certain markets. Where can i find the soap_parser class? Would be a great help. Thanks.

Anonymous said...

plz provide me the soap_parser...

Anonymous said...

i need soap parser class my mail address is mohib143ssg attherate of hotmail

Anonymous said...

If you want to get an easy connection to the Betfair SOAP API, you could try using bflib as showin in this php betfair bot tutorial. The framework is open source and is easy to get started with.