protected function sendRequest($uri, $method, $data = null) { try { $url = new \http\Url("https://{$this->active_domain}.chargify.com{$uri}", null, \http\Url::PARSE_MBLOC | \http\Url::PARSE_MBUTF8 | \http\Url::PARSE_TOPCT); $request = new Request($method, $url); $request->setHeaders(array('Content-Type' => 'application/xml')); $request->setOptions(array('httpauth' => "{$this->active_api_key}:x", 'timeout' => 45, 'connecttimeout' => 45)); if ($data) { $request->getBody()->append($data); } } catch (Exception $e) { //TODO: throw $e; } $response = (new \http\Client())->enqueue($request)->send()->getResponse($request); return $response; }
<?php use http\Client, http\Client\Request; use http\QueryString; //$params = new QueryString; $params["foo"] = "bar"; $params["bar"] = "foo"; $request = new Request("POST", "http://www.google.com"); $request->getBody()->append($params); $request->setContentType("application/x-www-form-urlencoded"); $client = new Client(); $client->enqueue($request); $client->send(); $response = $client->getResponse($request); printf("Sent:\n%s\n\n", $response->getParentMessage()); printf("%s returned '%s'\n%s\n", $response->getTransferInfo("effective_url"), $response->getInfo(), $response->getBody()); ?> Sent: POST http://example.com HTTP/1.1 Content-Type: application/x-www-form-urlencoded Content-Length: 7 bar=foo http://example.com/ returned 'HTTP/1.1 200 OK' <!doctype html> <html> <head> <title>Example Domain</title>
protected function sendRequest($uri, $method, $data = null) { try { $url = new \http\Url("https://{$this->domain}.chargify.com{$uri}", null, \http\Url::PARSE_MBLOC | \http\Url::PARSE_MBUTF8 | \http\Url::PARSE_TOPCT); $request = new Request($method, $url); $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' => 6))); $request->getBody()->append($data); } catch (Exception $e) { throw $e; } $response = (new \http\Client())->enqueue($request)->send()->getResponse($request); if ($response->getResponseCode() == 500) { throw new Exception("Unable to proceed your request at the moment. Please try again later."); } if ($response->getResponseCode() == 404) { throw new Exception("Unable to proceed your request. Please contact billing@scalr.net to get help."); } return $response; }