php - 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 文件被创建并且是空的。如果我保持该行处于活动状态,则使用上面列出的标题创建文件,但没有任何其他信息...
请问有什么想法吗?
解决方案
推荐阅读
- c++ - 为什么每次“for”迭代输出 4 次
- typescript - 打字稿,声明 2 个道具必须具有相同的值
- python - 如何从熊猫数据框中的字符串中提取最后一个日期
- java - 如何在插入之前检查行是否存在?
- reactjs - Epic 不是函数
- php - 警告 ftp_put() 文件名不能为空
- google-apps-script - 从独立的 Google Apps 脚本发布插件
- amazon-web-services - 如何在注册期间以“事务”方式将用户添加到自己的数据库?
- javascript - 当我重新加载页面值时不会改变,它们只有在我登录时才会改变
- bootstrap-4 - 如何将列表 Grup 项目放在“创建按钮”下方