Jump to content
  • 0

API - PHP - Post with _method: DELETE


ItsPeter

Question

Hi. I'm trying to figure out how to close the order via API on my demo account and I'm stuck. From what I was able to research, I have to use "_method: DELETE" header with POST, but doing so results with no response at all or invalid.request.format.line-2.column-51, depending on the approach.

Here's the cURL part of the code:
No response:

                    $ch = curl_init($connection_url);

                    curl_setopt($ch,CURLOPT_HTTPHEADER, array(

                        'Content-Type: application/json; charset=UTF-8',

                        'Accept: application/json; charset=UTF-8',

                        'X-IG-API-KEY: '.$key.'',

                        'Version: 1',

                        'Authorization: Bearer '.$access_token.'',

                        'IG-ACCOUNT-ID: '.$account_id.'',

                        '_method: DELETE'

                    ));

                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                    curl_setopt($ch, CURLOPT_POST, true);

                    curl_setopt($ch, CURLOPT_HEADER, true);

                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

                    $tokens = curl_exec($ch);

'Invalid request' response:

                    $ch = curl_init($connection_url);

                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                    curl_setopt($ch,CURLOPT_HTTPHEADER, array(

                        'Content-Type: application/json; charset=UTF-8',

                        'Accept: application/json; charset=UTF-8',

                        'X-IG-API-KEY: '.$key.'',

                        'Version: 1',

                        "Authorization: Bearer $access_token",

                        'IG-ACCOUNT-ID: '.$account_id.'',

                        '_method: DELETE',

                    ));

                    $tokens = curl_exec($ch);

 

Any help would be greatly appreciated.

Link to comment

1 answer to this question

Recommended Posts

  • 0

Hi,

I figured it out, here's the code for BUY position if anyone else struggles with the same problem, amend values relevant to your open positions:

                    $data = '{            
                        "dealId": "'.$deal_id.'",
                        "epic": null,
                        "expiry": null,
                        "direction": "SELL",
                        "size": "'.$size.'",
                        "level": null,
                        "orderType": "MARKET",
                        "timeInForce": "FILL_OR_KILL",
                        "quoteId": null
                    }';
   
                    $url_requests = 'https://demo-api.ig.com/gateway/deal/';
                    $connection_url = $url_requests.'///positions/otc';
                    $ch = curl_init($connection_url);
                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch,CURLOPT_HTTPHEADER, array(
                        '_method: DELETE',
                        'Content-Type: application/json; charset=UTF-8',
                        'Accept: application/json; charset=UTF-8',
                        'X-IG-API-KEY: '.$key.'',
                        'Version: 1',
                        "Authorization: Bearer $access_token",
                        'IG-ACCOUNT-ID: '.$account_id.'',
                    ));
   
                    $tokens = curl_exec($ch);
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...
us