PHP/Curl - Loop & POST buffer not clearing

偶尔善良 提交于 2019-12-11 12:42:34

问题


I'm using cUrl to POST to a web page (not local) and then return the html.

I need to do this multiple times, so the cUrl code is in a while loop. Here's the weird thing: it works as expected the first time, but doesn't seem to clear the POST buffer everytime there after. (I do close_curl($ch). And all the data passed through POST is correct.)

For instance, one of the text fields should be (and, the first time, is) pass "ANY". But the second time it is pass "ANY, ANY".

Am I correct that this problems lies in an uncleared POST buffer? How can I fix it?


SORRY: Here's a shortened version of the code...

$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"); 
}

And, when I do this, the first time through the loop $resultTable has 60 items in it. But everytime there after (using the same url) it has 0 items. And I'm pretty sure it's because the POST buffer isn't cleared and things get posted ontop of previous POST data.

How can I clear the POST data each time through the loop?


回答1:


You seems forget to reset $fields_string, so try this

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


来源:https://stackoverflow.com/questions/4517842/php-curl-loop-post-buffer-not-clearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!