/** * Send this HTTP request * * @throws Horde_Http_Exception * @return Horde_Http_Response_Base */ public function send() { // at this time only the curl driver is supported $client = new \http\Client('curl'); $body = new \http\Message\Body(); $data = $this->data; if (is_array($data)) { $body->addForm($data); } else { $body->append($data); } $httpRequest = new \http\Client\Request($this->method, (string) $this->uri, $this->headers, $body); $client->setOptions($this->_httpOptions()); $client->enqueue($httpRequest); try { $client->send(); $httpResponse = $client->getResponse($httpRequest); } catch (\http\Exception $e) { throw new Horde_Http_Exception($e); } return new Horde_Http_Response_Peclhttp2((string) $this->uri, $httpResponse); }
public function sendRequest($url, array $query_data = [], $cookie = true) { /** * @var \HttpQueryString $params */ $queryData = new \http\QueryString($query_data); /** * @var \HttpRequest $http */ $request = new \http\Client\Request("GET", $url); if (!empty($queryData)) { $request->getBody()->append($queryData); } $request->setContentType("application/x-www-form-urlencoded"); $client = new \http\Client(); $cookie_file = 'cookie.log'; if ($cookie) { $cookie_str = file_exists($cookie_file) ? file_get_contents($cookie_file) : ''; $cookie = new Cookie($cookie_str); $client_cookie = $cookie->getCookie('frontend'); if ($client_cookie != 'deleted') { $client->addCookies(['frontend' => $client_cookie]); } } $client->enqueue($request); $client->send(); /** @var \HttpResponse $response */ $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()); file_put_contents($cookie_file, ''); foreach ($response->getCookies() as $cookie) { /* @var $cookie http\Cookie */ foreach ($cookie->getCookies() as $name => $value) { $cookie = new \http\Cookie(); $cookie->addCookie($name, $value); file_put_contents($cookie_file, $cookie->toString(), FILE_APPEND); } } print_r($response->getHeaders()); return json_decode($response->getBody(), true); }
/** * Sends a request. * * @param string $path Request path * @param integer $method Request method * @param array $headers Array of headers * @return \http\Client\Response * @throws \Jyxo\Webdav\Exception On error */ protected function sendRequest($path, $method, array $headers = array()) { try { // Send request to a random server $request = $this->createRequest($this->getRandomServer(), $path, $method, $headers); $client = new \http\Client(); $client->enqueue($request); $client->send(); $response = $client->getResponse(); if (null !== $this->logger) { $this->logger->log(sprintf("%s %d %s", $request->getRequestMethod(), $response->getResponseCode(), $request->getRequestUrl())); } return $response; } catch (\http\Exception $e) { throw new Exception($e->getMessage(), 0, $e); } }