From 7eb85aeef1c603a2dc619416a6b0aa8528496bc6 Mon Sep 17 00:00:00 2001 From: sykez Date: Wed, 25 Dec 2019 20:42:25 +0800 Subject: [PATCH 1/2] Handles json post data Allows sending json payload in POST requests for the new DM endpoints. --- tmhOAuth.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tmhOAuth.php b/tmhOAuth.php index e389331..e7123ff 100644 --- a/tmhOAuth.php +++ b/tmhOAuth.php @@ -335,7 +335,7 @@ private function prepare_params() { if (isset($this->request_settings['oauth1_params'])) { $oauth1 = &$this->request_settings['oauth1_params']; $doing_oauth1 = true; - $params = array_merge($oauth1, $this->request_settings['params']); + $params = array_merge($oauth1, (is_array($this->request_settings['params']) ? $this->request_settings['params'] : [])); // Remove oauth_signature if present // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.") @@ -646,6 +646,19 @@ private function update_metrics() { return $this->metrics; } + /** + * Determines if the $string is json or not + * + * @param mixed $string + * + * @return bool + */ + private function isJson($string) { + json_decode($string); + + return (json_last_error() == JSON_ERROR_NONE); + } + /** * Utility function to create the request URL in the requested format. * If a fully-qualified URI is provided, it will be returned. @@ -781,7 +794,11 @@ private function curlit() { $this->request_settings['url'] = $this->request_settings['url'] . '?' . $this->request_settings['querystring']; } elseif ($this->request_settings['method'] == 'POST' || $this->request_settings['method'] == 'PUT') { $postfields = array(); - if (isset($this->request_settings['postfields'])) + + if ($this->isJson($this->request_settings['params'])) { + $postfields = $this->request_settings['params']; + $this->request_settings['headers']['Content-Type'] = 'application/json'; + } elseif (isset($this->request_settings['postfields'])) $postfields = $this->request_settings['postfields']; curl_setopt($c, CURLOPT_POSTFIELDS, $postfields); From a0b5e35d536550b876cb4e2aac19db9dac6fcc2c Mon Sep 17 00:00:00 2001 From: sykez Date: Fri, 3 Jan 2020 22:39:53 +0800 Subject: [PATCH 2/2] Bugfix: json_decode throws exception if it's array or object --- tmhOAuth.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tmhOAuth.php b/tmhOAuth.php index e7123ff..9360666 100644 --- a/tmhOAuth.php +++ b/tmhOAuth.php @@ -654,6 +654,10 @@ private function update_metrics() { * @return bool */ private function isJson($string) { + if (!is_string($string)) { + return false; + } + json_decode($string); return (json_last_error() == JSON_ERROR_NONE);