Esempio n. 1
0
 public function call(RequestInterface $request, ResponseInterface $response)
 {
     $handle = \fsockopen($request->getUrl()->getHostname(), $request->getPort(), $errno, $errstr, 30);
     if (!$handle) {
         throw new \RuntimeException('Error connecting to host: ' . $request->getUrl()->getHostname());
     }
     $string = '';
     if ($handle) {
         $builder = new StringBuilder();
         $builder->setRequest($request);
         fputs($handle, $builder->toString());
         while (false === feof($handle)) {
             $string .= fgets($handle, 1024);
         }
     }
     $response->parse($string);
     fclose($handle);
     if ($response->getCode() == 301) {
         $response->getHeaders()->get('Location')->getValue();
         $request->getUrl()->setUrl($response->getHeaders()->get('Location')->getValue());
         $request->setCookies($response->getCookies());
         $class = \get_class($response);
         $response = $this->call($request, new $class());
     }
     return $response;
 }
Esempio n. 2
0
 public function call(RequestInterface $request, ResponseInterface $response)
 {
     $this->initialize();
     $this->createOutFile($response);
     \curl_setopt($this->handle, CURLOPT_URL, $request->getUrl());
     if ($request->getMethod() == 'GET') {
         \curl_setopt($this->handle, \CURLOPT_HTTPGET, true);
     } elseif ($request->getMethod() == 'POST') {
         \curl_setopt($this->handle, \CURLOPT_POST, true);
     } else {
         \curl_setopt($this->handle, \CURLOPT_CUSTOMREQUEST, $request->getMethod());
     }
     $post = array();
     $files = array();
     foreach ($request->getParameters() as $param) {
         $post[$param->getName()] = $param->getValue();
     }
     foreach ($request->getFiles() as $file) {
         $files[$file->getName()] = '@' . $file->getValue() . ';type=' . $file->getType();
     }
     if ($request->getMethod() == 'POST' || $request->getMethod() == 'PUT') {
         if (!empty($files)) {
             $body = array_merge($post, $files);
         } elseif (!empty($post)) {
             $body = \http_build_query($post);
         } else {
             $body = $request->getBody();
         }
         \curl_setopt($this->handle, \CURLOPT_POSTFIELDS, $body);
     }
     if ($request->getMethod() == 'POST' || $request->getMethod() == 'PUT') {
         \curl_setopt($this->handle, \CURLOPT_POSTFIELDS, $post);
     }
     if (!$request->getCookies()->isEmpty()) {
         \curl_setopt($this->handle, \CURLOPT_COOKIE, $request->getCookies()->toString());
     }
     if (!$request->getHeaders()->isEmpty()) {
         \curl_setopt($this->handle, \CURLOPT_HTTPHEADER, $request->getHeaders()->toArray());
     }
     \curl_setopt($this->handle, \CURLOPT_PORT, $request->getPort());
     \curl_setopt($this->handle, \CURLOPT_FILE, $this->fileHandle);
     $returned = \curl_exec($this->handle);
     $info = \curl_getinfo($this->handle);
     \fclose($this->fileHandle);
     $response->setCode($info['http_code']);
     return $response;
 }
Esempio n. 3
0
 public function call(RequestInterface $request, ResponseInterface $response)
 {
     $this->initialize();
     \curl_setopt($this->handle, CURLOPT_URL, $request->getUrl());
     if ($request->getMethod() == 'GET') {
         \curl_setopt($this->handle, \CURLOPT_HTTPGET, true);
     } elseif ($request->getMethod() == 'POST') {
         \curl_setopt($this->handle, \CURLOPT_POST, true);
     } else {
         \curl_setopt($this->handle, \CURLOPT_CUSTOMREQUEST, $request->getMethod());
     }
     $post = array();
     $files = array();
     foreach ($request->getParameters() as $param) {
         $post[$param->getName()] = $param->getValue();
     }
     foreach ($request->getFiles() as $file) {
         $files[$file->getName()] = '@' . $file->getValue() . ';type=' . $file->getType();
     }
     if ($request->getMethod() == 'POST' || $request->getMethod() == 'PUT' || $request->getMethod() == 'PATCH') {
         // Files and any parameters - note body is not used
         if (!empty($files)) {
             $body = array_merge($post, $files);
         } elseif (!empty($post)) {
             $body = http_build_query($post);
         } else {
             $body = $request->getBody();
         }
         \curl_setopt($this->handle, \CURLOPT_POSTFIELDS, $body);
     }
     if (!$request->getCookies()->isEmpty()) {
         \curl_setopt($this->handle, \CURLOPT_COOKIE, $request->getCookies()->toString());
     }
     if (!$request->getHeaders()->isEmpty()) {
         \curl_setopt($this->handle, \CURLOPT_HTTPHEADER, $request->getHeaders()->toArray());
     }
     \curl_setopt($this->handle, \CURLOPT_PORT, $request->getPort());
     $returned = \curl_exec($this->handle);
     if (!$returned) {
         throw new \RuntimeException('Error connecting to host: ' . $request->getUrl()->getHostname());
     }
     $response->parse($returned);
     return $response;
 }