2010-12-23 89 views
1

我使用cUrl POST到网页(不是本地),然后返回HTML。PHP /卷曲 - 循环和POST缓冲区不清除

我需要多次这样做,所以cUrl代码在while循环中。这是奇怪的事情:它第一次按预期的方式工作,但似乎没有在每次后都清除POST缓冲区。 (我做close_curl($ ch),并且通过POST传递的所有数据都是正确的。)

例如,其中一个文本字段应该(并且是第一次)传递“ANY”。但第二次是通过“任何,任何”。

我是否正确,这个问题在于未清理的POST缓冲区?我该如何解决它?


SORRY:这里是代码的一个缩短版...

$someResults = mysql_query($someSQL); 

while($record = mysql_fetch_array($alertResults)){ 
    $url = "http://something.com/searchResults.asp"; 
    $someV = "Hi"; 

    $fields = array(
     //date to post. 
    );  

    foreach($fields as $key=>$value){ 
     $fields_string .= $key .'='. $value . '&'; 
    } 
    rtrim($fields_string,'&'); 
    $ch = curl_init(); 
    $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)'; 
    curl_setopt($ch,CURLOPT_USERAGENT, $userAgent); 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_POST,count($fields)); 
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); 
    curl_setopt($ch, CURLOPT_FAILONERROR, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_FORBID_REUSE, true); 
    ob_start(); 
    $html = curl_exec($ch); 
    curl_close($ch); 

    $dom = new DOMDocument(); 
    @$dom->loadHTML($html); 
    $xpath = new DOMXPath($dom); 
    $resultTable = $xpath->evaluate("/html/body//table"); 
} 

而且,我这样做的时候,第一时间通过循环$ resultTable中有60项。但每次之后(使用相同的url)它有0个项目。而且我非常肯定这是因为POST缓冲区没有被清除,并且事情会发布到以前的POST数据的顶部。

如何每次通过循环清除POST数据?

+4

你能发布环/卷曲的代码? – Oli 2010-12-23 10:29:34

回答

1

你似乎忘了复位$fields_string,那么试试这个

... 
curl_close($ch); 
unset($fields_string); 
...