【问题标题】:PHP/Curl - Loop & POST buffer not clearingPHP/Curl - 循环和 POST 缓冲区未清除
【发布时间】:2010-12-23 10:22:54
【问题描述】:

我正在使用 cUrl 发布到网页(非本地),然后返回 html。

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

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

我是否纠正了这个问题在于未清除的 POST 缓冲区?我该如何解决?


抱歉:这是代码的简化版本...

$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 个项目。但是每次之后(使用相同的网址)它都有0个项目。而且我很确定这是因为 POST 缓冲区没有被清除,并且事情被发布在以前的 POST 数据之上。

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

【问题讨论】:

  • 你能把循环/卷曲的代码贴出来吗?
  • 正如@Oli 所说,如果我们要评估问题所在,我们真的需要查看代码。 :-)
  • 已添加!非常感谢您的帮助。

标签: php html loops curl buffer


【解决方案1】:

你好像忘了重置$fields_string,所以试试这个

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

【讨论】:

    猜你喜欢
    • 2011-04-25
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2013-03-27
    • 1970-01-01
    • 2017-01-26
    相关资源
    最近更新 更多