andrewdps Posted September 22, 2010 Share Posted September 22, 2010 Consider this function, public function getData($data) { $availability = array(); foreach ($data as $id) { $availability[] = $this->getStatus($id); } return $availability; } If my guess is not wrong this function sends a request to get the status(calling getStatus()) everytime it encounters the data.Like,it calls for 10 requests and get the 10 statuses individually.Is there anyway I can send all the 10 requests at a time and get the10 responses back at once using curl function. Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
petroz Posted September 22, 2010 Share Posted September 22, 2010 Is there anyway I can send all the 10 requests at a time and get the10 responses back at once using curl function. Any help would be greatly appreciated. Can you rephrase your question? Quote Link to comment Share on other sites More sharing options...
andrewdps Posted September 22, 2010 Author Share Posted September 22, 2010 Sure, I've two functions public function getStatus($id) { } public function getData($data) { $availability = array(); foreach ($data as $id) { $availability[] = $this->getStatus($id); } return $availability; } All the requests in the above "for" loop code will be synchronous: a new one is fired only after the previous has completed.Now,I wish to rewrite the code using curl function instead of "for" loop,so that all the requests are made simultaneously and finally get back all the status(getstatus) in a single shot. I'm trying to use this tutorial to make this happen.Here is the code I've developed so far public function getStatuses($idList, $options = array()){ $mh = curl_multi_init(); $curly = array(); $results = array(); foreach($idList as $id=>$d){ $curly[$id] = curl_init(); $url = (is-array($d) && !empty($d['url'])) ? $d['url'] : $d; curl_setopt($curly[$id], CURLOPT_URL,$url); curl_setopt($curly[$id], CURLOPT_HEADER,0); curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER,1); //post? if (is_array($d)) { if (!empty($d['post'])) { curl_setopt($curly[$id], CURLOPT_POST,1); curl_setopt($curly[$id], CURLOPT_POSTFIELDS,$d['post']); } } if (!empty($options)) { curl_setopt_array($curly[$id], $options); } curl_multi_add_handle($mh, $curly[$id]); } $running = null; do{ curl_multi_exec($mh,$running); }while($running > 0); foreach ($curly as $id){ //create the array that vufind/smarty expects for drawing status info $ilsResponse = curl_multi_getcontent($curly[$id]) $results[$id] = $this->transformIlsResponse($ilsResponse); curl_multi_remove_handle($mh, $curly[$id]); curl_close($curly[$id]); } curl_multi_close($mh); return $results; } Let me know if something is wrong with the code. Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.