From 6b69e990e166e90b817e86d78d2f75ed10651594 Mon Sep 17 00:00:00 2001 From: aymm Date: Thu, 11 Mar 2021 16:07:01 +0000 Subject: [PATCH] Fixed avatar redirection handling --- src/APnutI.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/APnutI.php b/src/APnutI.php index 0280c08..81ba4de 100644 --- a/src/APnutI.php +++ b/src/APnutI.php @@ -223,6 +223,10 @@ class APnutI } if (!empty($response)) { $response = $this->parseHeaders($response); + if ($http_status == 302) { + #echo json_encode(preg_match_all('/^Location:(.*)$/mi', $response, $matches)); + throw new HttpPnutRedirectException($response); + } if (!empty($response)) { $response = json_decode($response, true); try { @@ -262,12 +266,7 @@ class APnutI } } if ($http_status < 200 || $http_status >= 300) { - if ($http_status == 302) { - #echo json_encode(preg_match_all('/^Location:(.*)$/mi', $response, $matches)); - throw new HttpPnutRedirectException($response); - } else { - throw new HttpPnutException('HTTP error '.$http_status); - } + throw new HttpPnutException('HTTP error '.$http_status); } elseif (isset($response['meta'], $response['data'])) { return $response['data']; } elseif (isset($response['access_token'])) { @@ -514,7 +513,14 @@ class APnutI public function getAvatar(int $user_id, array $args = []): string { //get returns an array with the url at idx 0 - return $this->get('/users/'.$user_id.'/avatar', $args)[0]; + $r = null; + try { + $r = $this->get('/users/'.$user_id.'/avatar', $args); + } catch (HttpPnutRedirectException $re) { + return $re->getMessage(); + } + $this->logger->error('Could not fetch avatar: No redirection! ' . json_encode($r)); + throw new PnutException('Could not fetch avatar: No redirection!'); } public function getAvatarUrl( @@ -530,7 +536,7 @@ class APnutI if (!empty($height)) { $args['h'] = $height; } - return $this->get('/users/'.$user_id.'/avatar', $args)[0]; + return $this->getAvatar($user_id, $args); } public function updateAvatar(