Skip to content

Commit

Permalink
Add makeAPIRequest method
Browse files Browse the repository at this point in the history
It will be used to make requests
to the new API
  • Loading branch information
anarute committed Jul 21, 2023
1 parent 6a58289 commit 6716ad9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ DB_HOST=localhost
DB_PORT=5432
DAO_BACKEND=PostgreSQL
EXTRA_DB_CONNECTION_PARAMETERS=
API_BASE_URL="http://localhost:8555"

LDAP_SERVER="localhost"
LDAP_PORT=389
Expand Down
38 changes: 38 additions & 0 deletions web/services/WebServicesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* along with PhpReport. If not, see <http://www.gnu.org/licenses/>.
*/

require_once(PHPREPORT_ROOT . '/util/ConfigurationParametersManager.php');

function escape_string($string)
{
Expand Down Expand Up @@ -46,3 +47,40 @@ function authenticate($login, $sid = NULL)
return $_SESSION['user'];

}

function makeAPIRequest($path, $params = null, $method = 'GET', $data = null)
{
$requestUri = ConfigurationParametersManager::getParameter('API_BASE_URL') . $path;
$api_token = $_SESSION['api_token'];

if ($params) {
$requestUri .= '?' . http_build_query($params);
}
$ch = curl_init();
$headers = array(
"Content-type: application/json",
"Authorization: Bearer " . $api_token,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $requestUri);
curl_setopt($ch, CURLOPT_SSH_COMPRESSION, true);

if ($method == 'POST') {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}

curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $requestUri
]);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}

curl_close($ch);
return json_decode($result);
}

0 comments on commit 6716ad9

Please sign in to comment.