Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP&;while循环中的cUrl-POST问题_Php_Loops_Curl_Libcurl - Fatal编程技术网

PHP&;while循环中的cUrl-POST问题

PHP&;while循环中的cUrl-POST问题,php,loops,curl,libcurl,Php,Loops,Curl,Libcurl,我有一个圈,里面有卷曲。(由于各种原因,我不能使用curl_multi。)问题是curl似乎在每次循环遍历之后保存了它发布的数据。例如,如果参数X是第一个循环通过的参数,如果是两个,则第二个循环通过的参数,则cUrl贴子为:“一,二”。它应该只显示“两个”。(尽管关闭并取消设置了卷曲控制柄。) 下面是代码的简化版本(去掉了不必要的信息): 您是否尝试过打印$fields\u字符串和$fields,以便在每次循环迭代中查看它们的值?也许其中一个还没有被清除。由于每次都使用curl\u init/c

我有一个圈,里面有卷曲。(由于各种原因,我不能使用curl_multi。)问题是curl似乎在每次循环遍历之后保存了它发布的数据。例如,如果参数X是第一个循环通过的参数,如果是两个,则第二个循环通过的参数,则cUrl贴子为:“一,二”。它应该只显示“两个”。(尽管关闭并取消设置了卷曲控制柄。)

下面是代码的简化版本(去掉了不必要的信息):


您是否尝试过打印$fields\u字符串和$fields,以便在每次循环迭代中查看它们的值?也许其中一个还没有被清除。由于每次都使用curl\u init/curl\u close,所以问题可能不在curl本身。

我希望看到创建$url、$fields和$fields\u string的代码。最好在使用变量之前显式声明它们,即使PHP不要求这样做。e、 g.Place
$fields=null位于适当范围的开头。循环没有自己的本地堆栈。如果您有一个类似for($i=0;$i<10;$i++){//do think in loop}echo$i;然后echo$i将打印9,即使它在循环之外。php不使用块作用域。如果你能回答编辑的问题,我将永远、不可挽回、无比感激。莎拉在上面已经讲了很多。这对我来说也真的很沮丧——来自C语言背景,在循环中没有一个定义良好的范围是令人恼火的。我刚刚学会了总是初始化循环中的局部变量。PHP确实具有良好作用域的一个地方是函数——这也许是您应该将curl代码放在的地方,在一个漂亮的“getSiteData($url,$fields)”函数中要干净得多,该函数会将$fields和$url复制到本地作用域中。
<?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.