Example #1
0
 /**
  * @param RequestHandler $handler
  * @param array $options User cURL options
  */
 public function prepareOptions(RequestHandler $handler, array $options = array())
 {
     $request = $handler->getRequest();
     $options = $this->parseOptions($options) + $this->defaultOptions;
     $options[CURLOPT_URL] = $url = $request->getUrl();
     $options[CURLOPT_HEADER] = false;
     // Set headers
     foreach ($request->getHeaders() as $name => $value) {
         $options[CURLOPT_HTTPHEADER][] = $name . ': ' . $value;
     }
     $options[CURLOPT_HEADERFUNCTION] = function ($ch, $header) use($handler) {
         $handler->addHeader((int) $ch, trim($header));
         return strlen($header);
     };
     if ($this->cookieFile) {
         $options[CURLOPT_COOKIEJAR] = $this->cookieFile->getFilename();
         $options[CURLOPT_COOKIEFILE] = $this->cookieFile->getFilename();
     }
     $method = strtoupper($request->getMethod());
     $params = $request->getParameters();
     if ($method === 'HEAD') {
         $options[CURLOPT_NOBODY] = true;
     } elseif ($method !== 'GET' && $method !== 'POST') {
         $options[CURLOPT_CUSTOMREQUEST] = $method;
     }
     $isFormMethod = in_array($method, array('POST', 'PUT', 'PATCH'));
     if ($method !== 'GET' && $method !== 'HEAD') {
         if ($request->getBody()) {
             $options[CURLOPT_POSTFIELDS] = $request->getBody();
         } elseif ($isFormMethod) {
             $options[CURLOPT_POST] = true;
             $options[CURLOPT_POSTFIELDS] = http_build_query($params);
         }
     }
     if (!$isFormMethod && $params) {
         // Add url query from form parameters.
         if (($pos = strpos($url, '?')) !== false) {
             $url = substr($url, 0, $pos);
         }
         $options[CURLOPT_URL] = $url . '?' . http_build_query($params);
         $request->setUrl($options[CURLOPT_URL]);
     }
     if ($cookies = $request->getCookies()) {
         if (count($cookies) > 0) {
             $options[CURLOPT_COOKIE] = $cookies[0];
         }
     }
     $this->options = $options;
     $handler->setOptions($options);
 }
Example #2
0
 /**
  * Handles response cURL resource.
  *
  * @param RequestHandler $handler
  * @param resource $ch
  * @param bool $multiRequest
  */
 private function handleResource(RequestHandler $handler, $ch, $multiRequest = false)
 {
     // Create response
     $response = $handler->createResponse($ch);
     // Dispatch event
     $this->dispatcherHelper->requestComplete($handler->getRequest(), $response, $this, $multiRequest);
     // Add response to collection
     $this->responseCollection->add($response, (int) $ch);
 }