我正在尝试使用PHP和cURL下载文件。我通过一系列cURL请求获取了下载文件的URL。使用PHP下载文件问题
当我尝试下载文件时,我得到一个空文件。请参阅相关的代码如下:
...
$output = curl_exec($ch);
curl_close($ch);
$regex = '/\b(https?|ftp|file):\/\/resources\.lendingclub\.com\/secure[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';
preg_match_all($regex, $output, $parts);
$url3a = $parts[0][0];
OutputMsg($url3a); //print the content of $url3a
DownloadFile($url3a, 'testfile.zip'); //downloads the file using a cURL request
...
这将创建零个字节文件testfile.zip。并输出以下屏幕:
https://resources.lendingclub.com/secure/LoanStats3a_securev1.csv.zip?signature=cmu73mJsyNhznZMBH6B%2FsFjoNuE%3D&issued=1459663950631
如果我添加一个线(见下文)
...
$output = curl_exec($ch);
curl_close($ch);
$regex = '/\b(https?|ftp|file):\/\/resources\.lendingclub\.com\/secure[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';
preg_match_all($regex, $output, $parts);
$url3a = $parts[0][0];
//line added below
$url3a = 'https://resources.lendingclub.com/secure/LoanStats3a_securev1.csv.zip?signature=cmu73mJsyNhznZMBH6B%2FsFjoNuE%3D&issued=1459663950631';
OutputMsg($url3a); //print the content of $url3a
DownloadFile($url3a, 'testfile.zip'); //downloads the file using a cURL request
...
文件被正确下载,屏幕输出和以前一样:
https://resources.lendingclub.com/secure/LoanStats3a_securev1.csv.zip?signature=cmu73mJsyNhznZMBH6B%2FsFjoNuE%3D&issued=1459663950631
我不知道为什么上面的第一个例子不起作用,第二个例子。我做的唯一的事情就是输入网址并将其分配给变量。
我可以提供更多的代码,包括DownloadFile函数。
我还没有想出一种方法来使它工作,而无需在源代码中输入url。
谢谢。