1

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?

3
  • 4
    could you post the code of the loop/curl? Commented Dec 23, 2010 at 10:29
  • As @Oli says, we really need to see the code if we're going to assess what the problem is. :-) Commented Dec 23, 2010 at 10:31
  • Added it! Help is very much appreciated. Commented Dec 23, 2010 at 22:05

1 Answer 1

1

You seems forget to reset $fields_string, so try this

...
curl_close($ch);
unset($fields_string);
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.