适应CURL下载CMD到CURL下载PHP

时间:2016-04-28 11:55:07

标签: php curl cmd

我有一个CURL CMD脚本来下载文件:

curl "http://xxxxxxxxxxxx.yy/fileToDownload.txt" ^
  -H "Host: xxxxxxxxxxxx.yy" ^
  -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0" ^
  -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" ^
  -H "Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3" ^
  -H "Accept-Encoding: gzip, deflate" ^
  -H "Connection: keep-alive" ^
  -H "Content-type: application/x-www-form-urlencoded" ^
  --data "version=5%%2E0%%2E1" ^
  --cookie cookies.txt ^
  --compressed ^
  --output file.txt

此脚本在批处理文件中完美运行 现在我尝试用PHP制作这个脚本。我尝试了几种方法却没有成功。

这是我上一个PHP脚本:

$source = "http://xxxxxxxxxxxx.yy/fileToDownload.txt";
$cookie = "cookie.txt";

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, array(
    'Host: xxxxxxxxxxxx.yy',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3',
    'Accept-Encoding: gzip, deflate',
    'Connection: keep-alive',
));
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_ENCODING , "");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'version=5%%2E0%%2E1');
$data = curl_exec ($ch);

$destination = "file.txt";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);

curl_close($ch);

当我执行此脚本时,我会下载该文件,但它始终为空。但是,使用CMD脚本可以很好地工作。

如果有人可以帮助我:)

0 个答案:

没有答案