From dd87924c796339475b8485076903b37c703b7b96 Mon Sep 17 00:00:00 2001 From: Ben P Date: Fri, 31 Dec 2021 11:50:59 -0700 Subject: [PATCH] More explicitly handle the HTTP request methods with PHP cURL to prevent errors with Nextcloud --- lib/DeckClass.php | 51 ++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/DeckClass.php b/lib/DeckClass.php index 001eea2..f43eea6 100644 --- a/lib/DeckClass.php +++ b/lib/DeckClass.php @@ -6,30 +6,31 @@ class DeckClass { $headers = [ "OCS-APIRequest: true" - ]; - if ($request !== '') {// adding attachments doesn't support Content-Type: application/json. - array_push($headers, "Content-Type: application/json"); - $options = [ - CURLOPT_USERPWD => NC_USER . ":" . NC_PASSWORD, - CURLOPT_URL => $endpoint, - CURLOPT_CUSTOMREQUEST => $request, - CURLOPT_POST => true, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_POSTFIELDS => json_encode($data), - CURLOPT_HTTPHEADER => $headers, - CURLOPT_SSLVERSION => "all", - ]; - } else { - $options = [ - CURLOPT_USERPWD => NC_USER . ":" . NC_PASSWORD, - CURLOPT_URL => $endpoint, - CURLOPT_POST => true, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_POSTFIELDS => $data, - CURLOPT_HTTPHEADER => $headers, - CURLOPT_SSLVERSION => "all", - ]; - } + ]; + + // set CURLOPTs commmon to all HTTP methods + $options = [ + CURLOPT_USERPWD => NC_USER . ":" . NC_PASSWORD, + CURLOPT_URL => $endpoint, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_SSLVERSION => "all", + ]; + + // set HTTP request specific headers and options/data + if ($request == '') {// an empty request value is used for attachments + // add data without JSON encoding or JSON Content-Type header + $options[CURLOPT_POST] = true; + $options[CURLOPT_POSTFIELDS] = $data; + } elseif ($request == "POST") { + array_push($headers, "Content-Type: application/json"); + $options[CURLOPT_POST] = true; + $options[CURLOPT_POSTFIELDS] = json_encode($data); + } elseif ($request == "GET") { + array_push($headers, "Content-Type: application/json"); + } + + // add headers to options + $options[CURLOPT_HTTPHEADER] = $headers; curl_setopt_array($curl, $options); $response = curl_exec($curl); @@ -113,4 +114,4 @@ class DeckClass { } } } -?> \ No newline at end of file +?>