add http status checks to curl functions

This commit is contained in:
Marc Michalsky forumZFD 2021-02-02 20:25:10 +01:00
parent aef7e71e33
commit e65ef40c11
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9

View file

@ -73,7 +73,9 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
* project. * project.
* *
* @param int|null $projectId * @param int|null $projectId
*
* @return array|false * @return array|false
* @throws Exception
*/ */
public function getProject(int $projectId = NULL): ?array { public function getProject(int $projectId = NULL): ?array {
@ -121,6 +123,7 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
* *
* @param int $projectId * @param int $projectId
* @param null|int $eventId * @param null|int $eventId
*
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
@ -250,15 +253,23 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
} }
$curl = curl_init($url); $curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, [ curl_setopt($curl, CURLOPT_HTTPHEADER,
"x-access-code: $this->apiKey", [
'Content-Type: application/json', "x-access-code: $this->apiKey",
]); 'Content-Type: application/json',
]
);
$response = json_decode(curl_exec($curl), TRUE); $response = json_decode(curl_exec($curl), TRUE);
$curl_status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl); curl_close($curl);
if ($response === FALSE) { if ($response === FALSE) {
throw new Exception('GET curl failed'); 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; return $response;
} }
@ -282,17 +293,25 @@ class CRM_TwingleCampaign_BAO_TwingleApiCall {
$curl = curl_init($url); $curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE); curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, [ curl_setopt($curl, CURLOPT_HTTPHEADER,
"x-access-code: $this->apiKey", [
'Content-Type: application/json', "x-access-code: $this->apiKey",
]); 'Content-Type: application/json',
]
);
$json = json_encode($data); $json = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
$response = json_decode(curl_exec($curl), TRUE); $response = json_decode(curl_exec($curl), TRUE);
$curl_status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl); curl_close($curl);
if ($response === FALSE) { if ($response === FALSE) {
throw new Exception('POST curl failed'); 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; return $response;
} }