简体   繁体   中英

Multiple curl posts using curl_multi

I'm trying to use curl to perform multiple curl posts to a URL. I have a page with a field for URL and a textarea box where I would put multiple emails (on separate lines) to be posted to the url.

Here's my code.

<?php
    $url = $_POST['url'];    
    $text = trim($_POST['emails']);
    $text = nl2br($text); 
    $text = explode("\n", $text);    
    foreach($text as $i => $text) {
      $fields = array(
        'u' => urlencode('0000'),
        'id' => urlencode('0000'),
        'FIELD0' => urlencode($text),
        'FIELD1' => urlencode('First'),
        'FIELD2' => urlencode('Last')
       );      
      $fields_string = "";
      foreach($fields as $key=>$value) { 
            $fields_string .= $key.'='.$value.'&';
      }
      rtrim($fields_string, '&');      
      $mh = curl_multi_init();
      $ch[$i] = curl_init();
      curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
      curl_multi_add_handle($mh, $ch[$i]);      
      curl_setopt($ch[$i],CURLOPT_URL, $url);
      curl_setopt($ch[$i],CURLOPT_POST, count($fields));
      curl_setopt($ch[$i],CURLOPT_POSTFIELDS, $fields_string);      
      $result = curl_exec($ch[$i]);
      curl_close($ch[$i]);      
   }    
?>

As it stands now, if I put one email into my field, it works. But when I put multiple emails into the field, it only posts the last one. Can someone help out?

It's because of this: $text = nl2br($text);

It creates invalid email addresses, which end with <br /> . Only the last one is valid, as you don't input new line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM