我正在尝试自动化工作流程并下载远程 xml 文件 (google-shopping-feed.xml)
我的函数如下所示
function saveRemoteFile($url, $filename) {
global $GlobalFileHandle;
$GlobalFileHandle = null;
set_time_limit(0);
# Open the file for writing...
$GlobalFileHandle = fopen($filename, 'w+');
$path_cookie = BASE_PATH . '/mycookie/cookie.txt';
if (!file_exists(realpath($path_cookie))) touch($path_cookie);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $GlobalFileHandle);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'cookie: __cfduid=d0d14dfd36e0da8e7858b58873b4263181523751395'
));
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath($path_cookie));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); # optional
curl_setopt($ch, CURLOPT_TIMEOUT, -1); # optional: -1 = unlimited, 3600 = 1 hour
curl_setopt($ch, CURLOPT_VERBOSE, false); # Set to true to see all the innards
# Only if you need to bypass SSL certificate validation
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
# Exceute the download - note we DO NOT put the result into a variable!
$result = curl_exec($ch);
fwrite($GlobalFileHandle, $result);
# Close CURL
curl_close($ch);
# Close the file pointer
fclose($GlobalFileHandle);
}
但我从远程得到关注
请稍等...启用 JavaScript 和 cookies 以继续还有其他方法获取文件内容吗?我可以直接在浏览器中获取它,但我想保存手动工作流程来下载然后上传文件...
看起来您尝试下载的 URL 不是简单的文件,要生成它需要 JavaScript 引擎的内容,因此请尝试使用类似 https://github.com/chrome-php/chrome 而不是curl