public function get($url, $params = array(), $headers = array(), $timeout = null) { // create the full URL if (count($params) > 0) { $url = $url . '?' . http_build_query($params); } // what are we doing? $log = usingLog()->startAction("HTTP GET '{$url}'"); // build the HTTP request $request = new HttpClientRequest($url); $request->withUserAgent("Storyplayer")->asGetRequest(); foreach ($headers as $key => $value) { $request->withExtraHeader($key, $value); } // special case - do we validate SSL certificates in this // test environment? $httpAddress = $request->getAddress(); if ($httpAddress->scheme == "https") { $validateSsl = fromConfig()->getModuleSetting("http.validateSsl"); if (null === $validateSsl) { // default to TRUE if no setting present $validateSsl = true; } if (!$validateSsl) { $request->disableSslCertificateValidation(); } } if ($timeout !== null) { $request->setReadTimeout($timeout); } // make the call $client = new HttpClient(); $response = $client->newRequest($request); // is this a valid response? if (!$response instanceof HttpClientResponse) { throw new E5xx_ActionFailed(__METHOD__); } // all done $log->endAction($response); return $response; }
/** * @param string $verb */ protected function makeHttpRequest($url, $verb, $params, $body, $headers = array(), $timeout = null) { // create the full URL if (count($params) > 0) { $url = $url . '?' . http_build_query($params); } // what are we doing? $logMsg = ["HTTP " . strtoupper($verb) . " '{$url}'"]; if ($body != null) { $logMsg[] = $body; } if (count($headers) > 0) { $logMsg[] = $headers; } $log = usingLog()->startAction($logMsg); // build the HTTP request $request = new HttpClientRequest($url); $request->withUserAgent("Storyplayer")->withHttpVerb($verb); if (is_array($headers)) { foreach ($headers as $key => $value) { $request->withExtraHeader($key, $value); } } if (is_array($body)) { foreach ($body as $key => $value) { $request->addData($key, $value); } } else { $request->setPayload($body); } // special case - do we validate SSL certificates in this // test environment? $validateSsl = fromConfig()->getModuleSetting("http.validateSsl"); if (null === $validateSsl) { // default to TRUE if no setting present $validateSsl = true; } if (!$validateSsl) { $request->disableSslCertificateValidation(); } if ($timeout !== null) { $request->setReadTimeout($timeout); } // make the call $client = new HttpClient(); $response = $client->newRequest($request); // is this a valid response? if (!$response instanceof HttpClientResponse) { throw new E5xx_ActionFailed(__METHOD__); } // all done $log->endAction($response); return $response; }