卷曲与 for 循环错误


Curl with for loop error

脚本在没有实现 for 循环的情况下运行,但是当我放置一个 for 循环时它失败了。

工作脚本:

$dob=new DateTime('28-12-83');
$regno='1984';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://domain.com/login.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'op=&sub=3&txtregno='.$regno.'&txtrollno=&txtpass=&txtdob='. $dob->format('d-m-y'));
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
curl_setopt($ch, CURLOPT_URL, 'http://domain.com/results.php');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$fp = fopen('mm.htm', 'w');
curl_setopt ($ch, CURLOPT_FILE, $fp);
$store = curl_exec ($ch);
fclose($fp);
curl_close ($ch);

上面的脚本运行良好,并生成一个带有输出的 mm.htm 文件。

for 循环后的脚本不正确

$dob=new DateTime('27-12-83');
$regno='1984';
for($i=1; $i<=3; $i++)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'domain.com/login.php');
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, 'op=&sub=3&txtregno='.$regno.'&txtrollno=&txtpass=&txtdob='. $dob->format('d-m-y'));
    curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $store = curl_exec ($ch);
    curl_setopt($ch, CURLOPT_URL, 'domain.com/results.php');
    curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    $fp = fopen($regno."_".$i.'.htm', 'w');
    curl_setopt ($ch, CURLOPT_FILE, $fp);
    $store = curl_exec ($ch);
    fclose($fp);
    $dob->modify('+1 day');
    set_time_limit(0);
    curl_close ($ch);
}

上面的脚本生成 3 个 htm 文件,但没有任何输出。这意味着此脚本在某处失败,但我未能跟踪问题。

如果你想将收到的内容存储在你用CURLOPT_FILE指定的文件中,你肯定不应该也使用CURLOPT_RETURNTRANSFER来要求它被返回吗?或者也许我应该问你为什么这样做?