phpCAS version 1.3.6

CurlMultiRequest.php
Go to the documentation of this file.
1 <?php
2
42 class CAS_Request_CurlMultiRequest
43 implements CAS_Request_MultiRequestInterface
44 {
45 private $_requests = array();
46 private $_sent = false;
47
48 /*********************************************************
49 * Add Requests
50 *********************************************************/
51
64 public function addRequest (CAS_Request_RequestInterface $request)
65 {
66 if ($this->_sent) {
67 throw new CAS_OutOfSequenceException(
68 'Request has already been sent cannot '.__METHOD__
69 );
70 }
71 if (!$request instanceof CAS_Request_CurlRequest) {
72 throw new CAS_InvalidArgumentException(
73 'As a CAS_Request_CurlMultiRequest, I can only work with CAS_Request_CurlRequest objects.'
74 );
75 }
76
77 $this->_requests[] = $request;
78 }
79
85 public function getNumRequests()
86 {
87 if ($this->_sent) {
88 throw new CAS_OutOfSequenceException(
89 'Request has already been sent cannot '.__METHOD__
90 );
91 }
92 return count($this->_requests);
93 }
94
95 /*********************************************************
96 * 2. Send the Request
97 *********************************************************/
98
106 public function send ()
107 {
108 if ($this->_sent) {
109 throw new CAS_OutOfSequenceException(
110 'Request has already been sent cannot send again.'
111 );
112 }
113 if (!count($this->_requests)) {
114 throw new CAS_OutOfSequenceException(
115 'At least one request must be added via addRequest() before the multi-request can be sent.'
116 );
117 }
118
119 $this->_sent = true;
120
121 // Initialize our handles and configure all requests.
122 $handles = array();
123 $multiHandle = curl_multi_init();
124 foreach ($this->_requests as $i => $request) {
125 $handle = $request->initAndConfigure();
126 curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
127 $handles[$i] = $handle;
128 curl_multi_add_handle($multiHandle, $handle);
129 }
130
131 // Execute the requests in parallel.
132 do {
133 curl_multi_exec($multiHandle, $running);
134 } while ($running > 0);
135
136 // Populate all of the responses or errors back into the request objects.
137 foreach ($this->_requests as $i => $request) {
138 $buf = curl_multi_getcontent($handles[$i]);
139 $request->_storeResponseBody($buf);
140 curl_multi_remove_handle($multiHandle, $handles[$i]);
141 curl_close($handles[$i]);
142 }
143
144 curl_multi_close($multiHandle);
145 }
146 }
CAS_Request_CurlMultiRequest\addRequest
addRequest(CAS_Request_RequestInterface $request)
Definition: CurlMultiRequest.php:64
CAS_Request_CurlMultiRequest\getNumRequests
getNumRequests()
Definition: CurlMultiRequest.php:85
CAS_Request_MultiRequestInterface
Definition: MultiRequestInterface.php:42
CAS_Request_RequestInterface
Definition: RequestInterface.php:40
CAS_Request_CurlRequest
Definition: CurlRequest.php:40
CAS_Request_CurlMultiRequest\$_sent
$_sent
Definition: CurlMultiRequest.php:46
CAS_OutOfSequenceException
Definition: OutOfSequenceException.php:44
CAS_InvalidArgumentException
Definition: InvalidArgumentException.php:40
CAS_Request_CurlMultiRequest\$_requests
$_requests
Definition: CurlMultiRequest.php:45
CAS_Request_CurlMultiRequest\send
send()
Definition: CurlMultiRequest.php:106
CAS_Request_CurlMultiRequest
Definition: CurlMultiRequest.php:42