Exemplo n.º 1
0
 /**
  * Send this HTTP request
  *
  * @throws Horde_Http_Exception
  * @return Horde_Http_Response_Base
  */
 public function send()
 {
     if (!defined('HTTP_METH_' . $this->method)) {
         throw new Horde_Http_Exception('Method ' . $this->method . ' not supported.');
     }
     $httpRequest = new HttpRequest((string) $this->uri, constant('HTTP_METH_' . $this->method));
     $data = $this->data;
     if (is_array($data)) {
         $httpRequest->setPostFields($data);
     } else {
         if ($this->method == 'PUT') {
             $httpRequest->setPutData($data);
         } else {
             $httpRequest->setBody($data);
         }
     }
     $httpRequest->setOptions($this->_httpOptions());
     try {
         $httpResponse = $httpRequest->send();
     } catch (HttpException $e) {
         if (isset($e->innerException)) {
             throw new Horde_Http_Exception($e->innerException);
         } else {
             throw new Horde_Http_Exception($e);
         }
     }
     return new Horde_Http_Response_Peclhttp((string) $this->uri, $httpResponse);
 }
Exemplo n.º 2
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;
 }
Exemplo n.º 3
0
 private function Request($method, $uri, $request_body, $query_args, $headers = array())
 {
     $HttpRequest = new HttpRequest();
     $HttpRequest->setOptions(array("redirect" => 10, "useragent" => "LibWebta AWS Client (http://webta.net)"));
     $timestamp = $this->GetTimestamp();
     $signature = $this->GetRESTSignature($timestamp);
     $HttpRequest->setUrl("https://cloudfront.amazonaws.com/" . self::API_VERSION . $uri);
     $HttpRequest->setMethod($method);
     if ($query_args) {
         $HttpRequest->addQueryData($query_args);
     }
     if ($request_body) {
         if ($method == constant("HTTP_METH_POST")) {
             $HttpRequest->setRawPostData(trim($request_body));
         } else {
             $HttpRequest->setPutData(trim($request_body));
         }
         $headers["Content-type"] = "text/xml";
     }
     $headers["Date"] = $timestamp;
     $headers["Authorization"] = "AWS {$this->AWSAccessKeyId}:{$signature}";
     $HttpRequest->addHeaders($headers);
     try {
         $HttpRequest->send();
         //$info = $HttpRequest->getResponseInfo();
         $data = $HttpRequest->getResponseData();
         $this->LastResponseHeaders = $data['headers'];
         return $data['body'];
     } catch (Exception $e) {
         if ($e->innerException) {
             $message = $e->innerException->getMessage();
         } else {
             $message = $e->getMessage();
         }
         throw new Exception($message);
     }
 }
Exemplo n.º 4
0
 /**
  * The CreateBucket operation creates a bucket. Not every string is an acceptable bucket name.
  *
  * @param string $bucket_name
  * @return string 
  */
 public function CreateBucket($bucket_name, $region = 'us-east-1')
 {
     $HttpRequest = new HttpRequest();
     $HttpRequest->setOptions(array("redirect" => 10, "useragent" => "LibWebta AWS Client (http://webta.net)"));
     $timestamp = $this->GetTimestamp(true);
     switch ($region) {
         case "us-east-1":
             $request = "";
             break;
         case "us-west-1":
             $request = "<CreateBucketConfiguration><LocationConstraint>us-west-1</LocationConstraint></CreateBucketConfiguration>";
             break;
         case "eu-west-1":
             $request = "<CreateBucketConfiguration><LocationConstraint>EU</LocationConstraint></CreateBucketConfiguration>";
             break;
     }
     $data_to_sign = array("PUT", "", "", $timestamp, "/{$bucket_name}/");
     $signature = $this->GetRESTSignature($data_to_sign);
     $HttpRequest->setUrl("https://{$bucket_name}.s3.amazonaws.com/");
     $HttpRequest->setMethod(constant("HTTP_METH_PUT"));
     $headers = array("Content-length" => strlen($request), "Date" => $timestamp, "Authorization" => "AWS {$this->AWSAccessKeyId}:{$signature}");
     $HttpRequest->addHeaders($headers);
     if ($request != '') {
         $HttpRequest->setPutData($request);
     }
     try {
         $HttpRequest->send();
         $info = $HttpRequest->getResponseInfo();
         if ($info['response_code'] == 200) {
             return true;
         } else {
             if ($HttpRequest->getResponseBody()) {
                 $xml = @simplexml_load_string($HttpRequest->getResponseBody());
                 throw new Exception((string) $xml->Message);
             } else {
                 throw new Exception(_("Cannot create S3 bucket at this time. Please try again later."));
             }
         }
     } catch (HttpException $e) {
         throw new Exception($e->__toString());
     }
 }
<?php

$url = 'http://localhost/book/put-form-page.php';
$data = array("email" => "*****@*****.**", "display_name" => "LornaJane");
$request = new HttpRequest($url, HTTP_METH_PUT);
$request->setHeaders(array("Content-Type" => "application/x-www-form-urlencoded"));
$request->setPutData(http_build_query($data));
$request->send();
$page = $request->getResponseBody();
echo $page;
Exemplo n.º 6
0
 private function request($path, $method, $data = "")
 {
     $data = trim($data);
     $httpRequest = new HttpRequest();
     $httpRequest->setOptions(array("useragent" => "Scalr (http://scalr.com)"));
     $fullUrl = "{$this->chefServerUrl}{$path}";
     $chunks = parse_url($fullUrl);
     if ($method == 'POST' && $data) {
         if (is_array($data)) {
             $httpRequest->setPostFields($data);
         } else {
             $httpRequest->setBody($data);
         }
     }
     if ($method == 'PUT' && $data) {
         $httpRequest->setPutData($data);
     }
     $httpRequest->setUrl($fullUrl);
     $httpRequest->setMethod(constant("HTTP_METH_{$method}"));
     $tz = @date_default_timezone_get();
     date_default_timezone_set("UTC");
     $timestamp = date("Y-m-d\\TH:i:s\\Z");
     date_default_timezone_set($tz);
     $chunks['path'] = str_replace('//', '/', $chunks['path']);
     $hashedPath = base64_encode(sha1($chunks['path'], true));
     $hashedBody = base64_encode(sha1($data, true));
     $userId = $this->username;
     $str = "Method:{$method}\n" . "Hashed Path:{$hashedPath}\n" . "X-Ops-Content-Hash:{$hashedBody}\n" . "X-Ops-Timestamp:{$timestamp}\n" . "X-Ops-UserId:{$userId}";
     $headers = array('x-ops-sign' => "algorithm=sha1;version=1.0", 'x-chef-version' => "0.10.8", 'x-ops-userid' => $userId, 'x-ops-timestamp' => $timestamp, 'x-ops-content-hash' => $hashedBody, 'content-type' => 'application/json', 'accept' => 'application/json');
     $r = array_merge($headers, $this->sign($str));
     $httpRequest->addHeaders($r);
     $httpRequest->send();
     if ($httpRequest->getResponseCode() == 401) {
         throw new Exception("Failed to authenticate as {$userId}. Ensure that your node_name and client key are correct.");
     }
     if ($httpRequest->getResponseCode() == 404) {
         throw new Exception("Client not found or parameters are not valid");
     } else {
         if ($httpRequest->getResponseCode() <= 205) {
             $data = $httpRequest->getResponseData();
             $retval = $data['body'] ? json_decode($data['body']) : true;
         } else {
             if ($httpRequest->getResponseCode() > 400) {
                 $data = $httpRequest->getResponseData();
                 $msg = $data['body'] ? json_decode($data['body']) : "";
                 if (is_array($msg->error)) {
                     $msg = $msg->error[0];
                 } elseif ($msg->error) {
                     $msg = $msg->error;
                 } else {
                     $msg = "Unknown error. Error code: {$httpRequest->getResponseCode()}";
                 }
                 throw new Exception("Request to chef server failed with error: {$msg} ({$method} {$path})");
             } else {
                 throw new Exception("Unexpected situation. Response code {$httpRequest->getResponseCode()}");
             }
         }
     }
     return $retval;
 }
Exemplo n.º 7
0
$key = 'hoge';
$url = $base_url . $key;
$data = 'Tokyo Cabinet';
function response_dump(HttpMessage $res)
{
    printf("Code: %d\n", $res->getResponseCode());
    printf("Body: %s\n", $res->getBody());
}
echo "GET:\n";
$req = new HttpRequest($url, HttpRequest::METH_GET);
$res = $req->send();
response_dump($res);
echo "\n";
echo "PUT:\n";
$req = new HttpRequest($url, HttpRequest::METH_PUT);
$req->setPutData($data);
//$req->setPutFile(__FILE__);
$res = $req->send();
if ($res->getResponseCode() == 100) {
    $res = $req->send();
}
response_dump($res);
echo "\n";
echo "GET:\n";
$req = new HttpRequest($url, HttpRequest::METH_GET);
$res = $req->send();
response_dump($res);
echo "\n";
echo "DELETE:\n";
$req = new HttpRequest($url, HttpRequest::METH_DELETE);
$res = $req->send();
Exemplo n.º 8
0
 /**
  * Make a request to Fluidinfo API
  *
  * @param $method
  * @param $path
  * @param $params
  * @param $payload
  * @return object
  */
 public function call($method, $path, $params = null, $payload = null, $contenttype = 'application/json', $inPool = false)
 {
     $url = $this->prefix . $path;
     if ($params) {
         $url .= '?' . $this->array2url($params);
     }
     $ch = new HttpRequest();
     $met = 0;
     if ($method == 'POST') {
         $met = HTTP_METH_POST;
     } else {
         if ($method == 'PUT') {
             $met = HTTP_METH_PUT;
         } else {
             if ($method == 'DELETE') {
                 $met = HTTP_METH_DELETE;
             } else {
                 if ($method == 'GET') {
                     $met = HTTP_METH_GET;
                 } else {
                     if ($method == 'HEAD') {
                         $met = HTTP_METH_HEAD;
                     } else {
                     }
                 }
             }
         }
     }
     $ch->setMethod($met);
     if ($this->credentials) {
         $ch->setOptions(array('url' => $url, 'timeout' => 65, 'low_speed_time' => 65, 'useragent' => 'fluid-phpv1.1', 'httpauth' => $this->credentials));
     } else {
         $ch->setOptions(array('url' => $url, 'timeout' => 65, 'low_speed_time' => 65, 'useragent' => 'fluid-phpv1.1'));
     }
     $headers = array();
     if ($method != 'GET') {
         if ($payload or $method == 'PUT') {
             $payload = json_encode($payload);
             //$ch->setPutData($payload);
             $headers[] = 'Content-Type: ' . $contenttype;
             $headers[] = 'Content-Length: ' . strlen($payload);
             if ($method == 'POST') {
                 $ch->setRawPostData($payload);
             } else {
                 $ch->setPutData($payload);
             }
             $ch->setContentType($contenttype);
         }
     }
     $ch->addHeaders($headers);
     $ch->addHeaders(array('Expect' => ''));
     if ($inPool == false) {
         $response = $ch->send();
         $infos = $response->getHeaders();
         $output = $response->getBody();
         if ($infos['Content-Type'] == 'application/json' or $infos['Content-Type'] == 'application/vnd.fluiddb.value+json') {
             $output = json_decode($output);
         }
         $return_arr = array($response->getResponseCode(), $output, implode("\n", $infos));
         print "<pre>";
         print "Returning for method " . $method . "<br />";
         print_r($return_arr);
         print "<br />";
         print "</pre>";
         return $return_arr;
     } else {
         $this->pool->attach($ch);
     }
 }