Thursday, August 24, 2017

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);
view raw okta_curl.php hosted with ❤ by GitHub

1 comments :

  1. Your blog post is really very informative & interesting. Thanks for sharing. For more updates on online shopping Coupons & discount Codes please visit Sammydress Discount Coupons

    ReplyDelete