Sort not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newbie99
    Junior Member
    • Dec 2018
    • 62

    #1

    Sort not working

    Hi,

    I have a very frustrating issue that I just can't work out.

    I have looked at the sample PHP code and created something similar, the data returns correctly, as expected...with the exception of sorting.

    Whatever I put in the 'params' (whether I hard code or set dynamically) has absolutely no effect on the results coming back.

    The results are 'correct' aside from the sort/filter, so the issue must lie with the way I'm formatting this section of the code, but given I've essentially copied it from the sample code I'm a bit lost!

    The line in question is as follows (with the params section hard coded for visibility):

    $postData = '[{"jsonrpc": "2.0", "method": "'.$method.'", "params" : {"filter":{"sortDir":"LATEST_TO_EARLIEST", "recordCount":"10"}}, "id": 1}]';

    the $method variable works correctly, otherwise I wouldn't be receiving the correct data, but if it would help to post additional code of course its not a problem (I'm just trying to keep it simple)!
  • newbie99
    Junior Member
    • Dec 2018
    • 62

    #2
    Sorry, for clarity, that is using the sample code from the Betfair API site, but I presume all PHP jsonrpc's for this will be the same structure so it hopefully makes sense!

    Comment

    • jabe
      Senior Member
      • Dec 2014
      • 705

      #3
      You need to tell it which piece of data you are sorting on. If you want your results sorted, you must use the OrderBy parameter. The SortDir parameter just specifies the sort direction of the data field in the OrderBy parameter.

      Are you aware that the OrderBy and SortDir parameters only apply to the listCurrentOrders call and no other? I ask because you have parameterised the $method in your code.

      Comment

      • newbie99
        Junior Member
        • Dec 2018
        • 62

        #4
        Yes, sorry it is for listCurrentOrders, I did try including OrderBy and had no luck either, hence I took it out. Here is the string in full (with orderBy put back in), which may be more transparent:

        $url = "https://api.betfair.com/exchange/betting/json-rpc/v1";

        ...initiate curl request etc...all works fine, ignoring the sortDir/OrderBy...

        $postData = '[{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listCurrentOrders", "params" : {"filter":{"orderBy":"BY_MATCH_TIME","sortDir":"LA TEST_TO_EARLIEST", "recordCount":"10"}}, "id": 1}]';

        Even stranger (to me at least), if I use other parameters, such as wallet (when calling for the account details for example) that seems to pass correctly.

        Comment

        • jabe
          Senior Member
          • Dec 2014
          • 705

          #5
          I'll set up a test and see what I can work it out, but it might not be today, I'm afraid.

          In the meantime, could you try adding orderProjection = ALL?
          Last edited by jabe; 20-12-2018, 03:40 PM.

          Comment

          • newbie99
            Junior Member
            • Dec 2018
            • 62

            #6
            Thank you, that would be greatly appreciated.

            For the avoidance of doubt you presumably mean "orderProjection": "ALL"?

            I tried that previously with no luck, so I took it out, but just tried again and still no impact.

            Its very odd, as its as if the string is being ignored, but that's not the case as when I deliberately invalidate the string (e.g. "orderProjection"="ALL") it just comes up with a blank screen. So it appears the string I'm submitting is valid, but is not somehow returning the data as expected. Very odd.

            Comment

            • newbie99
              Junior Member
              • Dec 2018
              • 62

              #7
              Ah, I think I may have solved it...

              Instead of:

              $postData = '[{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listCurrentOrders", "params" : {"filter":{"orderBy":"BY_MATCH_TIME","sortDir": "LA TEST_TO_EARLIEST", "recordCount":"10"}}, "id": 1}]';

              I changed the place of the second to last } as follows (removed from after "10"} and moved to after "filter":{:

              $postData = '[{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listCurrentOrders", "params" : {"filter":{},"orderBy":"BY_MATCH_TIME","sortDir":" LA TEST_TO_EARLIEST", "recordCount":"10"}, "id": 1}]';

              I think (but may be wrong), that the way you need to send params is split into 2 parts, firstly any filters (e.g. market ID etc) and then any sorts.

              That appears to work for me, but I hope I've understood why its now working (and hopefully this will be useful to someone else too as I don't think its overly clear from the documentation).

              Comment

              • Jacky
                Junior Member
                • Dec 2018
                • 21

                #8
                Originally posted by newbie99 View Post
                Ah, I think I may have solved it...

                Instead of:

                $postData = '[{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listCurrentOrders", "params" : {"filter":{"orderBy":"BY_MATCH_TIME","sortDir": "LA TEST_TO_EARLIEST", "recordCount":"10"}}, "id": 1}]';

                I changed the place of the second to last } as follows (removed from after "10"} and moved to after "filter":{:

                $postData = '[{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listCurrentOrders", "params" : {"filter":{},"orderBy":"BY_MATCH_TIME","sortDir":" LA TEST_TO_EARLIEST", "recordCount":"10"}, "id": 1}]';

                I think (but may be wrong), that the way you need to send params is split into 2 parts, firstly any filters (e.g. market ID etc) and then any sorts.

                That appears to work for me, but I hope I've understood why its now working (and hopefully this will be useful to someone else too as I don't think its overly clear from the documentation).
                Hello newbie99,
                nice to meet you! I am also PHP programer. I am in trouble in requesting API after login successfully, whatever I call any betting API, I got timeout connection, I confused for days, could you share your php code with me, I will thank you very much, I only want how success calling betting API.
                Or any other something.

                Comment

                • jabe
                  Senior Member
                  • Dec 2014
                  • 705

                  #9
                  Originally posted by newbie99 View Post
                  Ah, I think I may have solved it...

                  Instead of:

                  $postData = '[{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listCurrentOrders", "params" : {"filter":{"orderBy":"BY_MATCH_TIME","sortDir": "LA TEST_TO_EARLIEST", "recordCount":"10"}}, "id": 1}]';

                  I changed the place of the second to last } as follows (removed from after "10"} and moved to after "filter":{:

                  $postData = '[{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listCurrentOrders", "params" : {"filter":{},"orderBy":"BY_MATCH_TIME","sortDir":" LA TEST_TO_EARLIEST", "recordCount":"10"}, "id": 1}]';

                  I think (but may be wrong), that the way you need to send params is split into 2 parts, firstly any filters (e.g. market ID etc) and then any sorts.

                  That appears to work for me, but I hope I've understood why its now working (and hopefully this will be useful to someone else too as I don't think its overly clear from the documentation).
                  Looks like you've got it. This worked:

                  "filter": {},"orderBy":"BY_BET","sortDir":"LATEST_TO_EARLIES T"

                  Comment

                  • newbie99
                    Junior Member
                    • Dec 2018
                    • 62

                    #10
                    Originally posted by Jacky View Post

                    Hello newbie99,
                    nice to meet you! I am also PHP programer. I am in trouble in requesting API after login successfully, whatever I call any betting API, I got timeout connection, I confused for days, could you share your php code with me, I will thank you very much, I only want how success calling betting API.
                    Or any other something.
                    Hi Jacky,

                    Happy to share, but my file is a mix of html & php, rather than separating it out, so may not be exactly what you're after.

                    Do you just want the functions (can post the whole code, but most of it is probably useless to you)?

                    Comment

                    • Jacky
                      Junior Member
                      • Dec 2018
                      • 21

                      #11
                      Originally posted by newbie99 View Post

                      Hi Jacky,

                      Happy to share, but my file is a mix of html & php, rather than separating it out, so may not be exactly what you're after.

                      Do you just want the functions (can post the whole code, but most of it is probably useless to you)?
                      Hi newbie99,
                      Thank you for your help!
                      If possible, could you share your whole package of files which can run successfully, I need check what is different between yours and mine. excluding everything about your personal infomation. Thank you for your help!
                      My email address is kwaninmacau#gmail.com (#->@)

                      Comment

                      • newbie99
                        Junior Member
                        • Dec 2018
                        • 62

                        #12
                        Originally posted by Jacky View Post

                        Hi newbie99,
                        Thank you for your help!
                        If possible, could you share your whole package of files which can run successfully, I need check what is different between yours and mine. excluding everything about your personal infomation. Thank you for your help!
                        My email address is kwaninmacau#gmail.com (#->@)

                        <!--border-style:solid;
                        border-width:0.5px;
                        border-color:black;
                        margin:-0.5px;
                        color:#ff9900;
                        background-color:#2e2e1f;
                        --!>

                        <style>
                        table {
                        border-collapse:collapse;
                        border-spacing:0px;
                        border-width:0.5px;
                        border-color:white;
                        vertical-align: text-top;
                        margin:0px;
                        white-space:nowrap;
                        align:center;
                        width:100%;
                        }
                        th, td {
                        border-collapse:collapse;
                        border-spacing:0px;
                        border-width:0.5px;
                        border-color:white;
                        vertical-align: text-top;
                        margin:0px;
                        white-space:nowrap;
                        align:center;
                        width:100%;
                        }
                        body {
                        margin:0px;
                        padding:0px;
                        }
                        </style>


                        <?php

                        $app_key = [INSERT APP KEY HERE];
                        $session_token = [INSERT SESSION TOKEN HERE];
                        $request_type = 'account';
                        $request_name = 'getAccountFunds';
                        $paramaters ='{"wallet":"UK"}';
                        //$paramaters = '{"filter":{}}';

                        $account_data= json_decode(json_encode(betfair_request($app_key, $session_token, $request_type, $request_name, $paramaters)), true);

                        $balance = $account_data['availableToBetBalance'];
                        $exposure = $account_data['exposure'];
                        $exposure_limit = $account_data['exposureLimit'];
                        $exposure_per = abs($account_data['exposure'])/abs($account_data['exposureLimit'])*100;
                        $discount_rate = $account_data['discountRate'];
                        $points = $account_data['discountRate'];
                        $wallet = $account_data['wallet'];

                        $request_type = 'betting';
                        $request_name = 'listCurrentOrders';
                        $paramaters ='{"filter":{},"orderBy":"BY_MATCH_TIME","sortDir" :"LATEST_TO_EARLIEST", "orderProjection":"ALL","recordCount":"10","dateRa nge":{}}';
                        //"dateRange": {"from":"' . date('c') . '"},

                        $bet_data = json_decode(json_encode(betfair_request($app_key, $session_token, $request_type, $request_name, $paramaters)), true);
                        //echo var_dump($bet_data);
                        //echo var_dump($account_data);

                        $request_type = 'betting';
                        $request_name = 'listMarketBook';
                        $market_ids = '1.117991336';
                        $paramaters ='{"marketIds":['.$market_ids.'],"PriceProjection":["SP_AVAILABLE","SP_TRADED","EX_ALL_OFFERS"],"orderProjection":"EXECUTABLE", "matchProjection":"NO_ROLLUP"}';

                        $market_book = json_decode(json_encode(betfair_request($app_key, $session_token, $request_type, $request_name, $paramaters)), true);

                        var_dump($market_book);

                        ?>

                        <body>

                        <table>
                        <tr style='color:#ff9900;background-color:#2e2e1f;'>
                        <td>Cash Available</td>
                        <td>Exposure</td>
                        <td>Exposure Limit</td>
                        <td>Exposure %</td>
                        <td>Discount Rate</td>
                        <td>Points</td>
                        <td>Wallet</td>
                        </tr>
                        <tr style='color:#ff9900;background-color:#2e2e1f;'>
                        <?php echo "<td>".$balance."</td>"; ?>
                        <?php echo "<td>".$exposure."</td>"; ?>
                        <?php echo "<td>".$exposure_limit."</td>"; ?>
                        <?php echo "<td>".$exposure_per."</td>"; ?>
                        <?php echo "<td>".$discount_rate."</td>"; ?>
                        <?php echo "<td>".$points."</td>"; ?>
                        <?php echo "<td>".$wallet."</td>"; ?>
                        </tr>
                        </table>

                        </body>

                        <?php

                        echo "<table><tr style='color:#ff9900;background-color:#2e2e1f;'>";
                        echo "<td>Bet ID</td>";
                        echo "<td>Market ID</td>";
                        echo "<td>Selection ID</td>";
                        echo "<td>Handicap</td>";
                        echo "<td>Price</td>";
                        echo "<td>Size</td>";
                        echo "<td>bspLiability</td>";
                        echo "<td>side</td>";
                        echo "<td>status</td>";
                        echo "<td>persistence type</td>";
                        echo "<td>order type</td>";
                        echo "<td>Placed Date</td>";
                        echo "<td>Matched Date</td>";
                        echo "<td>Average Price Matched</td>";
                        echo "<td>Size Matched</td>";
                        echo "<td>Size Remaining</td>";
                        echo "<td>Size Lapsed</td>";
                        echo "<td>Size Cancelled</td>";
                        echo "<td>Size Voided</td>";
                        //echo "<td>Regulator</td>";
                        echo "</tr>";


                        foreach ($bet_data['currentOrders'] as $i) {

                        if($i['side']=='LAY')
                        {
                        echo "<tr style='background-color:#ff80c0;color:#000000;'>";
                        }
                        else
                        {
                        echo "<tr style='background-color:#389cc7;color:#ffffff;'>";
                        }

                        $market_id = $i['marketId'];
                        $request_type = 'betting';
                        $request_name = 'listMarketCatalogue';
                        $paramaters ='{"filter":{"marketIds":["'.$market_id.'"]},"marketProjection":["RUNNER_DESCRIPTION"],"maxResults": "200"}';
                        $market_data= json_decode(json_encode(betfair_request($app_key, $session_token, $request_type, $request_name, $paramaters)), true);
                        $runners = filter_array($market_data[0]['runners'],$i['selectionId'],'selectionId');

                        echo "<td>".$i['betId']."</td>";
                        // echo "<td>".$market_data[0]['marketName']."</td>";
                        echo "<td>".$market_data[0]['marketName']."(".$i['marketId'].")</td>";
                        echo "<td>".$runners[0]['runnerName']."(".$i['selectionId'].")</td>";
                        echo "<td>".$i['handicap']."</td>";
                        echo "<td>".$i['priceSize']['price']."</td>";
                        echo "<td>".$i['priceSize']['size']."</td>";
                        echo "<td>".$i['bspLiability']."</td>";
                        echo "<td>".$i['side']."</td>";
                        echo "<td>".$i['status']."</td>";
                        echo "<td>".$i['persistenceType']."</td>";
                        echo "<td>".$i['orderType']."</td>";
                        echo "<td>".$i['placedDate']."</td>";
                        echo "<td>".$i['matchedDate']."</td>";
                        echo "<td>".$i['averagePriceMatched']."</td>";
                        echo "<td>".$i['sizeMatched']."</td>";
                        echo "<td>".$i['sizeRemaining']."</td>";
                        echo "<td>".$i['sizeLapsed']."</td>";
                        echo "<td>".$i['sizeCancelled']."</td>";
                        echo "<td>".$i['sizeVoided']."</td>";
                        // echo "<td>".$i['regulatorCode']."</td>";
                        echo "</tr>";
                        }
                        echo "</table>";


                        function filter_array($array,$term,$field){
                        $matches = array();
                        foreach($array as $a){
                        if($a{$field} == $term)
                        $matches[]=$a;
                        }
                        return $matches;
                        }


                        function betfair_request($app_key, $session_token, $request_type, $request_name, $paramaters)
                        {
                        $jsonResponse = betfair_submit($app_key, $session_token, $request_type, $request_name, $paramaters);
                        return $jsonResponse[0]->result;
                        }



                        function betfair_submit($app_key, $session_token, $request_type, $request_name, $paramaters)
                        {

                        if ($request_type=='account') {
                        $url = "https://api.betfair.com/exchange/account/json-rpc/v1";
                        $method = "AccountAPING/v1.0/";
                        $method .= $request_name;
                        }
                        else {
                        $url = "https://api.betfair.com/exchange/betting/json-rpc/v1";
                        $method = "SportsAPING/v1.0/";
                        $method .= $request_name;
                        }


                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_POST, 1);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                        //Need to change (insecure)
                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);



                        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                        'X-Application: ' . $app_key,
                        'X-Authentication: ' . $session_token,
                        'Accept: application/json',
                        'Content-Type: application/json'
                        ));

                        $postData = '[{"jsonrpc": "2.0", "method": "'.$method.'", "params" : '.$paramaters.', "id": 1}]';
                        //$postData = '[{"jsonrpc": "2.0", "method": "'.$method.'", "params" : {"filter":{},"orderBy":"BY_MATCH_TIME","sortDir":" LATEST_TO_EARLIEST", "orderProjection":"ALL","recordCount":"10","dateRa nge":{}}, "id": 1}]';

                        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

                        debug('Post Data: ' . $postData);
                        $response = json_decode(curl_exec($ch));
                        debug('Response: ' . json_encode($response));
                        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 debug($debugString)
                        {
                        global $DEBUG;
                        if ($DEBUG)
                        echo $debugString . "\n\n";
                        }

                        ?>



                        The above works for me (you just need to insert your App & Session keys), but it is very much a work in progress, very messy, incomplete, random hard-coded bits and all the other rubbish that comes with a work in progress!

                        Comment

                        Working...
                        X