Inheritance: extends Guzzle\Http\Message\EntityEnclosingRequest
Exemplo n.º 1
0
 /**
  * @param string $request
  * @param string $location
  * @param string $action
  * @param integer $version
  * @param int $one_way
  *
  * @throws \VCR\VCRException It this method is called although VCR is disabled.
  *
  * @return string SOAP response.
  */
 public function doRequest($request, $location, $action, $version, $one_way = 0)
 {
     if ($this->status === self::DISABLED) {
         throw new VCRException('Hook must be enabled.', VCRException::LIBRARY_HOOK_DISABLED);
     }
     $vcrRequest = new Request('POST', $location);
     $contentType = $version == SOAP_1_2 ? 'application/soap+xml' : 'text/xml';
     $vcrRequest->addHeader('Content-Type', $contentType . '; charset=utf-8; action="' . $action . '"');
     $vcrRequest->setBody($request);
     $requestCallback = self::$requestCallback;
     $response = $requestCallback($vcrRequest);
     return (string) $response->getBody(true);
 }
Exemplo n.º 2
0
 /**
  * Returns a Request from specified stream context and path.
  *
  * If an existing Request is given, the stream context options
  * are set on the specified Request object.
  *
  * @param resource $context Stream context resource.
  * @param string $path Path to use as url.
  * @param Request $existing Optional, existing request.
  *
  * @return Request
  */
 public static function createRequestFromStreamContext($context, $path, Request $existing = null)
 {
     $http = self::getHttpOptionsFromContext($context);
     $request = $existing;
     if (empty($request)) {
         $method = !empty($http['method']) ? $http['method'] : 'GET';
         $request = new Request($method, $path, array());
     }
     if (!empty($http['header'])) {
         $headers = HttpUtil::parseHeaders(HttpUtil::parseRawHeader($http['header']));
         foreach ($headers as $key => $value) {
             $request->setHeader($key, $value);
         }
     }
     if (!empty($http['content'])) {
         $request->setBody($http['content']);
     }
     if (!empty($http['user_agent'])) {
         $request->setHeader('User-Agent', $http['user_agent']);
     }
     if (isset($http['follow_location'])) {
         $request->setCurlOption(CURLOPT_FOLLOWLOCATION, (bool) $http['follow_location']);
     }
     if (isset($http['max_redirects'])) {
         $request->setCurlOption(CURLOPT_MAXREDIRS, $http['max_redirects']);
     }
     if (isset($http['timeout'])) {
         $request->setCurlOption(CURLOPT_TIMEOUT, $http['timeout']);
     }
     // TODO: protocol_version
     return $request;
 }
Exemplo n.º 3
0
 /**
  * @param string $request
  * @param string $location
  * @param string $action
  * @param integer $version
  * @param int $one_way
  *
  * @throws \VCR\VCRException It this method is called although VCR is disabled.
  *
  * @return string SOAP response.
  */
 public function doRequest($request, $location, $action, $version, $one_way = 0, $options = array())
 {
     if ($this->status === self::DISABLED) {
         throw new VCRException('Hook must be enabled.', VCRException::LIBRARY_HOOK_DISABLED);
     }
     $vcrRequest = new Request('POST', $location);
     $contentType = $version == SOAP_1_2 ? 'application/soap+xml' : 'text/xml';
     $vcrRequest->setHeader('Content-Type', $contentType . '; charset=utf-8; action="' . $action . '"');
     $vcrRequest->setBody($request);
     if (!empty($options['login'])) {
         $vcrRequest->setAuthorization($options['login'], $options['password']);
     }
     /* @var \VCR\Response $response */
     $requestCallback = self::$requestCallback;
     $response = $requestCallback($vcrRequest);
     return $response->getBody();
 }
Exemplo n.º 4
0
 /**
  * Returns a response for given request or null if not found.
  *
  * @param Request $request Request.
  *
  * @return Response|null Response for specified request.
  */
 public function playback(Request $request)
 {
     foreach ($this->storage as $recording) {
         $storedRequest = Request::fromArray($recording['request']);
         if ($storedRequest->matches($request, $this->getRequestMatchers())) {
             return Response::fromArray($recording['response']);
         }
     }
     return null;
 }
Exemplo n.º 5
0
 /**
  * Returns a response for specified HTTP request.
  *
  * @param Request $request HTTP Request to send.
  *
  * @return Response Response for specified request.
  */
 public function send(Request $request)
 {
     $ch = curl_init($request->getUrl());
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod());
     curl_setopt($ch, CURLOPT_HTTPHEADER, HttpUtil::formatHeadersForCurl($request->getHeaders()));
     if (!is_null($request->getBody())) {
         curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getBody());
     } else {
         if (!is_null($request->getPostFields())) {
             curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request->getPostFields()));
         }
     }
     curl_setopt_array($ch, $request->getCurlOptions());
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_FAILONERROR, false);
     curl_setopt($ch, CURLOPT_HEADER, true);
     list($status, $headers, $body) = HttpUtil::parseResponse(curl_exec($ch));
     return new Response(HttpUtil::parseStatus($status), HttpUtil::parseHeaders($headers), $body, curl_getinfo($ch));
 }
Exemplo n.º 6
0
 /**
  * Sets a cURL option on a Request.
  *
  * @param Request  $request Request to set cURL option to.
  * @param integer  $option  cURL option to set.
  * @param mixed    $value   Value of the cURL option.
  * @param resource $curlHandle cURL handle where this option is set on (optional).
  */
 public static function setCurlOptionOnRequest(Request $request, $option, $value, $curlHandle = null)
 {
     switch ($option) {
         case CURLOPT_URL:
             $request->setUrl($value);
             break;
         case CURLOPT_FOLLOWLOCATION:
             $request->getParams()->set('redirect.disable', !$value);
             break;
         case CURLOPT_MAXREDIRS:
             $request->getParams()->set('redirect.max', $value);
             break;
         case CURLOPT_CUSTOMREQUEST:
             $request->setMethod($value);
             break;
         case CURLOPT_POST:
             if ($value == true) {
                 $request->setMethod('POST');
             }
             break;
         case CURLOPT_POSTFIELDS:
             // todo: check for file @
             if (is_array($value)) {
                 foreach ($value as $name => $fieldValue) {
                     $request->setPostField($name, $fieldValue);
                 }
                 if (count($value) == 0) {
                     $request->removeHeader('Content-Type');
                 }
             } else {
                 $request->setBody($value);
             }
             break;
         case CURLOPT_HTTPHEADER:
             foreach ($value as $header) {
                 $headerParts = explode(': ', $header, 2);
                 if (isset($headerParts[1])) {
                     $request->setHeader($headerParts[0], $headerParts[1]);
                 }
             }
             break;
         case CURLOPT_HEADER:
         case CURLOPT_WRITEFUNCTION:
         case CURLOPT_HEADERFUNCTION:
             // Ignore writer and header functions.
             // These options are stored and will be handled later in handleOutput().
             break;
         case CURLOPT_READFUNCTION:
             // Guzzle provides a callback to let curl read the body string.
             // To get the body, this callback is called manually.
             $bodySize = $request->getCurlOptions()->get(CURLOPT_INFILESIZE);
             Assertion::notEmpty($bodySize, "To set a CURLOPT_READFUNCTION, CURLOPT_INFILESIZE must be set.");
             $body = call_user_func_array($value, array($curlHandle, fopen('php://memory', 'r'), $bodySize));
             $request->setBody($body);
             break;
         default:
             $request->getCurlOptions()->set($option, $value);
             break;
     }
 }
Exemplo n.º 7
0
 /**
  * Creates a new Request from a specified array.
  *
  * @param  array  $request Request represented as an array.
  *
  * @return Request A new Request from specified array.
  */
 public static function fromArray(array $request)
 {
     $requestObject = new Request($request['method'], $request['url'], $request['headers']);
     if (!empty($request['post_fields']) && is_array($request['post_fields'])) {
         $requestObject->addPostFields($request['post_fields']);
     }
     if (!empty($request['post_files']) && is_array($request['post_files'])) {
         foreach ($request['post_files'] as $file) {
             $requestObject->addPostFile(new PostFile($file['fieldName'], $file['filename'], $file['contentType'], $file['postname']));
         }
     }
     return $requestObject;
 }
Exemplo n.º 8
0
 /**
  * Makes sure we've properly handled the POST body, such as ensuring that
  * CURLOPT_INFILESIZE is set if CURLOPT_READFUNCTION is set.
  *
  * @param Request  $request Request to set cURL option to.
  * @param resource $curlHandle cURL handle associated with the request.
  */
 public static function validateCurlPOSTBody(Request $request, $curlHandle = null)
 {
     $readFunction = $request->getCurlOption(CURLOPT_READFUNCTION);
     if (is_null($readFunction)) {
         return;
     }
     // Guzzle 4 sometimes sets the post body in CURLOPT_POSTFIELDS even if
     // they have already set CURLOPT_READFUNCTION.
     if ($request->getBody()) {
         return;
     }
     $bodySize = $request->getCurlOption(CURLOPT_INFILESIZE);
     Assertion::notEmpty($bodySize, "To set a CURLOPT_READFUNCTION, CURLOPT_INFILESIZE must be set.");
     $body = call_user_func_array($readFunction, array($curlHandle, fopen('php://memory', 'r'), $bodySize));
     $request->setBody($body);
 }