/** * 並列リクエストを実行します。 * * @param CurlMultiHandler $cmh * @return array[CurlResponse] */ public static function multieFecth(CurlMultiHandler $cmh) { $mh = $cmh->getHandle(); $active = null; do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active and $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } $crs = array(); foreach ($cmh->getHandlers() as $h) { $ch = $h->getHandle(); $response = curl_multi_getcontent($ch); if ($response === false) { throw new CurlFetchException(curl_errno($ch), curl_error($ch)); } array_push($crs, new CurlResponse($response, $ch)); $cmh->remove($h); $h->close(); } $cmh = null; return $crs; }
/** * * @test * @covers syamgot\Curl\Curl::multieFecth * @covers syamgot\Curl\CurlMultiHandler::__construct * @covers syamgot\Curl\CurlMultiHandler::add */ public function multieFecth() { // $mch = new CurlMultiHandler(); $mch->add(new CurlHandler('http://localhost:8000/')); $mch->add(new CurlHandler('http://localhost:8000/')); $cReses = Curl::multieFecth($mch); $this->assertEquals(count($cReses), 2); foreach ($cReses as $cRes) { $this->assertEquals($cRes->getHttpCode(), 200); $this->assertEquals($cRes->getBody(), 'hello'); } }