Example #1
0
 /**
  * Returns a cURL option from a Response.
  *
  * @param  Response $response Response to get cURL option from.
  * @param  integer $option cURL option to get.
  *
  * @throws \BadMethodCallException
  * @return mixed Value of the cURL option.
  */
 public static function getCurlOptionFromResponse(Response $response, $option = 0)
 {
     switch ($option) {
         case 0:
             // 0 == array of all curl options
             $info = array();
             foreach (self::$curlInfoList as $option => $key) {
                 $info[$key] = $response->getCurlInfo($key);
             }
             break;
         case CURLINFO_HTTP_CODE:
             $info = $response->getStatusCode();
             break;
         case CURLINFO_SIZE_DOWNLOAD:
             $info = $response->getHeader('Content-Length');
             break;
         case CURLINFO_HEADER_SIZE:
             $info = mb_strlen(HttpUtil::formatAsStatusWithHeadersString($response), 'ISO-8859-1');
             break;
         default:
             $info = $response->getCurlInfo($option);
             break;
     }
     if (!is_null($info)) {
         return $info;
     }
     $constants = get_defined_constants(true);
     $constantNames = array_flip($constants['curl']);
     throw new \BadMethodCallException("Not implemented: {$constantNames[$option]} ({$option}) ");
 }
Example #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;
 }
Example #3
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());
     }
     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));
 }
Example #4
0
 /**
  * Should mimic the format returned by PHP when using http_response_header.
  * See: http://php.net/manual/en/reserved.variables.httpresponseheader.php.
  */
 public static function getLastResponseHeaders()
 {
     return HttpUtil::buildHeaders(self::getLastResponse());
 }