Buddy Hall February 2016

PHP cURL command inside foreach loop only shows last result

I'm new to programming, and I've 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 this 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.

Answers


Buddy Hall February 2016

It turns out that storing the string into the array with the explode function included extra hidden return characters, so I added $ob2 = trim($ob2); into the foreach loop and it now works.

Post Status

Asked in February 2016
Viewed 2,941 times
Voted 6
Answered 1 times

Search




Leave an answer