Php 使用curl登录并多次移动到另一个页面

Php 使用curl登录并多次移动到另一个页面,php,curl,Php,Curl,我正在尝试登录到一个网站并重定向到数组中声明的页面。 我成功登录,并获得阵列中的第一个url。 但问题是,当我在第二次循环代码时会出错 守则: $url = 'https://url/signin'; $ch = curl_init($url); $data = [ "e_mail" => "email@", "password" => "123456" ]; curl_setopt($ch, CURLOPT_FOLLOWLOCATION, tru

我正在尝试登录到一个网站并重定向到数组中声明的页面。 我成功登录,并获得阵列中的第一个url。 但问题是,当我在第二次循环代码时会出错

守则:

$url = 'https://url/signin';
  $ch = curl_init($url);

  $data = [
    "e_mail" => "email@",
    "password" => "123456"
  ];

  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // allow redirections
  curl_setopt($ch, CURLOPT_POST, true); // we are making post request
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // 
  curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); // COOKIEEjAR To save data for cookies created for login process
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // TRUE means dont just echo output the data instead we can store the request response in some variaable

  $result = curl_exec($ch);
  $urls_to_loop = array('url1', 'url2');

foreach ($urls_to_loop as $key => $url) {
          curl_setopt($ch, CURLOPT_URL, $url);
          $exec = curl_exec($ch);

          // echo($exec);
          curl_close($ch);// close login CURL resource, and free up system resources

          $html = new simple_html_dom();
          $html->load($exec);
  $links = [];
  foreach($html->find('link') as $element){
    if($element->href[-1] === '4'){
      // check if url is not in the array
      if(!in_array($element->href, $links)){
        array_push($links, $element->href);
      }
    } 
  }

}//END foreach
这就是错误:

警告:curl\u setopt:提供的资源不是第263行C:\xampp\htdocs\web\index.php中的有效curl句柄资源

警告:curl\u exec:提供的资源不是第264行C:\xampp\htdocs\web\index.php中的有效curl句柄资源

警告:curl\u close:提供的资源不是第267行C:\xampp\htdocs\web\index.php中的有效curl句柄资源


警告:curl_setopt:提供的资源不是C:\xampp\htdocs\web\index.php中第263行的有效curl句柄资源。您正在循环期间关闭处理程序

curl_close($ch);

将该行移到脚本的末尾。

由于curl\u close$ch;,资源变得无效;。在foreach之后调用所有请求后调用它