使用php通过带有Cookie的CURL下载文件 [英] Download a file via CURL with Cookies using php
本文介绍了使用php通过带有Cookie的CURL下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要发布cookies.txt,然后使用CURL将页面下载到文本文件中。这是我的FGC示例,但显然FGC不适合使用cookie,因此我需要CURL。
I need to POST cookies.txt then download a page to a text file using CURL. This is my FGC example but apparently FGC is bad at cookies so I need CURL.
<?php
$file = file('source\1.txt');
foreach ($file as $link)
{
$link = trim($link);
$link2 = "http://site-that.com/authenticates?={$link}";
$downloaded = file_get_contents($link2);
$myFile = "parsed/$link" . ".txt";
$fh = fopen($myFile, 'a') or die('Cannot open file');
fwrite($fh, $downloaded);
}
$timestamp = time();
rename('source\1.txt', "source/done/done-{$timestamp}.txt");
echo 'Finished';
有什么想法吗?代码示例将不胜感激。一个简单的示例,例如google.com,将是个不错的选择。另外,如果您还有另一种更快的方法,请发布!
Any ideas? Code examples would be extremely appreciated. A simple example that does this to like google.com would be great Also, if you have another way to do this that's faster, please post!
推荐答案
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFileName); //$cookieFilename must be a path to a file with cookie data
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFileName);
//curl_setopt($curl, CURLOPT_COOKIE, $cookie); //you may also use this, here $cookie is a cookie string
curl_close($curl);
$data = curl_exec($curl);
这篇关于使用php通过带有Cookie的CURL下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文