No announcement yet.

Fetch Events php code

  • Filter
  • Time
  • Show
Clear All
new posts

  • Fetch Events php code

    I hope i'm in the right place, I'm a noob. Hoping someone could tell me why this code doesn't work, i just see "Error fetching event data from Betfair API":

    // Replace with your Betfair API credentials
    $api_url = "";
    $app_key = "YOUR_APP_KEY";
    $access_token = "YOUR_ACCESS_TOKEN";

    // Set the headers for the API request
    $headers = array(
    'X-Application' => $app_key,
    'X-Authentication' => $access_token,
    'Content-Type' => 'application/json',

    // Define the request parameters
    $data = array(
    'filter' => array(
    'eventTypeIds' => [7], // Horse Racing event type ID
    'maxResults' => 100, // You can adjust this to limit the number of results

    // Convert the request parameters to JSON
    $json_data = json_encode($data);

    // Initialize cURL session
    $ch = curl_init($api_url . 'listEvents/');

    // Set cURL options
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Execute the cURL request
    $response = curl_exec($ch);

    // Check for errors
    if (curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);

    // Close the cURL session

    // Decode the JSON response
    $response_data = json_decode($response, true);

    if (isset($response_data['result'])) {
    foreach ($response_data['result'] as $event) {
    echo "Event Name: " . $event['event']['name'] . "\n";
    echo "Event ID: " . $event['event']['id'] . "\n";
    echo "---------------------------\n";
    } else {
    echo "Error fetching event data from Betfair API.";

  • #2
    You are much more likely to get a reply to this sort of question if you just post the JSON string you send down the wire to Betfair, and the JSON response string you get back. Its much easier to figure out what you are doing wrong from seeing that data.