首页 > 解决方案 > curl 未填充的 cookie(IG 交易)

问题描述

我想通过他们的 API 检索 DATA ig。它通过我专用的脚本运行良好,但如果我通过 CRON 任务运行它,我无法用 X_SECURITY_TOKEN 和 CST 填充我的 cookie。详细信息给了我一个 request.txt 文件,其中包含所有信息。所以我的服务器和 ig 的服务器之间确实存在交换,除了填充 cookie 之外,一切正常。

这是我非常简单的脚本:

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

创建的文件是这个:

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

所以这不是写作问题。下面的脚本工作正常:

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

如果我注释掉 CURLOPT_COOKIEJAR 文件被创建并且是空的。如果我保持该行处于活动状态,则使用上面列出的标题创建文件,但没有任何其他信息...

请问有什么想法吗?

标签: phpcookiestrading

解决方案


推荐阅读