/** * Test asynchronous request through socketPerform() and socketSelect() * * @return void */ public function testRequestAsynchronous() { $test = $this; $req = new cURL\Request(); $req->getOptions()->set(CURLOPT_URL, $this->okTestUrl)->set(CURLOPT_RETURNTRANSFER, true); $req->addListener('complete', function ($event) use($test) { $test->validateSuccesfulResponse('/', $event->response); }); $n = 0; while ($req->socketPerform()) { $n++; $req->socketSelect(); } try { $req->socketPerform(); } catch (cURL\Exception $e) { } $this->assertInstanceOf('cURL\\Exception', $e); $this->assertGreaterThan(0, $n); $req = new cURL\Request(); $req->getOptions()->set(CURLOPT_URL, $this->errorTestUrl)->set(CURLOPT_TIMEOUT, 1)->set(CURLOPT_RETURNTRANSFER, true); $req->addListener('complete', function ($event) use($test) { $test->validateTimeoutedResponse($event->response); }); while ($req->socketPerform()) { $req->socketSelect(); } }
public function updateBanner() { $url = (new Owl())->getUrl('', 'banners'); $request = new Request($url); $request->getOptions()->set(CURLOPT_TIMEOUT, 8)->set(CURLOPT_RETURNTRANSFER, true)->set(CURLOPT_USERAGENT, $this->di->get('config')->curlUserAgent); $response = $request->send(); $httpCode = $response->getInfo(CURLINFO_HTTP_CODE); if ($response->hasError() || $httpCode != 200) { $error = $response->getError()->getMessage(); throw new \Exception("Ошибка!!! http code: {$httpCode} message: {$error} url: {$url}"); } $content = json_decode($response->getContent(), true); parent::createBanner($content); }
public function request($url, $useOwlServer = true) { $url = urldecode($url); PH_DEBUG ? Debugger::dumpBar($url, 'url') : null; if ($useOwlServer) { $url = $this->getUrl($url); $request = new Request($url); $request->getOptions()->set(CURLOPT_TIMEOUT, 8)->set(CURLOPT_RETURNTRANSFER, true)->set(CURLOPT_USERAGENT, $this->di->get('config')->curlUserAgent); $response = $request->send(); $content = json_decode($response->getContent(), true); $url = $this->getUrl('', 'common'); $request = new Request($url); $request->getOptions()->set(CURLOPT_TIMEOUT, 8)->set(CURLOPT_RETURNTRANSFER, true)->set(CURLOPT_USERAGENT, $this->di->get('config')->curlUserAgent); $response = $request->send(); $common = json_decode($response->getContent(), true); return $content + $common; } $response = Contents::get($url); return $response; }
/** * Tests whether 'complete' event on individual Request has not been fired * when Request::send() was used. */ public function testRequestCompleteEventSynchronous() { $eventFired = 0; $req = new cURL\Request(); $req->getOptions()->set(CURLOPT_URL, $this->createRequestUrl())->set(CURLOPT_RETURNTRANSFER, true); $req->addListener('complete', function (cURL\Event $event) use(&$eventFired) { $eventFired++; }); $req->send(); $this->assertEquals(0, $eventFired); }
/** * Execute the request. * @throws CurlException * @throws \Quazardous\PriceministerWs\RuntimeException * @throws ApiException * @return \Quazardous\PriceministerWs\Response\BasicResponse */ public function execute() { $url = $this->getOption('url') . '?' . http_build_query($this->getParameters()); $curl = new CurlRequest($url); $curl->getOptions()->set(CURLOPT_TIMEOUT, $this->getOption('timeout'))->set(CURLOPT_RETURNTRANSFER, true)->set(CURLOPT_HEADER, true); $curlResponse = $curl->send(); if ($curlResponse->hasError()) { $error = $curlResponse->getError(); throw new CurlException($error ? $error->getMessage() : 'Unkown exception', $error ? $error->getCode() : null); } $code = $curlResponse->getInfo(CURLINFO_HTTP_CODE); $content = $curlResponse->getContent(); $header_size = $curlResponse->getInfo(CURLINFO_HEADER_SIZE); $header = substr($content, 0, $header_size); $body = substr($content, $header_size); $basic = new BasicResponse($header, $body); $start = substr($body, 0, 256); $matches = null; if (preg_match('@<\\?xml[^>]+encoding="[^\\s"]+[^?]*\\?>\\s*<errorresponse@si', $start, $matches)) { $xml = simplexml_load_string($body); if ($xml === false) { throw new RuntimeException('Response content is no valid XML', RuntimeException::NO_VALID_XML); } $details = array(); if ($xml->error->details->detail) { foreach ($xml->error->details->detail as $detail) { $details[] = (string) $detail; } } throw new ApiException($xml->error->message, $xml->error->type, $xml->error->code, $details, $basic); } if ($code != 200) { $e = new RuntimeException('HTTP code is not 200 (' . $code . ')', RuntimeException::HTTP_CODE_NOT_200); $e->setResponse($basic); throw $e; } return $basic; }
/** * Create the raw CURL request. * @return CurlRequest */ protected function getCurlRequest($url) { $curl = new CurlRequest($url); $curl->getOptions()->set(CURLOPT_TIMEOUT, $this->getOption('timeout'))->set(CURLOPT_RETURNTRANSFER, true)->set(CURLOPT_HEADER, true); return $curl; }
/** * Tests whether 'complete' event on individual Request has been fired * when using RequestsQueue */ public function testRequestCompleteEvent() { $eventFired = 0; $req = new cURL\Request(); $req->getOptions()->set(CURLOPT_URL, $this->createRequestUrl())->set(CURLOPT_RETURNTRANSFER, true); $req->addListener('complete', function ($event) use(&$eventFired) { $this->validateSuccesfulResponse($event->response); $eventFired++; }); $queue = new cURL\RequestsQueue(); $queue->attach($req); $queue->send(); $this->assertEquals(1, $eventFired); }
/** * Test requests attaching on run time * * @return void */ public function testAttachNewOnRuntime() { $total = 10; $test = $this; $queue = new cURL\RequestsQueue(); $queue->getDefaultOptions()->set(CURLOPT_RETURNTRANSFER, true)->set(CURLOPT_ENCODING, ''); $n = 0; $attachNew = function () use($queue, &$n, $total) { if ($n < $total) { $n++; $request = new cURL\Request(); $request->_path = '/' . $n; $request->getOptions()->set(CURLOPT_URL, $this->okTestUrl . $n); $queue->attach($request); } }; $attachNew(); $queue->addListener('complete', function (cURL\Event $event) use(&$requests, $test, $attachNew) { $test->validateSuccesfulResponse($event->request->_path, $event->response); $attachNew(); }); $queue->send(); $this->assertEquals($total, $n); }