private function getMessage(HttpResponse $response) : string { $dom = domdocument_load_html($response->getBody()); if ($dom->getElementsByTagName('description')->length === 0) { return 'I dun goofed'; } preg_match('/([^:]+)/', $dom->getElementsByTagName('description')->item(2)->textContent, $before); preg_match('/\\:(.*)/', $dom->getElementsByTagName('description')->item(2)->textContent, $after); return '**[' . $before[0] . '](http://www.dictionary.com/browse/' . str_replace(" ", "-", $before[0]) . ')**' . $after[0]; }
private function getMessageFromSearch2(Response $response) : string { $internalErrors = libxml_use_internal_errors(true); $dom = new \DOMDocument(); $dom->loadHTML($response->getBody()); libxml_use_internal_errors($internalErrors); $xpath = new \DOMXPath($dom); $firstResult = $dom->getElementById('quickref_functions')->getElementsByTagName('li')->item(0); var_dump($firstResult); return sprintf('[ [%s](%s) ] %s', $firstResult->textContent, 'https://php.net' . $firstResult->getElementsByTagName('a')->item(0)->getAttribute('href'), 'foo'); }
private function getMessage(Response $response) : string { $internalErrors = libxml_use_internal_errors(true); $dom = new \DOMDocument(); $dom->loadHTML($response->getBody()); libxml_use_internal_errors($internalErrors); if ($dom->getElementsByTagName('resultset')->length === 0) { return 'I cannot find that title.'; } $result = $dom->getElementsByTagName('imdbentity')->item(0); return sprintf('[ [%s](%s) ] %s', $result->firstChild->wholeText, 'http://www.imdb.com/title/' . $result->getAttribute('id'), $result->getElementsByTagName('description')->item(0)->textContent); }
private function getMessage(HttpResponse $response) : string { $dom = domdocument_load_html($response->getBody()); if ($dom->getElementsByTagName('resultset')->length === 0) { return 'I cannot find that title.'; } /** @var \DOMElement $result */ $result = $dom->getElementsByTagName('imdbentity')->item(0); /** @var \DOMText $titleNode */ $titleNode = $result->firstChild; return sprintf('[ [%s](%s) ] %s', $titleNode->wholeText, 'http://www.imdb.com/title/' . $result->getAttribute('id'), $result->getElementsByTagName('description')->item(0)->textContent); }
private function getMessage(Response $response) : string { if ($response->getStatus() !== 200) { return "Something seems to be not working fine. Please visit https://tryhaskell.org/ directly."; } $data = json_decode($response->getBody(), true); if (isset($data["success"])) { $output = str_replace("`", "\\`", preg_replace("~\\s~", " ", implode(" ", $data["success"]["stdout"]))); return sprintf("Return value: `%s` — Output: %s", str_replace("`", "\\`", $data["success"]["value"]), $output ? "`{$output}`" : "*none*"); } else { if (isset($data["error"])) { return sprintf("Error: `%s`", str_replace("`", "\\`", preg_replace("~\\s~", " ", $data["error"]))); } } return "Something went wrong, it has to be fixed in code."; }
private function inflateGzipBody(Response $response) { $src = $response->getBody(); if (is_resource($src)) { $destination = fopen('php://memory', 'r+'); fseek($src, 10, SEEK_SET); stream_filter_prepend($src, 'zlib.inflate', STREAM_FILTER_READ); stream_copy_to_stream($src, $destination); rewind($destination); $response->setBody($destination); } elseif (strlen($src)) { $body = gzdecode($src); $response->setBody($body); } }
/** * Generates a new exception using the response to provide details. * * @param Response $response HTTP response to generate the exception from * @return AcmeException exception generated from the response body */ private function generateException(Response $response) { $body = $response->getBody(); $status = $response->getStatus(); $info = json_decode($body); $uri = $response->getRequest()->getUri(); if (isset($info->type, $info->detail)) { return new AcmeException("Invalid response: {$info->detail}.\nRequest URI: {$uri}.", $info->type); } return new AcmeException("Invalid response: {$body}.\nRequest URI: {$uri}.", $status); }
private function getMessageFromSearch(HttpResponse $response) : \Generator { try { $dom = domdocument_load_html($response->getBody()); /** @var \DOMElement $firstResult */ $firstResult = $dom->getElementById("quickref_functions")->getElementsByTagName("li")->item(0); /** @var \DOMElement $anchor */ $anchor = $firstResult->getElementsByTagName("a")->item(0); $response = (yield $this->httpClient->request(self::URL_BASE . $anchor->getAttribute("href"))); return $this->getMessageFromMatch($response); } catch (\Throwable $e) { return 'Something went badly wrong with that lookup... ' . $e->getMessage(); } }
/** * Dispatch the request for this operation and process the response. Allows you to * modify the request before it is sent. * * @return mixed * @param \Amp\Artax\Response $response The HTTP response. */ public function processResponse(\Amp\Artax\Response $response) { return $response->getBody(); }
protected function extractData(Response $response) { $contentType = null; if ($response->hasHeader('Content-Type') === false) { throw new GithubArtaxServiceException("Content-Type header not set in response."); } $contentTypeHeaders = $response->getHeader('Content-Type'); foreach ($contentTypeHeaders as $contentTypeHeader) { $parts = explode(';', $contentTypeHeader); foreach ($parts as $part) { switch ($part) { case 'application/x-www-form-urlencoded': $formEncoded = $response->getBody(); $data = []; parse_str($formEncoded, $data); return $data; case 'application/json': return $this->decodeJson($response); } } } $headersString = implode(',', $contentTypeHeaders); throw new GithubArtaxServiceException("Could not determine how to extract data from {$headersString}"); }
/** * @return ResponseInterface */ protected function convertResponse(Response $artaxResponse, RequestInterface $request, array $requestOptions) { return new \GuzzleHttp\Psr7\Response($artaxResponse->getStatus(), $artaxResponse->getAllHeaders(), $artaxResponse->getBody(), $artaxResponse->getProtocol(), $artaxResponse->getReason()); }