login PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mizar1
    Junior Member
    • Dec 2009
    • 12

    #1

    login PHP

    Hello
    anyone can help me
    I can not login with php

    I have no answer
    where am I wrong?



    code
    **************************************************


    <?php
    define('APP_KEY', 'XXXXXXXXX'); //enter your application key
    define('USERNAME', 'XXXXXX'); //enter your betfair username
    define('PASSWORD', 'XXXX'); //enter your betfair password
    define('SESSION_TOKEN', bot_login());

    echo SESSION_TOKEN;

    function bot_login() {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://identitysso.betfair.com/api/login");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Application: ' . APP_KEY,
    'Accept: application/json'
    ));

    curl_setopt($ch, CURLOPT_SSLCERT, CERT);
    $postData = '?username=' . USERNAME . '&password=' . PASSWORD;
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    echo $postData;
    $response = json_decode(curl_exec($ch));
    if (empty($response->sessionToken)) {
    echo 'No sessionToken, Response: ' . json_encode($response);
    exit(-1);
    } else {
    return $response->sessionToken;
    }
    }




    tanks
  • jabe
    Senior Member
    • Dec 2014
    • 705

    #2
    I'm trying to work out how to do with with VB.Net. I haven't written any PHP for many years.

    What did your $ch and $postData look like in the end?

    I suspect the question mark in your $postData may not be wanted.

    Comment

    • Fred77
      Junior Member
      • Jan 2009
      • 37

      #3
      Login Keepalive Logout

      Browsing the forum I see this thread has over 23000 views but no solution posted! Typically I would use curl for this but lately I've been exploring PHP's native functions so had this code to hand - it probably has a few bits that aren't needed.

      It would seem that PHP is often not configured correctly for HTTPS(SSL), it looks for cert bundles in wrong places. If you can't fix the configuration then you can temporarily tell PHP where to look with the 'capath' context option.

      This script is for secure cert based login, for automated non-interactive usage. You'll need to generate your certificate and key as per instructions in the getting started guide.

      PHP Code:
      <?php
      // Login Keepalive Logout

      // Only tested on linux
      // This is for automated login using a cert and key file that you've 
      // previously generated and registered with your betfair account. 
      // Requires additional error checking to be more robust.
      // Security warning: This script & BF_CERT & BF_KEY should not be web accessible.


      define("BF_USERNAME""");
      define("BF_PASSWORD""");
      define("BF_APP_KEY""");
      define("BF_CERT"__DIR__."/client-2048.crt"); // assumes our cert and key files
      define("BF_KEY"__DIR__."/client-2048.key");  // are in same dir as this script


      echo "Login for user " BF_USERNAME "\n";
      $opts = [
          
      'ssl' => [
      //        'capath' => '/etc/ssl/certs/',
              
      'CN_match' => 'betfair.com',
              
      'verify_peer' => true,
              
      'local_cert' => BF_CERT,
              
      'local_pk' => BF_KEY
          
      ],
          
      'http' => [
              
      'method'  => 'POST',
              
      'header'  => [
                  
      'Content-type: application/x-www-form-urlencoded',
                  
      'X-Application: ' BF_APP_KEY,
                  
      'Accept: application/json'
              
      ],
              
      'content' => http_build_query([
                  
      'username' => BF_USERNAME,
                  
      'password' => BF_PASSWORD
              
      ])
          ]
      ];
      $context  stream_context_create($opts);
      $json file_get_contents('https://identitysso.betfair.com/api/certlogin'false$context);
      $response json_decode($json);
      print_r($response);
      if (
      $response->loginStatus != "SUCCESS") die("login failed - loginStatus: {$response->loginStatus}\n");
      $mysession $response->sessionToken// we now have a session token
      echo "Ok.\n\n";



      echo 
      "Keepalive using session $mysession\n";
      $opts = [
          
      'ssl' => [
      //        'capath' => '/etc/ssl/certs/',
              
      'CN_match' => 'betfair.com',
              
      'verify_peer' => true
          
      ],
          
      'http' => [
              
      'method'  => 'GET',
              
      'header'  => [
                  
      'X-Authentication: ' $mysession,
                  
      'X-Application: ' BF_APP_KEY,
                  
      'Accept: application/json'
              
      ]
          ]
      ];
      $context  stream_context_create($opts);
      $json file_get_contents('https://identitysso.betfair.com/api/keepAlive'false$context);
      $response json_decode($json);
      print_r($response);
      if (
      $response->status != "SUCCESS") die("keepalive failed - error: {$response->error}\n");
      echo 
      "Ok.\n\n";



      echo 
      "Logout using session $mysession\n";
      $opts = [
          
      'ssl' => [
      //        'capath' => '/etc/ssl/certs/',
              
      'CN_match' => 'betfair.com',
              
      'verify_peer' => true
          
      ],
          
      'http' => [
              
      'method'  => 'GET',
              
      'header'  => [
                  
      'X-Authentication: ' $mysession,
                  
      'X-Application: ' BF_APP_KEY,
                  
      'Accept: application/json'
              
      ]
          ]
      ];
      $context  stream_context_create($opts);
      $json file_get_contents('https://identitysso.betfair.com/api/logout'false$context);
      $response json_decode($json);
      print_r($response);
      if (
      $response->status != "SUCCESS") die("logout failed - error: {$response->error}\n");
      echo 
      "Ok.\n\n";

      ?>
      Output:

      Login for user XXXXXXXXX
      stdClass Object
      (
      [sessionToken] => YYYYYYYYYYxE0/uOk7eb5h6YdVfgov6EbjQKSG727s=
      [loginStatus] => SUCCESS
      )
      Ok.

      Keepalive using session YYYYYYYYYYxE0/uOk7eb5h6YdVfgov6EbjQKSG727s=
      stdClass Object
      (
      [token] => YYYYYYYYYYxE0/uOk7eb5h6YdVfgov6EbjQKSG727s=
      [product] => ZZZZZZZZZZigHOie
      [status] => SUCCESS
      [error] =>
      )
      Ok.

      Logout using session YYYYYYYYYYxE0/uOk7eb5h6YdVfgov6EbjQKSG727s=
      stdClass Object
      (
      [token] => YYYYYYYYYYxE0/uOk7eb5h6YdVfgov6EbjQKSG727s=
      [product] => ZZZZZZZZZZigHOie
      [status] => SUCCESS
      [error] =>
      )
      Ok.

      Comment

      • hendrixback
        Junior Member
        • Jan 2015
        • 10

        #4
        Thank you for the code.

        I have the .pem certificate, but I don't have the .key or .crt ones.

        Where can i get those?

        my output is:

        stdClass Object ( [loginStatus] => CERT_AUTH_REQUIRED ) login failed - loginStatus: CERT_AUTH_REQUIRED

        Comment

        • johnlenon
          Junior Member
          • Sep 2018
          • 2

          #5
          Usually PEM is combined of .crt and .Key. Please open the .PEM file and you can create the .CRT and .KEY because i have done that.

          Comment

          • Yaresnli
            Junior Member
            • Jul 2022
            • 1

            #6
            I'm trying to figure out how to do it with VB.Net. I haven't written PHP for many years.

            What did your $ch and $postData look like at the end?

            I suspect the question mark in your $postData might be undesirable. I've been doing research on this for a long time, but I haven't found a solution yet.

            Please help.

            Comment

            • Dias
              Junior Member
              • Mar 2022
              • 1

              #7
              Hi, good afternoon everyone!

              Could someone provide me with some working in PHP?

              my biggest problem is that I only know how to work with PURE PHP, so I need something already working...

              Thanks in advance!!!

              Comment

              • EchoOne314
                Junior Member
                • Apr 2022
                • 1

                #8
                After some trial and error, this is my working code on Windows. Maybe it will be useful to someone. Creating a working cert and pem file was difficult. Only XCA worked for me. Follow this Betfair guide. Betfair doesn't mention it, but also export the pem file under the Private Keys tab.

                PHP Code:
                    // Enable curl in php.ini
                    // Find the following: ;extension=php_curl.dll or ;extension=curl or something similar
                    // Delete the semi-colon. Save the file and restart the server.
                    
                function betfairLogin() {
                        
                $ch curl_init();

                        
                curl_setopt($chCURLOPT_URL'https://identitysso-cert.betfair.com/api/certlogin');
                        
                curl_setopt($chCURLOPT_POST1);
                        
                curl_setopt($chCURLOPT_RETURNTRANSFER1);
                        
                // Absolute path worked for me.
                        
                curl_setopt($chCURLOPT_SSLKEY'C:/.../yourPemFile.pem'); // or yourKeyFile.key
                        
                curl_setopt($chCURLOPT_SSLCERT'C:/.../yourCrtFile.crt');
                        
                curl_setopt($chCURLOPT_HTTPHEADER, array(
                            
                'X-Application:YOUR_LIVE_OR_DELAYED_APP_KEY',
                            
                'Accept:application/json',
                            
                'Content-Type:application/x-www-form-urlencoded'
                        
                ));

                        
                $params = array(
                            
                'username'=>'YOUR_BETFAIR_USERNAME',
                            
                'password'=>'YOUR_BETFAIR_PASSWORD'
                        
                );

                        
                curl_setopt($chCURLOPT_POSTFIELDShttp_build_query($params));

                        
                $json json_decode(curl_exec($ch), true);

                        
                $httpStatus curl_getinfo($chCURLINFO_HTTP_CODE);

                        
                curl_close($ch);

                        if (
                $httpStatus == 200 && $json['loginStatus'] == 'SUCCESS') {
                            return 
                $json['sessionToken'];    
                        } else {
                            return 
                '';
                        }

                Comment

                Working...
                X