forked from richp10/discourse-api-php
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample.php
59 lines (44 loc) · 1.52 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
use pnoeric\DiscourseAPI;
// ideally you have a composer auto-loader in place, but if not, you can just require the library:
// require_once 'lib/DiscourseAPI.php';
// note that you need to specify the URL of your Discourse install, and
// for API_KEY you'll need your key from Discourse
$api = new DiscourseAPI( DISCOURSE_URL, API_KEY, 'https' );
// create user
$r = $api->createUser( 'John Doe', 'johndoe', '[email protected]', 'foobar!!' );
print_r( $r );
// in order to activate we need the id
$r = $api->getUserByUsername( 'johndoe' );
print_r( $r );
// activate the user
$r = $api->activateUser( $r->apiresult->user->id );
print_r( $r );
// create a category
$r = $api->createCategory( 'a new category', 'cc2222' );
print_r( $r );
$catId = $r->apiresult->category->id;
// create a topic in a category
$r = $api->createTopic(
'This is the title of a brand new topic',
"This is the body text of a brand new topic. What else is there to say? Enjoy the topic! Hurrah!",
$catId,
'johndoe'
);
print_r( $r );
$topicId = $r->apiresult->id;
// create a post in a topic
$now = new DateTime();
$r = $api->createPost(
'This is the body of a new post in an existing topic',
$topicId,
'johndoe',
$now
);
// change site setting (these are admin-only settings, NOT user preferences)
// use 'true' and 'false' between quotes
$r = $api->changeSiteSetting( 'invite_expiry_days', 29 );
print_r( $r );
// log off user
$api->logoutUserByUsername( 'johndoe' );
// and there are many, many more methods in the API... check 'em out!