Process multiple cURL with PHP

Jump to solution
buddyhall
Community Champion

Hi,

I'm new to programming, but made a page using PHP that lets us enter userIDs and add observers using the Canvas API and that worked great. I'm now trying to enter a list of people to be observed and have it process multiple URLs and that is where I'm running into problems. It only seems to process the final userID in the box. Any ideas what I'm doing wrong? This doesn't need to be run often and I'm not worried about performance so I don't really need the cURL commands to process simultaneously.

$observeeA = explode ("\n", $observee);

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

foreach ($observeeA as $ob2){

   if ($idtype == "Canvas") {

     $url = $domain . "/api/v1/users/$observer/observees/" . $ob2; }

   else {

     $url = $domain . "/api/v1/users/sis_user_id:$observer/observees/sis_user_id:" . $ob2; }

   curl_setopt($ch, CURLOPT_URL, $url);

   $resp = curl_exec($ch);

   echo $ob2 . "<br>" . $url . "<br>" . $resp . "<br>";

   }

curl_close($ch);

The echo on line 13 outputs all the correct entries for the URL, but it never gets a response until the last one in the list.

Labels (1)
1 Solution
buddyhall
Community Champion

The variables in my array had an extra hidden return character in them. I inserted this line between 6 and 7 above and it works flawlessly now. I figured I would add this incase anyone else is trying to use arrays with multiple cURL calls in PHP.

$ob2 = trim($ob2);

View solution in original post