示例#1
0
 /**
  * Makes api request
  *
  * @param   string     $qid     The id of the query.
  * @param   array      $options optional Query options for the request.
  * @param   string     $path    optional Uri path for the request (/user by default)
  * @param   string     $method  optional Http method (GET by default)
  * @return  object     Returns object that is an response data.
  * @throws  CloudynException
  */
 public function call($qid, array $options = array(), $path = '/user', $method = 'GET')
 {
     $options['qid'] = (string) $qid;
     $options['out'] = self::OUT_JSON;
     if (!isset($options['rqid'])) {
         $options['rqid'] = $this->getRequestId();
     }
     if (!isset($options['apiversion'])) {
         $options['apiversion'] = '0.4';
     }
     $this->request = $this->createNewRequest();
     $this->request->setUrl($this->getUrl() . $path);
     $this->request->setMethod(constant('HTTP_METH_' . strtoupper($method)));
     $this->request->setOptions(array('redirect' => 10, 'useragent' => 'Scalr Client (http://scalr.com)'));
     $this->request->addQueryData($options);
     //This line is very necessary or HttpResponce will add stored cookies
     $this->request->resetCookies();
     $this->message = $this->tryCall($this->request);
     $json = $this->message->getBody();
     $json = preg_replace('#^[^\\{\\[]+|[^\\}\\]]+$#', '', trim($json));
     $obj = json_decode($json);
     if (isset($obj->status) && $obj->status != 'ok' && isset($obj->message)) {
         throw new CloudynException('Cloudyn error. ' . $obj->message);
     }
     return $obj;
 }
 public function __construct(HttpMessage $response = null)
 {
     if ($response && $response->getType() != HTTP_MSG_RESPONSE) {
         throw new Kwf_Exception("invalid response type");
     }
     if ($response && ($response->getResponseCode() == 301 || $response->getResponseCode() == 302)) {
         //workaround for strange pecl_http bug that breaks requests that redirect to the same url again
         //and for some reason then there is only response message containing in the body the second response message (including http headers)
         if (preg_match('#^HTTP/1\\.. [0-9]{3} #', $response->getBody())) {
             $r = HttpMessage::factory($response->getBody());
             if ($r->getType() == HTTP_MSG_RESPONSE) {
                 $response = $r;
             }
         }
     }
     $this->_response = $response;
 }
示例#3
0
 /**
  * Perform the HTTP request.
  *
  * @param      \phpcouch\http\HttpRequest HTTP Request object
  *
  * @return     \phpcouch\http\HttpResponse  The response from the server
  *
  * @author     Simon Thulbourn <*****@*****.**>
  * @since      1.0.0
  */
 public function sendRequest(\phpcouch\http\HttpRequest $request)
 {
     $internalRequest = new \HttpRequest($request->getDestination(), self::$httpMethods[$request->getMethod()]);
     // additional headers
     foreach ($request->getHeaders() as $key => $values) {
         foreach ($values as $value) {
             $this->headers[$key] = $value;
         }
     }
     if (!isset($this->headers['Content-Type'])) {
         $this->headers['Content-Type'] = 'application/json';
     }
     if (null !== ($payload = $request->getContent())) {
         if (is_resource($payload)) {
             // This adapter has no real stream support as of now
             $payload = stream_get_contents($payload, -1, 0);
         }
         if ('PUT' == $request->getMethod()) {
             $internalRequest->setPutData($payload);
         } elseif ('POST' == $request->getMethod()) {
             $internalRequest->setBody($payload);
             $this->headers['Content-Length'] = strlen($payload);
         }
     }
     $internalRequest->addHeaders($this->headers);
     $message = new \HttpMessage($internalRequest->send());
     $response = new HttpResponse();
     $response->setStatusCode($message->getResponseCode());
     if (!isset($response)) {
         throw new TransportException('Could not read HTTP response status line');
     }
     foreach ($message->getHeaders() as $key => $value) {
         $response->setHeader($key, $value);
     }
     $response->setContent($message->getBody());
     if ($message->getResponseCode() >= 400) {
         if ($message->getResponseCode() % 500 < 100) {
             // a 5xx response
             throw new HttpServerErrorException($message->getResponseStatus(), $message->getResponseCode(), $response);
         } else {
             // a 4xx response
             throw new HttpClientErrorException($message->getResponseStatus(), $message->getResponseCode(), $response);
         }
     }
     return $response;
 }
 /**
  * {@inheritdoc}
  * @see Scalr\Service\Aws\Client.ClientResponseInterface::getRawContent()
  */
 public function getRawContent()
 {
     return $this->message->getBody();
 }
示例#5
0
 public function getBody()
 {
     return $this->_message->getBody();
 }
 public function getBody()
 {
     $content = array('body' => parent::getBody(), 'content-type' => $this->getHeader('content-type'));
     $document = array('url' => $this->getUrl(), 'content' => $content);
     return $this->getCharsetFront()->convert($document);
 }
示例#7
0
文件: Client.php 项目: honzap/php
 /**
  * Fetches properties from the response.
  *
  * @param \HttpMessage $response Response
  * @return array
  */
 private function getProperties(\HttpMessage $response)
 {
     // Process the XML with properties
     $properties = array();
     $reader = new \Jyxo\XmlReader();
     $reader->XML($response->getBody());
     // Ignore warnings
     while (@$reader->read()) {
         if (\XMLReader::ELEMENT === $reader->nodeType && 'D:prop' === $reader->name) {
             while (@$reader->read()) {
                 // Element must not be empty and has to look something like <lp1:getcontentlength>13744</lp1:getcontentlength>
                 if (\XMLReader::ELEMENT === $reader->nodeType && !$reader->isEmptyElement) {
                     if (preg_match('~^lp\\d+:(.+)$~', $reader->name, $matches)) {
                         // Apache
                         $properties[$matches[1]] = $reader->getTextValue();
                     } elseif (preg_match('~^D:(.+)$~', $reader->name, $matches)) {
                         // Lighttpd
                         $properties[$matches[1]] = $reader->getTextValue();
                     }
                 } elseif (\XMLReader::END_ELEMENT === $reader->nodeType && 'D:prop' === $reader->name) {
                     break;
                 }
             }
         }
     }
     return $properties;
 }
示例#8
0
function response_dump(HttpMessage $res)
{
    printf("Code: %d\n", $res->getResponseCode());
    printf("Body: %s\n", $res->getBody());
}