diff options
Diffstat (limited to 'misc')
| -rw-r--r-- | misc/send_request_authenticated.php | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/misc/send_request_authenticated.php b/misc/send_request_authenticated.php index 6e74cac..842334c 100644 --- a/misc/send_request_authenticated.php +++ b/misc/send_request_authenticated.php @@ -17,7 +17,8 @@ function send_request_authenticated( bool $dontLookUpInstance = false, string $method = 'GET', ?Actor $actingAs = null, - ?string $body = null + ?string $body = null, + ?string $contentType = null ) { if (!str_starts_with($uri, 'https://')) { throw new RuntimeException('refusing to fetch a non-https uri'); @@ -33,10 +34,18 @@ function send_request_authenticated( $instance = Instance::findByHostname($remoteHost); } + if ($body != null && $method != 'GET') { + $contentType ??= 'application/x-www-form-urlencoded'; + } + if (!isset($instance->auth->outboundToken) || $instance->auth->outboundToken == null) { // hopefully we can make the request anyway? - $context = stream_context_create(['http' => ['method' => $method, 'content' => $body]]); - $resp = file_get_contents($uri); + $context = stream_context_create(['http' => [ + 'method' => $method, + 'content' => $body, + 'header' => isset($contentType) ? "Content-Type: $contentType" : null + ]]); + $resp = file_get_contents($uri, context: $context); // $http_response_header just poofs into existence . // i don't like this api. i should probably use curl instead. hmm // TODO: use curl instead of file_get_contents @@ -56,9 +65,13 @@ function send_request_authenticated( if ($actingAs != null) { $header .= "\nX-PawPub-Actor: $actingAs->uri"; } + if ($contentType != null) { + $header .= "\nContent-Type: $contentType"; + } $context = stream_context_create(['http' => [ 'method' => $method, - 'header' => $header + 'header' => $header, + 'content' => $body ]]); $result = new stdClass(); $result->body = file_get_contents($uri, context: $context); |
