PHP 与cUrl - while 循环中的 POST 问题

发布于 2024-10-08 17:47:05 字数 1572 浏览 6 评论 0原文

我有一个 while 循环,里面有 cUrl 。 (由于各种原因,我无法使用curl_multi。)问题是cUrl似乎在每次循环遍历后保存它发布的数据。例如,如果参数 X 在第一次循环中为 One,如果在第二次循环中为 Two,则 cUrl 会发布:“One,Two”。它应该只是发布“两个”。(尽管关闭并取消设置卷曲句柄。)

这是代码的简化版本(删除了不必要的信息):

<?php   
  while(true){

           // code to form the $url. this code is local to the loop. 
          // so the variables should be "erased" and made new for each 
         // loop through.

    $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);
    $html = curl_exec($ch);
    curl_close($ch);
    unset($ch);

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

           // $resultTable is 20 the first time through the loop,
           // and 0 everytime thereafter because the POSTing doesn't work right 
           //with the "saved" parameters. 

我在这里做错了什么?


编辑

迈克尔是对的。我需要取消设置 $fields/$fields_string。新数据被连接到旧数据上。

但我不明白为什么会发生这种情况。 $fields 是循环中的局部变量。 循环结束后是否应该将其从堆栈中删除,然后再创建一个新的?

I have a while loop, with cUrl inside. (I can't use curl_multi for various reasons.) The problem is that cUrl seems to save the data it POSTS after each loop traversal. For instance, if parameter X is One the first loop through, and if it's Two the second loop through, cUrl posts: "One,Two". It should just POST "Two".(This is despite closing and unsetting the curl handle.)

Here's a simplified version of the code (with unecessary info stripped out):

<?php   
  while(true){

           // code to form the $url. this code is local to the loop. 
          // so the variables should be "erased" and made new for each 
         // loop through.

    $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);
    $html = curl_exec($ch);
    curl_close($ch);
    unset($ch);

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

           // $resultTable is 20 the first time through the loop,
           // and 0 everytime thereafter because the POSTing doesn't work right 
           //with the "saved" parameters. 

What am I doing wrong here?


EDIT

Michael was right. I needed to unset $fields/$fields_string. The new data was concatenated onto the old data.

I don't understand why this happens, though. $fields is a local variable in the loop. Shouldn't it be removed from the stack once the loop ends, and then it'd be created a new?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2024-10-15 17:47:05

您是否尝试过打印 $fields_string 和 $fields 以在每次循环迭代时查看它们的值?也许其中之一还没有被清除。由于您每次都使用curl_init/curl_close,因此问题可能不在于curl 本身。

Have you tried printing the $fields_string and $fields to see their values at each loop iteration? Perhaps one of them isn't getting cleared. Since you're using curl_init/curl_close each time, the problem is likely not in curl itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文