Betfair Developers


PHP
Discussions about the widely-used general-purpose scripting language PHP that is especially suited for Web development and can be embedded into HTML.

Thread Closed
threesixes threesixes is offline
Junior Member
 
Default API with PHP & cUrl

Hello,

I want to write my own script, but without SOAP and XML. My question is: can I do this only with the function cUrl ?

I need some information to do this with cUrl. I have to send the logindata to http://www.betfair.com/publicapi/v3/BFGlobalService/ !? Is it right ?

The following fields are needed to login:
locationId=0
password=bfpassword
productId=82
username=bfusername
vendorSoftwareId=0

But in which format I have to send it ? via Cookie or via Post ? Do anybody khow how the header have to look ?

Thanks for every help.
MarkL MarkL is offline
BDP Team
 
Default

Hi,

I can't, for the life of me, imagine why you'd want to do this, but...

You'll need to send an XML document that conforms to a SOAP request that the API understands. You have the right fields. For the Login call, the XML looks something like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bfg="http://www.betfair.com/publicapi/v3/BFGlobalService/">
<soapenv:Header/>
<soapenv:Body>
<bfg:login>
<bfg:request>
<ipAddress>0</ipAddress>
<locationId>0</locationId>
<password>bfpassword</password>
<productId>82</productId>
<username>bfusername</username>
<vendorSoftwareId>0</vendorSoftwareId>
</bfg:request>
</bfg:login>
</soapenv:Body>
</soapenv:Envelope>

You can use a program called SoapUI to get an idea of what the requests look like.

You need to POST the document to:
https://api.betfair.com/global/v3/BFGlobalService
threesixes threesixes is offline
Junior Member
 
Default

Thanks for your reply! But the answer for my request is: HTTP Status 404 - cannot find ...


Here is my simple script:
PHP Code:
<?
function curl($url,$post="",$cookie="",$referer="",$useragent="MSIE",$proxy="",$header=0,$timeout=20) {
$curl curl_init();

curl_setopt($curlCURLOPT_URL$url);
curl_setopt($curlCURLOPT_HEADER$header);
curl_setopt($curlCURL_HTTP_VERSION_1_11);
curl_setopt($curlCURLOPT_TIMEOUT$timeout);

        if(
$post) {
        
curl_setopt($curlCURLOPT_POST1);
        
curl_setopt($curlCURLOPT_POSTFIELDS$post);
        }

        if (
strpos($url"https") == 0) {
        
curl_setopt($curlCURLOPT_SSL_VERIFYPEERFALSE);
        
curl_setopt($curlCURLOPT_SSL_VERIFYHOST2);
        }

        if(
$useragent)   curl_setopt($curlCURLOPT_USERAGENT$useragent);

        if(
$cookie)      curl_setopt($curlCURLOPT_COOKIE$cookie);
        if(
$referer)     curl_setopt($curlCURLOPT_REFERER$referer);

        
curl_setopt($curlCURLOPT_RETURNTRANSFER,1);

}


$post '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bfg="http://www.betfair.com/publicapi/v3/BFGlobalService/">
<soapenv:Header/>
<soapenv:Body>
<bfg:login>
<bfg:request>
<ipAddress>0</ipAddress>
<locationId>0</locationId>
<password>XXXX</password>
<productId>82</productId>
<username>XXXX</username>
<vendorSoftwareId>0</vendorSoftwareId>
</bfg:request>
</bfg:login>
</soapenv:Body>
</soapenv:Envelope>'
;

echo 
curl("https://api.betfair.com/global/v3/BFGlobalService",$post,"","","","",1);
?>

Last edited by vossie; 08-06-2010 at 08:25 PM
vossie's Avatar
vossie vossie is offline
BDP Team
 
Default

There is working curl method in the BetfairPHP framework in ./classes/betfaitutility.class.php

PHP Code:
function sendXmlOverPost($url$xml$soapAction$ip) {
          
        
$header = array();
        
$header[] = "Content-Type: text/xml";
        
$header[] = "SOAPAction: ".$soapAction;
        
$header[] = "x-ip: ".$ip;
        
        
$ch curl_init();      
          
curl_setopt($chCURLOPT_URL,            $url);
          
curl_setopt($chCURLOPT_HTTPHEADER,    $header);
          
curl_setopt($chCURLOPT_POST,            1);
          
curl_setopt($chCURLOPT_POSTFIELDS,    $xml);
          
curl_setopt($chCURLOPT_RETURNTRANSFER,1);
          
curl_setopt($chCURLOPT_TIMEOUT,        15);
          
curl_setopt($chCURLOPT_SSL_VERIFYPEER,0);
          
curl_setopt($chCURLOPT_SSL_VERIFYHOST,0);    
          
$result curl_exec($ch); // Send to remote and return data to caller.
          
curl_close($ch);
          
          
// A workaround deletes the Schema declarations, as they confuse PHP5
        
$result str_replace('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:n2="http://www.betfair.com/publicapi/types/global/v3/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'''$result);
        
$result str_replace('</soap:Envelope>'''$result);
        
$result str_replace('<n:'.$soapAction.'Response xmlns:n="http://www.betfair.com/publicapi/v3/BFGlobalService/">''<loginResponse>'$result);
        
$result str_replace('<n:'.$soapAction.'Response xmlns:n="http://www.betfair.com/publicapi/v3/BFGlobalService/">''<loginResponse>'$result);        
        
$result str_replace('n:'''$result);
        
$result str_replace('n1:'''$result);
        
$result str_replace('n2:'''$result);
        
$result str_replace('n3:'''$result);
        
$result str_replace('soap:'''$result);        
        
$result str_replace('xsi:'''$result);
        
$result str_replace('xsd:'''$result);
        
$result str_replace('<Body>'''$result);
        
$result str_replace('</Body>'''$result);
    
        if(isset(
$result)){
            return  
simplexml_load_string($result);
        }
        else {
            return 
$result;
        }
    } 
as you can see a bit of a pig, also see SoapClientExtended as it addresses some quirks in PHP5 built in soap

PHP Code:
/**
 * Extends the built in PHP SOAP class and 
 * removes Null or empty values from the request 
 * rather than setting xsi:nill=true which can 
 * cause internal error on the betfair API
 *
 */
class SoapClientExtended extends SoapClient 
__________________
"Why don't you just fix your little problem and light this candle?" - Alan B. Shepard, Jr

Click here to join Betfair today and claim your free £25 bet!
threesixes threesixes is offline
Junior Member
 
Default

@vossie: thanks for your help, but i need your help again. sorry.


Which ip, need the sendXmlOverPost function ? And what's my mistake ?

PHP Code:
<?
function sendXmlOverPost($url$xml$soapAction$ip) {

        
$header = array();
        
$header[] = "Content-Type: text/xml";
        
$header[] = "SOAPAction: ".$soapAction;
        
$header[] = "x-ip: ".$ip;

        
$ch curl_init();
          
curl_setopt($chCURLOPT_URL,            $url);
          
curl_setopt($chCURLOPT_HTTPHEADER,    $header);
          
curl_setopt($chCURLOPT_POST,            1);
          
curl_setopt($chCURLOPT_POSTFIELDS,    $xml);
          
curl_setopt($chCURLOPT_RETURNTRANSFER,1);
          
curl_setopt($chCURLOPT_TIMEOUT,        15);
          
curl_setopt($chCURLOPT_SSL_VERIFYPEER,0);
          
curl_setopt($chCURLOPT_SSL_VERIFYHOST,0);
          
$result curl_exec($ch); // Send to remote and return data to caller.
          
curl_close($ch);

          
// A workaround deletes the Schema declarations, as they confuse PHP5
        
$result str_replace('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:n2="http://www.betfair.com/publicapi/types/global/v3/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'''$result);
        
$result str_replace('</soap:Envelope>'''$result);
        
$result str_replace('<n:'.$soapAction.'Response xmlns:n="http://www.betfair.com/publicapi/v3/BFGlobalService/">''<loginResponse>'$result);
        
$result str_replace('<n:'.$soapAction.'Response xmlns:n="http://www.betfair.com/publicapi/v3/BFGlobalService/">''<loginResponse>'$result);
        
$result str_replace('n:'''$result);
        
$result str_replace('n1:'''$result);
        
$result str_replace('n2:'''$result);
        
$result str_replace('n3:'''$result);
        
$result str_replace('soap:'''$result);
        
$result str_replace('xsi:'''$result);
        
$result str_replace('xsd:'''$result);
        
$result str_replace('<Body>'''$result);
        
$result str_replace('</Body>'''$result);

        if(isset(
$result)){
            return  
simplexml_load_string($result);
        }
        else {
            return 
$result;
        }
    }


$xml '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bfg="http://www.betfair.com/publicapi/v3/BFGlobalService/">
<soapenv:Header/>
<soapenv:Body>
<bfg:login>
<bfg:request>
<ipAddress>0</ipAddress>
<locationId>0</locationId>
<password>XXXX</password>
<productId>82</productId>
<username>XXXX</username>
<vendorSoftwareId>0</vendorSoftwareId>
</bfg:request>
</bfg:login>
</soapenv:Body>
</soapenv:Envelope>'
;

echo 
sendXmlOverPost("https://api.betfair.com/global/v3/BFGlobalService"$xml"post""127.0.0.1");
?>
MarkL MarkL is offline
BDP Team
 
Default

The IP address of the machine making the request.
threesixes threesixes is offline
Junior Member
 
Default

I edited the script a little bit and now I get the sessionToken, but what have i to do now ?

How can I get All Event Types ??
PHP Code:
<?
function sendXmlOverPost($url$xml$soapAction$ip) {

        
$header = array();
        
$header[] = "Content-Type: text/xml";
        
$header[] = "SOAPAction: ".$soapAction;
        
$header[] = "x-ip: ".$ip;

        
$ch curl_init();
          
curl_setopt($chCURLOPT_URL,            $url);
          
curl_setopt($chCURLOPT_HTTPHEADER,    $header);
          
curl_setopt($chCURLOPT_POST,            1);
          
curl_setopt($chCURLOPT_POSTFIELDS,    $xml);
          
curl_setopt($chCURLOPT_RETURNTRANSFER,1);
          
curl_setopt($chCURLOPT_TIMEOUT,        15);
          
curl_setopt($chCURLOPT_SSL_VERIFYPEER,0);
          
curl_setopt($chCURLOPT_SSL_VERIFYHOST,0);
          
$result curl_exec($ch); // Send to remote and return data to caller.

          
return $result;

          
curl_close($ch);
}


$username "threesixes";
$password "XXX";
$product_id 82;
$vendorSoftwareId 0;
$clientIp "127.0.0.1";

$xml '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bfg="http://www.betfair.com/publicapi/v3/BFGlobalService/">
<soapenv:Header/>
<soapenv:Body>
<bfg:login>
<bfg:request>
<ipAddress>0</ipAddress>
<locationId>0</locationId>
<password>'
.$password.'</password>
<productId>82</productId>
<username>'
.$username.'</username>
<vendorSoftwareId>0</vendorSoftwareId>
</bfg:request>
</bfg:login>
</soapenv:Body>
</soapenv:Envelope>'
;

$login sendXmlOverPost("https://api.betfair.com/global/v3/BFGlobalService"$xml"post"$clientIp);
preg_match("/\<sessionToken xsi:type=\"xsd:string\"\>(.*?)\<\/sessionToken\>/"$login$sessionToken);

echo 
$sessionToken[1];

?>
MarkL MarkL is offline
BDP Team
 
Default

To call getAllMarkets, you'll need to construct another XML fragment like you did for Login that matches the XML needed by getAllMarkets. The getAllMarkets request has a field called "sessionToken" that you'll need to fill in with the sessionToken you got in the response to Login.

Then, call your sendXmlOverPost method using the XML.

Also, you have a small bug in the way you're using sendXmlOverPost:
$login = sendXmlOverPost("https://api.betfair.com/global/v3/BFGlobalService", $xml, "post", $clientIp);

You are passing "post" as the soapAction. This mostly doesn't matter, but some API services will not accept that. soapAction is the name of the service you're calling. So, for Login it should be "login". I.e.,

$login = sendXmlOverPost("https://api.betfair.com/global/v3/BFGlobalService", $xml, "login", $clientIp);

soapAction is just the lowercase name of the service you're calling.
Fred77 Fred77 is offline
Senior Member
 
Default

You're heading for a world of pain. I like it. It's an interesting exercise.

That sample has a dirty fix imo. Disabling SSL verification when you're passing important items like usernames and passwords is bad practice. Remove those 2 lines. If verification fails then google the fix for it rather than disabling it.

What is your reason for not wanting to use the SOAP extension?
threesixes threesixes is offline
Junior Member
 
Default

My skills for classes aren't very good, so i want it for myself. If i write it by my self , i understand the source much better.

The next step, i got all getAllEventTypes. Soccer has the id 15826207, but what in hell is the next step ? How can I view the odds ?

PHP Code:
$xml = '<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <env:Body>
    <n1:getAllEventTypes
xmlns:n1="http://www.betfair.com/publicapi/BFServiceV2/">
      <n1:request>
        <header>
          <clientStamp>0</clientStamp>
          <sessionToken>'.$sessionToken[1].'</sessionToken>
        </header>
        <locale>de</locale>
      </n1:request>
    </n1:getAllEventTypes>
  </env:Body>
</env:Envelope>';

$getAllEventTypes = str_replace(">",">\n",sendXmlOverPost("https://api.betfair.com/global/v3/BFGlobalService", $xml, "post", $clientIp));
echo $getAllEventTypes."<br><br>";
Thread Closed

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump