I've set a global variable to equal an access token, and when I print_($session_id) the token is visible. What I'd like to do is pass that variable into my CURLOPT_HTTPHEADER, and have it equal to "Session" in the function get_field_data_id() below:
function get_session_id() {
global $session_id;
$curl = curl_init();
$session_url = "/auth";
curl_setopt_array($curl, array(
CURLOPT_URL => $session_url ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Accept: application/json",
"username: USER",
"password: PASS",
"authorizationkey: KEY"
),
));
$response = curl_exec($curl);
curl_close($curl);
$session_id = $response;
} add_filter( 'gform_after_submission_4', 'get_session_id' );
function get_field_data_id() {
global $session_id;
$curl = curl_init();
$file_url = "URL";
curl_setopt_array($curl, array(
CURLOPT_URL => $file_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Accept: application/json",
"Session: {$session_id}"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;}
However when I run this function, I get an error message that the session ID is invalid. What am I missing?
{"Message":"Invalid Authorization. Session ID is invalid. "}
Related
I'm using php curl API to fetch the posts from WordPress. But I'm not getting X-WP-Total or X-WP-TotalPages keys with response to work with pagination.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/wp-json/wp/v2/posts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'Accept: application/json',
"Content-Type: application/json",
"Access-Control-Allow-Origin: *",
"Access-Control-Allow-Methods: GET, POST, OPTIONS",
"Access-Control-Allow-Headers: X-Requested-With"
],
));
$response = curl_exec($curl);
$response = json_decode($response);
// print_r($response);exit;
curl_close($curl);
I am trying to pass the Variable into Curl But It's Not Working as I am trying to Call JSON API I Tried all The Possible Ways But Ntg Worked.
$name = "SAI";
$amount = "42000";
$user = "1643031393";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.interakt.ai/v1/public/track/events/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"{\"userId\":\"1643031393\",\"event\":\"SEVA BILL GENRATED\",\"traits\": {\"donated\":\"YES\",\"seva_name\":\"$name\",\"seva_amount\":\"$amount\",\"Seva Date\":\"12-22-2022\",\"E-Receipt URL\":\"https://ack.saaa.org"},\"createdAt\": \"2022-01-24T13:26:52.926Z\"}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Basic gfdsds"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;````
The value of CURLOPT_POSTFIELD option must be JSON format!
Like this:
$data = json_encode(array(
"name" => "Steve",
"amount" => 12345,
"user" => 123456789
));
$curl = curl_init();
curl_setopt_array($curl, array(
...
CURLOPT_POSTFIELDS => $data,
...
));
I am trying to perform some function based on the response I will get from Curl URL. Below is the code I have written. But the response coming is always empty. Due to which the control always goes into an else condition.
if(isset($_POST['verify_button'])){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://*******/v1/****/*********/".$_POST['********']."?MVNO=*******",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic Z*******",
"accept: application/json"
),
));
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response);
if($data->resultCode == '0'){
echo '<div class="mm_success_alert">Sucess</div>';
}
else{
echo '<div class="mm_danger_alert">Fail.</div>';
}
How I can create a user by Client secrets in Azure AD with PHP?
I need access token in below code to create a user. To have this token I need to login first. How I can create a user automatically without any login.
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://graph.microsoft.com/v1.0/users',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"accountEnabled": true,
"displayName": "Adele Vance",
"userPrincipalName": "adelev2#xxx.net",
"passwordProfile" : {
"forceChangePasswordNextSignIn": true,
"password": "xWwvJ]6NMw+bWH-d"
}
}',
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $accessToken",
"Content-Type: application/json"
),
));
You can refer to this sample, which uses a daemon that does not require user login, and uses the client credential flow to obtain an access token to call MS graph api to create a user. You need to grant User.ReadWrite.All application permissions for the application.
With special thanks to Carl which provide useful links I did it by using two below functions:
I receive a token by calling getToken function and use it in getToken to create a user without any previous login.
function getToken() {
$curl = curl_init();
$dir = env('OAUTH_DIR_ID');
$clientId = env('OAUTH_APP_ID');
$secretKey = env('OAUTH_APP_PASSWORD');
curl_setopt_array($curl, array(
CURLOPT_URL => "https://login.microsoftonline.com/$dir/oauth2/v2.0/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => "client_id=$clientId&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=$secretKey&grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'x-ms-gateway-slice=estsfd; stsservicecookie=estsfd'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
function addUser($accessToken)
{
try {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://graph.microsoft.com/v1.0/users',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"accountEnabled": true,
"displayName": "Adele Vance",
"userPrincipalName": "adelev2#yoed.net",
"passwordProfile" : {
"forceChangePasswordNextSignIn": true,
"password": "xWwvJ]6NMw+bWH-d"
}
}',
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $accessToken",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
curl_close($curl);
var_dump($response); // Debug print
exit();
} catch (Error $ex) {
$home = env('APP_URL');
header("Location: $home/signin.php?err=" . $ex->getMessage());
die();
}
}
Right now i am able to get the data from api by using below code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://test-api.test.org/user/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer eyJ0eXAiOiJKV1QiI1NiJ9.eyJhdWQiOiIxIiwianRpIjoN2ZiQwiZXhwIjoxNTk0MjY5NjI"
),
));
$response = curl_exec($curl);
$data = json_decode($response, true);
echo $response;
But authorization code is expiring every 15 min and i have to refresh the token after that interval in php.
My requirement is:
To generate token dynamically by using jwt api, user and password
check for if the token is valid or expired after sometime
once the token is expired then refresh it and generate a new token dynamically.
Thanks in advance for all your support.
I found solution of my problem and its working perfectly fine.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://{instance}/app/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"username\":\"yourusername\", \"yourpassword\":\"{pass}\"}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}