/** * {@inheritdoc} */ protected function sendInternalRequest(InternalRequestInterface $internalRequest) { $body = new Body(); $body->append($this->prepareBody($internalRequest)); $request = new Request($internalRequest->getMethod(), $uri = (string) $internalRequest->getUri(), $this->prepareHeaders($internalRequest), $body); $httpVersion = $internalRequest->getProtocolVersion() === InternalRequestInterface::PROTOCOL_VERSION_1_0 ? \http\Client\Curl\HTTP_VERSION_1_0 : \http\Client\Curl\HTTP_VERSION_1_1; $request->setOptions(array('protocol' => $httpVersion, 'timeout' => $this->getConfiguration()->getTimeout())); try { $this->client->reset()->enqueue($request)->send(); } catch (\Exception $e) { throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage()); } $response = $this->client->getResponse(); return $this->getConfiguration()->getMessageFactory()->createResponse($response->getResponseCode(), $response->getHttpVersion(), $response->getHeaders(), $response->getBody()->getResource()); }
public function send() { if (is_null($this->getBody())) { $body = null; } else { $body = new Body(); $body->append($this->getBody()); } $request = new Request($this->getMethod(), 'https://' . $this->getUrl(), $this->getHeaders(), $body); $request->addQuery($this->getQuery()); $client = new Client(); $client->enqueue($request)->send(); $this->response = $client->getResponse(); }
<?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>