Skip to content

Instantly share code, notes, and snippets.

@bhu1st
Last active August 24, 2017 17:10
Show Gist options
  • Save bhu1st/0472c7348f56d746902b044444b909be to your computer and use it in GitHub Desktop.
Save bhu1st/0472c7348f56d746902b044444b909be to your computer and use it in GitHub Desktop.
OKTA API Call with PHP Curl
/**********************************
Function for OKTA REST API CALL
***********************************/
function Okta ($url, $method = "GET", $data = "") {
$apiKey = "your key";
$baseUrl = "https://dev-123456.oktapreview.com";
$headers = array(
'Authorization: SSWS ' . $apiKey,
'Accept: application/json',
'Content-Type: application/json'
);
$curl_url = $baseUrl. $url;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $curl_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
if ($method == "POST") {
curl_setopt($curl, CURLOPT_POST, 1);
}
if ($method == "GET") {
curl_setopt($curl, CURLOPT_HTTPGET, 1);
}
if (!empty($data)) {
//curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
}
//$output = curl_exec($curl);
if (($output = curl_exec($curl)) === FALSE) {
die("Curl Failed: " . curl_error($curl));
}
//curl_getinfo($curl);
curl_close($curl);
return json_decode($output);
}
//Sample call:
$data = array(
'username' => 'johndoe',
'password' => 'sanfrancisco'
);
$result = Okta ("/api/v1/authn", "POST", $data);
print_r($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment