Jump to content

Using Curl function


andrewdps

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/214105-using-curl-function/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/214105-using-curl-function/#findComment-1114161
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.