Hi,
I've managed to create an app and I can get my token, but run into problem when trying the sample codes using PHP.
1-
I have the following result:
GsNSk/Y5OeQ064mwpZYBqpbCJVbSnJp5v7/89cLz78s=
Call to api-ng failed:
Response: null
2- with this one:
The result is:
vWVGgzautsE2/QFoaaDGRvbYJAWJQqMF6nRoPh0bS04=
Warning: Invalid argument supplied for foreach() in /homepages/16/d408779085/htdocs/pukkanewb/listEventType.php on line 133
Any idea?
Thank you
I've managed to create an app and I can get my token, but run into problem when trying the sample codes using PHP.
1-
PHP Code:
<?php
ob_start();
$sessionToken = getACookie();
ob_end_clean();
echo $sessionToken;
function getACookie(){
$loginEndpoint= "https://identitysso.betfair.com/api/login";
$cookie = "";
$username = "myusername";
$password = "mypassword";
$login = "true";
$redirectmethod = "POST";
$product = "home.betfair.int";
$url = "https://www.betfair.com/";
$fields = array
(
'username' => urlencode($username),
'password' => urlencode($password),
'login' => urlencode($login),
'redirectmethod' => urlencode($redirectmethod),
'product' => urlencode($product),
'url' => urlencode($url)
);
//open connection
$ch = curl_init($loginEndpoint);
//url-ify the data for the POST
$counter = 0;
$fields_string = "&";
foreach($fields as $key=>$value)
{
if ($counter > 0)
{
$fields_string .= '&';
}
$fields_string .= $key.'='.$value;
$counter++;
}
rtrim($fields_string,'&');
curl_setopt($ch, CURLOPT_URL, $loginEndpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_HEADER, true); // DO RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // DO RETURN THE CONTENTS OF THE CALL
//execute post
$result = curl_exec($ch);
echo $result;
if($result == false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
$temp = explode(";", $result);
$result = $temp[0];
$end = strlen($result);
$start = strpos($result, 'ssoid=');
$start = $start + 6;
$cookie = substr($result, $start, $end);
}
curl_close($ch);
return $cookie;
}
function sportsApingRequest($appKey, $sessionToken, $operation, $params)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://beta-api.betfair.com/rest/v1/$operation/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Application: ' . $appKey,
'X-Authentication: ' . $sessionToken,
'Accept: application/json',
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = json_decode(curl_exec($ch));
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_status == 200) {
return $response;
} else {
echo 'Call to api-ng failed: ' . "\n";
echo 'Response: ' . json_encode($response);
exit(-1);
}
}
function getAllEventTypes($appKey, $sessionToken)
{
$jsonResponse = sportsApingRequest($appKey, $sessionToken, 'listEventTypes', '{"filter":{}}');
return $jsonResponse[0]->result;
}
function extractHorseRacingEventTypeId($allEventTypes)
{
foreach ($allEventTypes as $eventType) {
if ($eventType->eventType->name == 'Horse Racing') {
return $eventType->eventType->id;
}
}
}
//echo getAllEventTypes('Yr69P8uXBTYjI2S7', $cookie);
echo getAllEventTypes('aK4fhFsx3z0bswsq', $cookie);
echo extractHorseRacingEventTypeId(getAllEventTypes('aK4fhFsx3z0bswsq', $cookie));
?>
I have the following result:
GsNSk/Y5OeQ064mwpZYBqpbCJVbSnJp5v7/89cLz78s=
Call to api-ng failed:
Response: null
2- with this one:
PHP Code:
<?php
ob_start();
$sessionToken = getACookie();
ob_end_clean();
echo $sessionToken;
function getACookie(){
$loginEndpoint= "https://identitysso.betfair.com/api/login";
$cookie = "";
$username = "myusername";
$password = "mypassword";
$login = "true";
$redirectmethod = "POST";
$product = "home.betfair.int";
$url = "https://www.betfair.com/";
$fields = array
(
'username' => urlencode($username),
'password' => urlencode($password),
'login' => urlencode($login),
'redirectmethod' => urlencode($redirectmethod),
'product' => urlencode($product),
'url' => urlencode($url)
);
//open connection
$ch = curl_init($loginEndpoint);
//url-ify the data for the POST
$counter = 0;
$fields_string = "&";
foreach($fields as $key=>$value)
{
if ($counter > 0)
{
$fields_string .= '&';
}
$fields_string .= $key.'='.$value;
$counter++;
}
rtrim($fields_string,'&');
curl_setopt($ch, CURLOPT_URL, $loginEndpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_HEADER, true); // DO RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // DO RETURN THE CONTENTS OF THE CALL
//execute post
$result = curl_exec($ch);
echo $result;
if($result == false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
$temp = explode(";", $result);
$result = $temp[0];
$end = strlen($result);
$start = strpos($result, 'ssoid=');
$start = $start + 6;
$cookie = substr($result, $start, $end);
}
curl_close($ch);
return $cookie;
}
function sportsApingRequest($appKey, $sessionToken, $operation, $params)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://beta-api.betfair.com/json-rpc");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Application: ' . $appKey,
'X-Authentication: ' . $sessionToken,
'Accept: application/json',
'Content-Type: application/json'
));
$postData =
'[{ "jsonrpc": "2.0", "method": "SportsAPING/v1.0/' . $operation . '", "params" :' . $params . ', "id": 1}]';
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = json_decode(curl_exec($ch));
curl_close($ch);
if (isset($response[0]->error)) {
echo 'Call to api-ng failed: ' . "\n";
echo 'Response: ' . json_encode($response);
exit(-1);
} else {
return $response;
}
}
function getAllEventTypes($appKey, $sessionToken)
{
$jsonResponse = sportsApingRequest($appKey, $sessionToken, 'listEventTypes', '{"filter":{}}');
return $jsonResponse;
}
function extractHorseRacingEventTypeId($allEventTypes)
{
foreach ($allEventTypes as $eventType) {
if ($eventType->eventType->name == 'Horse Racing') {
return $eventType->eventType->id;
}
}
}
echo extractHorseRacingEventTypeId(getAllEventTypes('aK4fhFsx3z0bswsq',$cookie));
?>
vWVGgzautsE2/QFoaaDGRvbYJAWJQqMF6nRoPh0bS04=
Warning: Invalid argument supplied for foreach() in /homepages/16/d408779085/htdocs/pukkanewb/listEventType.php on line 133
Any idea?
Thank you
Comment