Example #1
0
 public function testNotUseMasterKeyByDefault()
 {
     Client::useMasterKey(false);
     $headers = Client::buildHeaders("token", null);
     $this->assertNotContains("master", $headers["X-LC-Sign"]);
     $headers = Client::buildHeaders("token", false);
     $this->assertNotContains("master", $headers["X-LC-Sign"]);
     $headers = Client::buildHeaders("token", true);
     $this->assertContains("master", $headers["X-LC-Sign"]);
 }
Example #2
0
 private function request($url, $method, $data = null)
 {
     $appUrl = "http://" . getenv("LC_APP_HOST") . ":" . getenv("LC_APP_PORT");
     $url = $appUrl . $url;
     $headers = Client::buildHeaders(null, true);
     $headers["Content-Type"] = "application/json;charset=utf-8";
     $headers["Origin"] = getenv("LC_APP_HOST");
     // emulate CORS
     $h = array_map(function ($k, $v) {
         return "{$k}: {$v}";
     }, array_keys($headers), $headers);
     $req = curl_init($url);
     curl_setopt($req, CURLOPT_HTTPHEADER, $h);
     curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
     if ($method == "POST") {
         curl_setopt($req, CURLOPT_POST, 1);
         curl_setopt($req, CURLOPT_POSTFIELDS, json_encode($data));
     } else {
         // GET
         if ($data) {
             curl_setopt($req, CURLOPT_URL, $url . "?" . http_build_query($data));
         }
     }
     $resp = curl_exec($req);
     $errno = curl_errno($req);
     curl_close($req);
     if ($errno > 0) {
         throw new \RuntimeException("CURL connection error {$errno}: {$url}");
     }
     $data = json_decode($resp, true);
     if (isset($data["error"])) {
         $code = isset($data["code"]) ? $data["code"] : -1;
         throw new CloudException("{$code} {$data['error']}", $code);
     }
     return $data;
 }