예제 #1
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;
 }
예제 #2
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;
 }