Example #1
0
 function request($url, $options = array())
 {
     $url = "http://" . $this->host . ":" . $this->port . $url;
     $method = HTTP_METH_GET;
     $headers = array("Host" => $this->host, "Referer" => "http://localhost/", "Content-Type" => "application/json");
     $params = '';
     foreach ($options as $name => $option) {
         switch ($name) {
             case "method":
                 $method = $option;
                 break;
             case "headers":
                 $headers = array_merge($option, $headers);
                 break;
             case "params":
                 $params = http_build_query($option);
                 break;
             case "postdata":
                 $post_data = $option;
                 break;
             default:
                 trigger_error("Unknown http option: {$name}", E_USER_WARNING);
         }
     }
     if (!empty($params)) {
         $url = $url . "?" . $params;
     }
     $request = new HttpRequest($url, $method);
     $request->setHeaders($headers);
     if (isset($post_data)) {
         if ($method == HTTP_METH_PUT) {
             $request->addPutData($post_data);
         } else {
             if ($method == HTTP_METH_POST) {
                 $request->setRawPostData($post_data);
             }
         }
     }
     $json = $request->send()->getBody();
     $data = json_decode($json, true);
     if (isset($data['rows'])) {
         $data = new CouchResult($data);
     }
     return $data;
 }
Example #2
0
 protected function sendRequest($uri, $method, $data = null)
 {
     try {
         $request = new HttpRequest("https://{$this->domain}.chargify.com{$uri}");
         $request->setHeaders(array('Content-Type' => 'application/json', 'Authorization' => 'Basic ' . base64_encode("{$this->apiKey}:x")));
         $request->setOptions(array('httpauth' => "{$this->apiKey}:x", 'timeout' => 45, 'connecttimeout' => 45, 'ssl' => array('version' => 1)));
         $request->setMethod(constant("HTTP_METH_{$method}"));
         if ($method == 'POST' && $data) {
             $request->setBody($data);
         }
         if ($method == 'PUT') {
             $request->addPutData($data);
         }
     } catch (Exception $e) {
         //TODO:
         throw $e;
     }
     $request->send();
     if ($request->getResponseCode() == 500) {
         throw new Exception("Unable to proceed your request at the moment. Please try again later.");
     }
     if ($request->getResponseCode() == 404) {
         throw new Exception("Unable to proceed your request. Please contact billing@scalr.net to get help.");
     }
     return $request;
 }