Ejemplo n.º 1
0
 /**
  * Add request
  * @param \Curl\Request $request
  */
 public function addRequest(Request $request)
 {
     $reflection = new \ReflectionClass($request);
     if (!empty($this->commonRequestOptions)) {
         $possibleCurlOpts = array();
         $eventTypes = Request::getAvailableEventTypes();
         foreach ($this->commonRequestOptions as $k => $p) {
             if (is_string($k)) {
                 try {
                     if ($reflection->hasMethod($m = 'set' . ucfirst($k))) {
                         // try to set param
                         call_user_func_array(array($request, $m), (array) $p);
                     } else {
                         if (isset($eventTypes[strtolower($k)])) {
                             // try to attach event handler
                             $request->on(strtolower($k), $p);
                         }
                     }
                 } catch (\Exception $e) {
                     // TODO add debug mode
                     trigger_error('Invalid param in common options. Error: ' . $e->getMessage(), E_USER_NOTICE);
                 }
             } else {
                 if (is_numeric($k)) {
                     $possibleCurlOpts[$k] = $p;
                 }
             }
         }
         // end foreach
         // try to set cURL option
         $request->setOptions($possibleCurlOpts);
     }
     $this->requests[] = $request;
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * 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();
     }
 }
Ejemplo n.º 3
0
 /**
  * @param array $matches
  * @param ChannelContext $context
  */
 public function processMessage(array $matches, ChannelContext $context)
 {
     $uri = $matches[1];
     $req = new cURL\Request('http://ws.spotify.com/lookup/1/.json?uri=spotify:track:' . $uri);
     $req->addListener('complete', function (cURL\Event $event) use($context) {
         $this->onRequestComplete($event, $context);
     });
     $this->sendRequest($req);
 }
Ejemplo n.º 4
0
 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);
 }
Ejemplo n.º 5
0
 /**
  * @param array $matches
  * @param ChannelContext $context
  */
 public function processMessage(array $matches, ChannelContext $context)
 {
     $videoId = $matches[1];
     if ($item = $this->getCacheDriver()->fetch($videoId)) {
         $context->getLogger()->debug("Using cached entry for YouTube video {$videoId}.");
         $this->generateResponse($item, $context);
         return;
     }
     $query = http_build_query(['id' => $videoId, 'part' => 'snippet,contentDetails,statistics', 'key' => $this->apiKey]);
     $req = new cURL\Request('https://www.googleapis.com/youtube/v3/videos?' . $query);
     $req->addListener('complete', function (cURL\Event $event) use($videoId, $context) {
         $this->onRequestComplete($event, $videoId, $context);
     });
     $this->sendRequest($req);
 }
Ejemplo n.º 6
0
 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;
 }
Ejemplo n.º 7
0
 /**
  * Detach request from queue.
  *
  * @param Request $request Request to remove
  * @return self
  */
 public function detach(Request $request)
 {
     unset($this->queue[$request->getUID()]);
     return $this;
 }
Ejemplo n.º 8
0
 /**
  * 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;
 }
Ejemplo n.º 10
0
 /**
  * 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;
 }
Ejemplo n.º 11
0
 /**
  * Requests which fail very quickly might cause infinite loop and return no response.
  * http://curl.haxx.se/libcurl/c/curl_multi_perform.html
  * "If an added handle fails very quickly, it may never be counted as a running_handle."
  * This test ensures that it won't loop and will return properly error code.
  */
 public function testErrorCode()
 {
     $request = new cURL\Request('');
     $request->addListener('complete', function (cURL\Event $e) {
         $this->assertEquals('<url> malformed', $e->response->getError()->getMessage());
     });
     $queue = new cURL\RequestsQueue();
     $queue->attach($request);
     $queue->send();
 }
Ejemplo n.º 12
0
 /**
  * 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);
 }
Ejemplo n.º 13
0
 /**
  * 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);
 }