0

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?

Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
Joshua Terrill
  • 1,995
  • 5
  • 21
  • 40

1 Answers1

1

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.

Marek
  • 7,337
  • 1
  • 22
  • 33
  • Okay, then how would I get each line from the textarea and feed it through my loop? I was looking at this post for guidance and that's where I get my previous code from: http://stackoverflow.com/questions/3702400/get-each-line-from-textarea – Joshua Terrill Oct 06 '14 at 08:46
  • Just remove the line, that's it. It's messing up your input. – Marek Oct 06 '14 at 08:49