}
/**
- * @throws HTTPException\InternalServerErrorException
+ * {@inheritDoc}
*/
public function request(string $method, string $url, array $opts = []): IHTTPResult
{
$conf = [];
- if (!empty($opts['cookiejar'])) {
- $jar = new FileCookieJar($opts['cookiejar']);
+ if (!empty($opts[HTTPRequestOptions::COOKIEJAR])) {
+ $jar = new FileCookieJar($opts[HTTPRequestOptions::COOKIEJAR]);
$conf[RequestOptions::COOKIES] = $jar;
}
- $header = [];
+ $headers = [];
- if (!empty($opts['accept_content'])) {
- $header['Accept'] = $opts['accept_content'];
+ if (!empty($opts[HTTPRequestOptions::ACCEPT_CONTENT])) {
+ $headers['Accept'] = $opts[HTTPRequestOptions::ACCEPT_CONTENT];
}
- if (!empty($opts['header'])) {
- $header = array_merge($opts['header'], $header);
+ if (!empty($opts[HTTPRequestOptions::LEGACY_HEADER])) {
+ $this->logger->notice('Wrong option \'headers\' used.');
+ $headers = array_merge($opts[HTTPRequestOptions::LEGACY_HEADER], $headers);
}
- if (!empty($opts['headers'])) {
- $this->logger->notice('Wrong option \'headers\' used.');
- $header = array_merge($opts['headers'], $header);
+ if (!empty($opts[HTTPRequestOptions::HEADERS])) {
+ $headers = array_merge($opts[HTTPRequestOptions::HEADERS], $headers);
}
- $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $header);
+ $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $headers);
+
+ if (!empty($opts[HTTPRequestOptions::TIMEOUT])) {
+ $conf[RequestOptions::TIMEOUT] = $opts[HTTPRequestOptions::TIMEOUT];
+ }
- if (!empty($opts['timeout'])) {
- $conf[RequestOptions::TIMEOUT] = $opts['timeout'];
+ if (!empty($opts[HTTPRequestOptions::BODY])) {
+ $conf[RequestOptions::BODY] = $opts[HTTPRequestOptions::BODY];
}
$conf[RequestOptions::ON_HEADERS] = function (ResponseInterface $response) use ($opts) {
- if (!empty($opts['content_length']) &&
- $response->getHeaderLine('Content-Length') > $opts['content_length']) {
+ if (!empty($opts[HTTPRequestOptions::CONTENT_LENGTH]) &&
+ (int)$response->getHeaderLine('Content-Length') > $opts[HTTPRequestOptions::CONTENT_LENGTH]) {
throw new TransferException('The file is too big!');
}
};
}
/** {@inheritDoc}
- *
- * @throws HTTPException\InternalServerErrorException
*/
public function head(string $url, array $opts = []): IHTTPResult
{
{
$opts = [];
- $opts[RequestOptions::JSON] = $params;
+ $opts[RequestOptions::BODY] = $params;
if (!empty($headers)) {
- $opts['headers'] = $headers;
+ $opts[RequestOptions::HEADERS] = $headers;
}
if (!empty($timeout)) {
--- /dev/null
+<?php
+
+namespace Friendica\Network;
+
+use GuzzleHttp\RequestOptions;
+
+/**
+ * This class contains a list of possible HTTPClient request options.
+ */
+class HTTPRequestOptions
+{
+ /**
+ * accept_content: (array) supply Accept: header with 'accept_content' as the value
+ */
+ const ACCEPT_CONTENT = 'accept_content';
+ /**
+ * timeout: (int) out in seconds, default system config value or 60 seconds
+ */
+ const TIMEOUT = RequestOptions::TIMEOUT;
+ /**
+ * cookiejar: (string) path to cookie jar file
+ */
+ const COOKIEJAR = 'cookiejar';
+ /**
+ * headers: (array) header array
+ */
+ const HEADERS = RequestOptions::HEADERS;
+ /**
+ * header: (array) header array (legacy version)
+ */
+ const LEGACY_HEADER = 'header';
+ /**
+ * content_length: (int) maximum File content length
+ */
+ const CONTENT_LENGTH = 'content_length';
+ /**
+ * body: (mixed) Setting the body for sending data
+ */
+ const BODY = RequestOptions::BODY;
+}
/**
* Sends a HTTP request to a given url
*
- * @param string $method A HTTP request ()
+ * @param string $method A HTTP request
* @param string $url Url to send to
- * @param array $opts parameters
+ * @param array $opts (optional parameters) associative array with:
+ * 'body' => (mixed) setting the body for sending data
+ * 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value
+ * 'timeout' => int Timeout in seconds, default system config value or 60 seconds
+ * 'cookiejar' => path to cookie jar file
+ * 'header' => header array
+ * 'content_length' => int maximum File content length
*
* @return IHTTPResult
*/