Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
fusiondirectory
fusiondirectory-integrator
Commits
b06bea04
Verified
Commit
b06bea04
authored
1 month ago
by
dockx thibault
Browse files
Options
Download
Patches
Plain Diff
Feat(webservice) - add HTTP status code handling and support for empty POST data
parent
0cc8c68b
dev
1 merge request
!55
Resolve "[Integrator] - Rest library - update to allow easy trigger of archiving objects"
Pipeline
#32403
failed with stages
in 19 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/FusionDirectory/Rest/WebServiceCall.php
+30
-4
src/FusionDirectory/Rest/WebServiceCall.php
with
30 additions
and
4 deletions
+30
-4
src/FusionDirectory/Rest/WebServiceCall.php
+
30
−
4
View file @
b06bea04
...
@@ -10,6 +10,8 @@ class WebServiceCall
...
@@ -10,6 +10,8 @@ class WebServiceCall
*/
*/
private
$ch
;
private
$ch
;
private
$httpStatusCode
;
// Store the HTTP status code
/**
/**
* @param string $URL
* @param string $URL
* @param string $method
* @param string $method
...
@@ -70,7 +72,12 @@ class WebServiceCall
...
@@ -70,7 +72,12 @@ class WebServiceCall
break
;
break
;
case
'post'
:
case
'post'
:
curl_setopt
(
$this
->
ch
,
CURLOPT_POST
,
TRUE
);
curl_setopt
(
$this
->
ch
,
CURLOPT_POST
,
TRUE
);
curl_setopt
(
$this
->
ch
,
CURLOPT_POSTFIELDS
,
json_encode
(
$this
->
data
));
// Allows a POST to be performed without DATA
if
(
!
empty
(
$this
->
data
))
{
curl_setopt
(
$this
->
ch
,
CURLOPT_POSTFIELDS
,
json_encode
(
$this
->
data
));
}
curl_setopt
(
$this
->
ch
,
CURLOPT_USERAGENT
,
$customUserAgent
);
curl_setopt
(
$this
->
ch
,
CURLOPT_USERAGENT
,
$customUserAgent
);
break
;
break
;
}
}
...
@@ -206,17 +213,36 @@ class WebServiceCall
...
@@ -206,17 +213,36 @@ class WebServiceCall
public
function
execute
():
array
public
function
execute
():
array
{
{
$response
=
curl_exec
(
$this
->
ch
);
$response
=
curl_exec
(
$this
->
ch
);
// Capture the HTTP status code
$this
->
httpStatusCode
=
curl_getinfo
(
$this
->
ch
,
CURLINFO_HTTP_CODE
);
$this
->
handleCurlError
(
$this
->
ch
);
$this
->
handleCurlError
(
$this
->
ch
);
// Handle 204 No Content response
if
(
$this
->
httpStatusCode
===
204
)
{
curl_close
(
$this
->
ch
);
return
[];
// Return an empty array for 204 responses
}
$decoded
=
json_decode
(
$response
,
true
);
$decoded
=
json_decode
(
$response
,
true
);
curl_close
(
$this
->
ch
);
curl_close
(
$this
->
ch
);
if
(
!
is_array
(
$decoded
))
{
if
(
!
is_array
(
$decoded
))
{
throw
new
\
Exception
(
'Invalid JSON response: '
.
$response
);
throw
new
\
Exception
(
'Invalid JSON response: '
.
$response
);
}
}
return
$decoded
;
return
$decoded
;
}
}
/**
* Retrieve the HTTP status code of the last request
* @return int
*/
public
function
getHttpStatusCode
():
int
{
return
$this
->
httpStatusCode
;
}
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets