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

Saturday, February 18, 2006

PHP and SOAP

4 comments
I mentioned in one of my previous posts how i accessed the betfair api. The reason i am mentioning it again is because of this site. I owe quite a lot of things to this site. When i was banging my head against the wall to find a solution to my problems i found pattemplate to be quite helpful. Not that it has anything to do with SOAP.

Here is a summary of my problem, i needed to access the api through PHP and the documentation on their site was not helpful. Then i came across some code which used NuSOAP library. The response was very slow, and also the class i used was not sufficient to handle all the operations allowed in the API. To complicate matters further the Betfair people switched to a new version of their api and i was left on my own to find a solution. Then in one of the Perl forums i found a snippet where somebody (god bless him!) showed some code on how to send a raw request to a soap server.

I latched onto that chance and digged in PHP to find equivalent solution. Here is what i did. I converted the XML request data into templates and used Pat-template system to fill-in the values. Then i used the excellent Curl library to send raw POST request to the betfair SOAP server and tada! we got response.

//function to execute curl calls
function call_curl($xml)
{
$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

if( LOCAL )
{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
}

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;
}

}//function ends



The rest as they say was history ;-) Btw if u dont know anything about Curl library i would advise u to give them a visit. It is an excellent tool. I have used it in various projects and here are a few of the uses i could find in my experience:
  • Form POSTing
  • Downloading a file and saving it to disk
  • Uploading files to a FTP server with authentication
  • Spidering!
  • Loggin in to a site and navigating through password protected areas and downloading feeds etc.
  • Telnet
  • Data transfer between servers
Thats it i guess for the moment, but before i go heres what u can expect in my coming posts: More about some other unix tools like NetCat, Dig, Socat etc and my new PHP development Framework.

4 comments :

Anonymous said...

Hi Sandeep!

I've also been wanting to connect to Betfair's API but wasn't able to with Nusoap. Would you happen to have the rest of the code you took the snippet from? I'm just not sure what's missing to plug it all in. Thanks a heap!

BTW, what sort of an application did you find for the API?

Anonymous said...

Hi Sandeep,

Sure would appreciate some assistance with that code - any time I post via CURL to the API I get a "General Internal Error" (highly descriptive error code - NOT!).

Any advice appreciated!!

code pirate said...

Hi can you post snippet of the code you are using. It might help me as i did these things around 6 months back and i dont have the access to betfair site anymore.

Cheers
sandy

Unknown said...

There is a new open source library which will handle all the soap negotiation with Betfair. The release was announced on this Betfair API release announcement. There is also a very raw demo of its functionality on the betfair api example page.

I know this post is very old, but hopefully this will help somebody who stumbles up on it.