From e65ef40c11769b62573cf3b788c4bd4bbbace9ad Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Tue, 2 Feb 2021 20:25:10 +0100 Subject: [PATCH] add http status checks to curl functions --- CRM/TwingleCampaign/BAO/TwingleApiCall.php | 35 +++++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/CRM/TwingleCampaign/BAO/TwingleApiCall.php b/CRM/TwingleCampaign/BAO/TwingleApiCall.php index aae47fa..5d6d037 100644 --- a/CRM/TwingleCampaign/BAO/TwingleApiCall.php +++ b/CRM/TwingleCampaign/BAO/TwingleApiCall.php @@ -73,7 +73,9 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall { * project. * * @param int|null $projectId + * * @return array|false + * @throws Exception */ public function getProject(int $projectId = NULL): ?array { @@ -121,6 +123,7 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall { * * @param int $projectId * @param null|int $eventId + * * @return array * @throws Exception */ @@ -250,15 +253,23 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall { } $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); - curl_setopt($curl, CURLOPT_HTTPHEADER, [ - "x-access-code: $this->apiKey", - 'Content-Type: application/json', - ]); + curl_setopt($curl, CURLOPT_HTTPHEADER, + [ + "x-access-code: $this->apiKey", + 'Content-Type: application/json', + ] + ); $response = json_decode(curl_exec($curl), TRUE); + $curl_status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($response === FALSE) { throw new Exception('GET curl failed'); } + if ($curl_status_code == 404) { + throw new Exception('http status code 404 (not found)'); + } elseif ($curl_status_code == 500) { + throw new Exception('https status code 500 (internal error)'); + } return $response; } @@ -282,17 +293,25 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall { $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_POST, TRUE); - curl_setopt($curl, CURLOPT_HTTPHEADER, [ - "x-access-code: $this->apiKey", - 'Content-Type: application/json', - ]); + curl_setopt($curl, CURLOPT_HTTPHEADER, + [ + "x-access-code: $this->apiKey", + 'Content-Type: application/json', + ] + ); $json = json_encode($data); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); $response = json_decode(curl_exec($curl), TRUE); + $curl_status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($response === FALSE) { throw new Exception('POST curl failed'); } + if ($curl_status_code == 404) { + throw new Exception('http status code 404 (not found)'); + } elseif ($curl_status_code == 500) { + throw new Exception('https status code 500 (internal error)'); + } return $response; }