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 th开发者_如何转开发e 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?
You seems forget to reset $fields_string
, so try this
...
curl_close($ch);
unset($fields_string);
...
精彩评论