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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.