ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
CurlMultiRequest.php
Go to the documentation of this file.
1 <?php
2 
43 {
44  private $_requests = array();
45  private $_sent = false;
46 
47  /*********************************************************
48  * Add Requests
49  *********************************************************/
50 
64  {
65  if ($this->_sent) {
67  'Request has already been sent cannot ' . __METHOD__
68  );
69  }
70  if (!$request instanceof CAS_Request_CurlRequest) {
72  'As a CAS_Request_CurlMultiRequest, I can only work with CAS_Request_CurlRequest objects.'
73  );
74  }
75 
76  $this->_requests[] = $request;
77  }
78 
84  public function getNumRequests()
85  {
86  if ($this->_sent) {
88  'Request has already been sent cannot ' . __METHOD__
89  );
90  }
91  return count($this->_requests);
92  }
93 
94  /*********************************************************
95  * 2. Send the Request
96  *********************************************************/
97 
105  public function send()
106  {
107  if ($this->_sent) {
108  throw new CAS_OutOfSequenceException(
109  'Request has already been sent cannot send again.'
110  );
111  }
112  if (!count($this->_requests)) {
113  throw new CAS_OutOfSequenceException(
114  'At least one request must be added via addRequest() before the multi-request can be sent.'
115  );
116  }
117 
118  $this->_sent = true;
119 
120  // Initialize our handles and configure all requests.
121  $handles = array();
122  $multiHandle = curl_multi_init();
123  foreach ($this->_requests as $i => $request) {
124  $handle = $request->initAndConfigure();
125  curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
126  $handles[$i] = $handle;
127  curl_multi_add_handle($multiHandle, $handle);
128  }
129 
130  // Execute the requests in parallel.
131  do {
132  curl_multi_exec($multiHandle, $running);
133  } while ($running > 0);
134 
135  // Populate all of the responses or errors back into the request objects.
136  foreach ($this->_requests as $i => $request) {
137  $buf = curl_multi_getcontent($handles[$i]);
138  $request->_storeResponseBody($buf);
139  curl_multi_remove_handle($multiHandle, $handles[$i]);
140  curl_close($handles[$i]);
141  }
142 
143  curl_multi_close($multiHandle);
144  }
145 }
addRequest(CAS_Request_RequestInterface $request)
Add a new Request to this batch.
getNumRequests()
Retrieve the number of requests added to this batch.
This interface defines a class library for performing multiple web requests in batches.
foreach($paths as $path) $request
Definition: asyncclient.php:32
This interface defines a class library for performing web requests.
Provides support for performing web-requests via curl.
Definition: CurlRequest.php:40
This class defines Exceptions that should be thrown when the sequence of operations is invalid...
Exception that denotes invalid arguments were passed.
$i
Definition: disco.tpl.php:19
send()
Perform the request.
This interface defines a class library for performing multiple web requests in batches.