Aller au contenu

Aide pour API REST


cybermad

Sujets conseillés

Bonsoir à tous,

Je désire récupérer de la DATA ig via leur API.
Ca marche très bien via un script sur mon dédié, mais si je le lance via une tâche CRON je n'arrive pas à remplir mon cookies avec la X_SECURITY_TOKEN et la CST. Le verbose me sort un fichier request.txt qui contient bien toutes les infos.
Il y a donc bien un échange entre mon serveur et celui de ig, tout fonctionne sauf le remplissage du cookie.

Voici mon script très allégé:

 $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>';

 

Le fichier qui se crée est celui-là:

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

 

donc ce n'est pas un problème d'écriture.
Le script ci-dessous fonctionne bien:

$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);

 

Si je commente CURLOPT_COOKIEJAR le fichier est créé et est vide. Si je laisse la ligne active alors le fichier est créé avec les en-têtes listées plus haut, mais sans aucune autre info...

 

Et pour compliquer le tout si je fais un:

    curl_setopt($curl, CURLOPT_VERBOSE, true);
    $verbose = fopen('request.txt', 'w');
    curl_setopt($curl, CURLOPT_STDERR, $verbose);

le fichier est bien rempli avec tous les éléments... alors oui je pourrais me contenter du verbose... mais CURLOPT_COOKIEJAR devrait fonctionner !


Une idée svp ?

Lien vers le commentaire
Partager sur d’autres sites

Veuillez vous connecter pour commenter

Vous pourrez laisser un commentaire après vous êtes connecté.



Connectez-vous maintenant
×
×
  • Créer...