I'm implementing oauth2 authorisation for our application, using keycloak to authenticate users. I successfully obtain a 'code' from keycloak. I submit this back to keycloak's rest api, to obtain a token for the user, using curl in a PHP module. Then I receive an error 'couldn't connect to server', however I have firefox's developer tools active and this shows a successful connection with an access_token returned (as does the keycloak log).
$ch = curl_init("http://localhost:8080/realms/openemr-local/protocol/openid-connect/token");
$cparams = ['code' => $kc_code, 'grant_type'=>'authorization_code', 'redirect_uri'=>'http://localhost:8300/interface/login/login.php?kc_response=true', 'client_id'=>'openemr'];
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $cparams);
$ch_response = curl_exec($ch);
if( curl_error($ch)) {
echo '<br>curl error: - ' . curl_error($ch);
echo ' and returned: ' . $ch_response;
}
else {
echo '<br>got token response: ' . $ch_response;
$json_text = json_decode($ch_response);
echo $json_text->contents . '<br>';
}
curl_error is true and gives:
Failed to connect to localhost port 8080 after 0 ms: Couldn't connect to server and returned:
However when I use firefox's developer tools it shows that there was a response from this curl connection, with the data i need. Looking at the keycloak log the call was seen and answered as well.
So my question is:
how can i get php curl function to indicate there was no error and
return the correct data