API Trading with CURL and IG

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #170244 quote
    imencity
    Participant
    Senior

    Hi,

    I want to retrieve DATA ig via their API. It works very well via a script on my dedicated, but if I run it via a CRON task I cannot fill my cookies with the X_SECURITY_TOKEN and the CST. The verbose gives me a request.txt file which contains all the information. So there is indeed an exchange between my server and that of ig, everything works except the filling of the cookie.

    Here is my very light script:

    $lien = 'https://demo-api.ig.com/gateway/deal/session';
      $path_cookie = __DIR__.'/cookiesIGtest.txt';
      if (!file_exists($path_cookie )) touch($path_cookie);
    
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $lien);
        curl_setopt($curl, CURLOPT_COOKIESESSION, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 200);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_USERAGENT, 'userAgentMozilla');
        curl_setopt($curl, CURLOPT_POSTFIELDS,'{
            "identifier": "myid",
            "password": "mypass"
            } ');
       //     $path_cookie = fopen("cookiesIGtest.txt", 'w');
        curl_setopt($curl, CURLOPT_COOKIEJAR , $path_cookie);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Accept: application/json',
            'VERSION: 2',
            'X-IG-API-KEY: 3.........4'
          ));
        curl_setopt($curl, CURLOPT_VERBOSE, true);
        $verbose = fopen('request.txt', 'w');
        curl_setopt($curl, CURLOPT_STDERR, $verbose);
    if(!curl_exec($curl)){
        die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
    }
    else{
        $response = curl_exec($curl);
    }
    curl_close($curl);
    $result = json_decode($response, true);
    
    echo '<pre>';
    var_dump($result);
    echo'</pre>';

    The file that is created is this one:

    # Netscape HTTP Cookie File
    # https://curl.se/docs/http-cookies.html
    # This file was generated by libcurl! Edit at your own risk.

    so it’s not a writing problem. The script below works fine:

    $lien = 'https://www.google.com/';
    $path_cookie = __DIR__.'/test_cookies.txt';
    if (!file_exists($path_cookie)) touch($path_cookie);
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $lien);
    curl_setopt($curl, CURLOPT_COOKIESESSION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $path_cookie);
    curl_setopt($curl, CURLOPT_USERAGENT, 'userAgentMozilla');
    
    $return = curl_exec($curl);
    curl_close($curl);

    If I comment out CURLOPT_COOKIEJAR the file is created and is empty. If I leave the line active then the file is created with the headers listed above, but without any other info …

    Any idea please?

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.

API Trading with CURL and IG


General Trading: Market Analysis & Manual Trading

New Reply
Author
author-avatar
imencity @imencity Participant
Summary

This topic contains 1 voice and has 0 replies.

Topic Details
Forum: General Trading: Market Analysis & Manual Trading
Language: English
Started: 05/24/2021
Status: Active
Attachments: No files
Logo Logo
Loading...