Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use the Flarum API to create a new user
<?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 = "john.smith@example.org";
$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": "john.smith@example.org",
}
}
*/
@devjosh12

This comment has been minimized.

Copy link

@devjosh12 devjosh12 commented Jun 28, 2018

How to login from outside, please share code?

@devjosh12

This comment has been minimized.

Copy link

@devjosh12 devjosh12 commented Jun 28, 2018

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/");
}

?>`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment