Beispiel #1
0
 /**
  *
  * adapt Request to HttpRequest
  *
  * {@link http://us.php.net/manual/en/http.constants.php
  *  HTTP Predefined Constant}
  *
  * {@link http://us.php.net/manual/en/http.request.options.php
  *  HttpRequest options}
  *
  * @throws InvalidArgumentException
  * @param  Request                  $request
  * @param  Endpoint                 $endpoint
  * @param  HttpRequest
  * @return \HttpRequest
  */
 public function toHttpRequest($request, $endpoint)
 {
     $url = $endpoint->getBaseUri() . $request->getUri();
     $httpRequest = new \HttpRequest($url);
     $headers = array();
     foreach ($request->getHeaders() as $headerLine) {
         list($header, $value) = explode(':', $headerLine);
         if ($header = trim($header)) {
             $headers[$header] = trim($value);
         }
     }
     // Try endpoint authentication first, fallback to request for backwards compatibility
     $authData = $endpoint->getAuthentication();
     if (empty($authData['username'])) {
         $authData = $request->getAuthentication();
     }
     if (!empty($authData['username']) && !empty($authData['password'])) {
         $headers['Authorization'] = 'Basic ' . base64_encode($authData['username'] . ':' . $authData['password']);
     }
     switch ($request->getMethod()) {
         case Request::METHOD_GET:
             $method = HTTP_METH_GET;
             break;
         case Request::METHOD_POST:
             $method = HTTP_METH_POST;
             if ($request->getFileUpload()) {
                 $httpRequest->addPostFile('content', $request->getFileUpload(), 'application/octet-stream; charset=binary');
             } else {
                 $httpRequest->setBody($request->getRawData());
                 if (!isset($headers['Content-Type'])) {
                     $headers['Content-Type'] = 'text/xml; charset=utf-8';
                 }
             }
             break;
         case Request::METHOD_HEAD:
             $method = HTTP_METH_HEAD;
             break;
         default:
             throw new InvalidArgumentException('Unsupported method: ' . $request->getMethod());
     }
     $httpRequest->setMethod($method);
     $httpRequest->setOptions(array('timeout' => $endpoint->getTimeout(), 'connecttimeout' => $endpoint->getTimeout(), 'dns_cache_timeout' => $endpoint->getTimeout()));
     $httpRequest->setHeaders($headers);
     return $httpRequest;
 }
Beispiel #2
0
 /**
  * Prepare the client to send the file and params in request
  *
  * @param  \Zend_Http_Client $client
  * @param  Request           $request
  * @return void
  */
 protected function prepareFileUpload($client, $request)
 {
     $filename = $request->getFileUpload();
     $client->setFileUpload('content', 'content', file_get_contents($filename), 'application/octet-stream; charset=binary');
 }
Beispiel #3
0
 /**
  * Create a stream context for a request
  *
  * @param  Request  $request
  * @param  Endpoint $endpoint
  * @return resource
  */
 public function createContext($request, $endpoint)
 {
     $method = $request->getMethod();
     $context = stream_context_create(array('http' => array('method' => $method, 'timeout' => $endpoint->getTimeout())));
     if ($method == Request::METHOD_POST) {
         if ($request->getFileUpload()) {
             stream_context_set_option($context, 'http', 'content', file_get_contents($request->getFileUpload()));
             $request->addHeader('Content-Type: multipart/form-data');
         } else {
             $data = $request->getRawData();
             if (null !== $data) {
                 stream_context_set_option($context, 'http', 'content', $data);
                 $request->addHeader('Content-Type: text/xml; charset=UTF-8');
             }
         }
     }
     // Try endpoint authentication first, fallback to request for backwards compatibility
     $authData = $endpoint->getAuthentication();
     if (empty($authData['username'])) {
         $authData = $request->getAuthentication();
     }
     if (!empty($authData['username']) && !empty($authData['password'])) {
         $request->addHeader('Authorization: Basic ' . base64_encode($authData['username'] . ':' . $authData['password']));
     }
     $headers = $request->getHeaders();
     if (count($headers) > 0) {
         stream_context_set_option($context, 'http', 'header', implode("\r\n", $headers));
     }
     return $context;
 }
Beispiel #4
0
 public function testSetAndGetFileUpload()
 {
     $this->request->setFileUpload(__FILE__);
     $this->assertEquals(__FILE__, $this->request->getFileUpload());
 }