Last active
March 9, 2024 20:34
-
-
Save woganmay/4c15a0f7c16e41ab3a3ea1a73c595bf9 to your computer and use it in GitHub Desktop.
Use the Flarum API to create a new user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$api_url = "https://my.flarum.url/api/users"; | |
$token = $session->token; // See: https://gist.github.com/woganmay/88f15e96fc019657a0e594366403b5cf | |
// This must be a token for a user with Administrator access | |
$new_username = "johnsmith"; | |
$new_password = "password1234"; | |
$new_email = "[email protected]"; | |
$ch = curl_init($api_url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
'Content-Type: application/json', | |
'Authorization: Token ' . $token | |
]); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ | |
'data' => [ | |
'attributes' => [ | |
"username" => $new_username, | |
"password" => $new_password, | |
"email" => $new_email | |
] | |
] | |
])); | |
$result = curl_exec($ch); | |
$new_user = json_decode($result); | |
$new_user; // Will be a large JSON object containing the new user's details: | |
/* SAMPLE | |
{ | |
"data": | |
"type": "users", | |
"id": "1", | |
"attributes": { | |
"username": "johnsmith", | |
"joinTime": "2017-02-11T16:34:40+00:00", | |
"isActivated": false, | |
"email": "[email protected]", | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Its Done || Thanks :)
`<?php
//$userId = 2;
$some_data = array(
'identification' => 'divyesh',
'password' => 'divyesh12'
);
$some_data_json = json_encode($some_data, true);
$apikey = 'YACub2KLfe8hjfIUcUKtt6t2SMKJUIXnZbqhc3nX';
$curl = curl_init();
// You can also set the URL you want to communicate with by doing this:
// $curl = curl_init('http://localhost/echoservice');
// We POST the data
curl_setopt($curl, CURLOPT_POST, 1);
// Set the url path we want to call
curl_setopt($curl, CURLOPT_URL, 'http://academy.forumias.com/discussion/api/token');
// Make it so the data coming back is put into a string
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Insert the data
curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data_json);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($some_data_json),
'Authorization: Token ' . $apikey,
]
);
// You can also bunch the above commands into an array if you choose using: curl_setopt_array
// Send the request
$result = curl_exec($curl);
// Free up the resources $curl is using
curl_close($curl);
// $result = json_decode($result, true);
//$student = $result['data'];
//print_r($result);
$response = json_decode($result,true);
$token = $response['token'];
if(isset($token)) {
$expire = time() + (86400 * 30);
setcookie('flarum_remember', $token , $expire, "/");
header("Location: http://academy.forumias.com/discussion/");
}
?>`