Fetch Events php code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sats
    Junior Member
    • Oct 2023
    • 4

    #1

    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 = "https://api.betfair.com/exchange/betting/rest/v1.0/";
    $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
    curl_close($ch);

    // 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.";
    }


  • geoffw123
    Senior Member
    • Mar 2014
    • 250

    #2
    Hi
    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.

    Comment

    • bfexplorer
      Senior Member
      • Sep 2018
      • 212

      #3
      listEvents accepts object not array when you form data:

      Originally posted by sats View Post
      // 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
      );

      Comment

      Working...
      X