1
1
<?php
2
2
3
3
/**
4
- * Discourse API client for PHP
5
- *
6
- * This is the Discourse API client for PHP
7
- * This is a very experimental API implementation.
8
- *
9
- * @category DiscourseAPI
10
- * @package DiscourseAPI
11
- * @author Original author DiscourseHosting <[email protected] >
12
- * @copyright 2013, DiscourseHosting.com
13
- * @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
14
- * @link https://github.com/discoursehosting/discourse-api-php
15
- */
4
+ * Discourse API client for PHP
5
+ *
6
+ * This is the Discourse API client for PHP
7
+ * This is a very experimental API implementation.
8
+ *
9
+ * @category DiscourseAPI
10
+ * @package DiscourseAPI
11
+ * @author Original author DiscourseHosting <[email protected] >
12
+ * @copyright 2013, DiscourseHosting.com
13
+ * @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
14
+ * @link https://github.com/discoursehosting/discourse-api-php
15
+ */
16
16
17
17
class DiscourseAPI
18
18
{
@@ -22,11 +22,11 @@ class DiscourseAPI
22
22
private $ _httpAuthName = '' ;
23
23
private $ _httpAuthPass = '' ;
24
24
25
- function __construct ($ dcHostname , $ apiKey = null , $ protocol= 'http ' , $ httpAuthName= '' , $ httpAuthPass= '' )
25
+ function __construct ($ dcHostname , $ apiKey = null , $ protocol = 'http ' , $ httpAuthName = '' , $ httpAuthPass = '' )
26
26
{
27
27
$ this ->_dcHostname = $ dcHostname ;
28
28
$ this ->_apiKey = $ apiKey ;
29
- $ this ->_protocol = $ protocol ;
29
+ $ this ->_protocol = $ protocol ;
30
30
$ this ->_httpAuthName = $ httpAuthName ;
31
31
$ this ->_httpAuthPass = $ httpAuthPass ;
32
32
}
@@ -39,18 +39,18 @@ private function _getRequest($reqString, $paramArray = null, $apiUser = 'system'
39
39
$ ch = curl_init ();
40
40
$ url = sprintf (
41
41
'%s://%s%s?%s ' ,
42
- $ this ->_protocol ,
43
- $ this ->_dcHostname ,
44
- $ reqString ,
42
+ $ this ->_protocol ,
43
+ $ this ->_dcHostname ,
44
+ $ reqString ,
45
45
http_build_query ($ paramArray )
46
46
);
47
47
48
- curl_setopt ($ ch , CURLOPT_HTTPHEADER , [
48
+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , [
49
49
"Api-Key: " . $ this ->_apiKey ,
50
50
"Api-Username: $ apiUser "
51
51
]);
52
-
53
- if (!empty ($ this ->_httpAuthName ) && !empty ($ this ->_httpAuthPass )) {
52
+
53
+ if (!empty ($ this ->_httpAuthName ) && !empty ($ this ->_httpAuthPass )) {
54
54
curl_setopt ($ ch , CURLOPT_USERPWD , $ this ->_httpAuthName . ": " . $ this ->_httpAuthPass );
55
55
curl_setopt ($ ch , CURLOPT_HTTPAUTH , CURLAUTH_BASIC );
56
56
}
@@ -74,28 +74,32 @@ private function _putRequest($reqString, $paramArray, $apiUser = 'system')
74
74
75
75
private function _postRequest ($ reqString , $ paramArray , $ apiUser = 'system ' )
76
76
{
77
+
77
78
return $ this ->_putpostRequest ($ reqString , $ paramArray , $ apiUser , false );
78
79
}
79
80
80
81
private function _putpostRequest ($ reqString , $ paramArray , $ apiUser = 'system ' , $ putMethod = false )
81
82
{
83
+
82
84
$ ch = curl_init ();
83
85
$ url = sprintf (
84
86
'%s://%s%s?api_key=%s&api_username=%s ' ,
85
- $ this ->_protocol ,
86
- $ this ->_dcHostname ,
87
- $ reqString ,
88
- $ this ->_apiKey ,
87
+ $ this ->_protocol ,
88
+ $ this ->_dcHostname ,
89
+ $ reqString ,
90
+ $ this ->_apiKey ,
89
91
$ apiUser
90
92
);
93
+
94
+
91
95
curl_setopt ($ ch , CURLOPT_URL , $ url );
92
96
curl_setopt ($ ch , CURLOPT_POSTFIELDS , http_build_query ($ paramArray ));
93
97
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
94
98
if ($ putMethod ) {
95
99
curl_setopt ($ ch , CURLOPT_CUSTOMREQUEST , "PUT " );
96
100
}
97
-
98
- if (!empty ($ this ->_httpAuthName ) && !empty ($ this ->_httpAuthPass )) {
101
+
102
+ if (!empty ($ this ->_httpAuthName ) && !empty ($ this ->_httpAuthPass )) {
99
103
curl_setopt ($ ch , CURLOPT_USERPWD , $ this ->_httpAuthName . ": " . $ this ->_httpAuthPass );
100
104
curl_setopt ($ ch , CURLOPT_HTTPAUTH , CURLAUTH_BASIC );
101
105
}
@@ -126,8 +130,8 @@ function group($groupname, $usernames = array())
126
130
return false ;
127
131
}
128
132
129
- foreach ($ obj ->apiresult as $ group ) {
130
- if ($ group ->name === $ groupname ) {
133
+ foreach ($ obj ->apiresult as $ group ) {
134
+ if ($ group ->name === $ groupname ) {
131
135
$ groupId = $ group ->id ;
132
136
break ;
133
137
}
@@ -141,7 +145,7 @@ function group($groupname, $usernames = array())
141
145
)
142
146
);
143
147
144
- if ($ groupId ) {
148
+ if ($ groupId ) {
145
149
return $ this ->_putRequest ('/admin/groups/ ' . $ groupId , $ params );
146
150
} else {
147
151
return $ this ->_postRequest ('/admin/groups ' , $ params );
@@ -214,19 +218,19 @@ function activateUser($userId)
214
218
return $ this ->_putRequest ("/admin/users/ {$ userId }/activate " , array ());
215
219
}
216
220
217
- /**
218
- * suspendUser
219
- *
220
- * @param integer $userId id of user to suspend
221
- *
222
- * @return mixed HTTP return code
223
- */
221
+ /**
222
+ * suspendUser
223
+ *
224
+ * @param integer $userId id of user to suspend
225
+ *
226
+ * @return mixed HTTP return code
227
+ */
224
228
225
229
function suspendUser ($ userId )
226
230
{
227
231
return $ this ->_putRequest ("/admin/users/ {$ userId }/suspend " , array ());
228
232
}
229
-
233
+
230
234
/**
231
235
* getUsernameByEmail
232
236
*
@@ -237,19 +241,20 @@ function suspendUser($userId)
237
241
238
242
function getUsernameByEmail ($ email )
239
243
{
240
- $ users = $ this ->_getRequest ('/admin/users/list/active.json ' ,
241
- [ 'filter ' => $ email , 'show_emails ' => 'true ' ]
244
+ $ users = $ this ->_getRequest (
245
+ '/admin/users/list/active.json ' ,
246
+ ['filter ' => $ email , 'show_emails ' => 'true ' ]
242
247
);
243
- foreach ($ users ->apiresult as $ user ) {
244
- if ($ user ->email === $ email ) {
248
+ foreach ($ users ->apiresult as $ user ) {
249
+ if ($ user ->email === $ email ) {
245
250
return $ user ->username ;
246
251
}
247
252
}
248
-
253
+
249
254
return false ;
250
255
}
251
256
252
- /**
257
+ /**
253
258
* getUserByUsername
254
259
*
255
260
* @param string $userName username of user
@@ -261,18 +266,18 @@ function getUserByUsername($userName)
261
266
{
262
267
return $ this ->_getRequest ("/users/ {$ userName }.json " );
263
268
}
264
-
269
+
265
270
/**
266
- * getUserByExternalID
267
- *
268
- * @param string $externalID external id of sso user
269
- *
270
- * @return mixed HTTP return code and API return object
271
- */
272
- function getUserByExternalID ($ externalID )
273
- {
274
- return $ this ->_getRequest ("/users/by-external/ {$ externalID }.json " );
275
- }
271
+ * getUserByExternalID
272
+ *
273
+ * @param string $externalID external id of sso user
274
+ *
275
+ * @return mixed HTTP return code and API return object
276
+ */
277
+ function getUserByExternalID ($ externalID )
278
+ {
279
+ return $ this ->_getRequest ("/users/by-external/ {$ externalID }.json " );
280
+ }
276
281
277
282
/**
278
283
* createCategory
@@ -307,7 +312,7 @@ function createCategory($categoryName, $color, $textColor = '000000', $userName
307
312
* @return mixed HTTP return code and API return object
308
313
*/
309
314
310
- function createTopic ($ topicTitle , $ bodyText , $ categoryId , $ userName , $ replyToId = 0 )
315
+ function createTopic ($ topicTitle , $ bodyText , $ categoryId , $ userName , $ replyToId = 0 )
311
316
{
312
317
$ params = array (
313
318
'title ' => $ topicTitle ,
@@ -318,7 +323,7 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId
318
323
);
319
324
return $ this ->_postRequest ('/posts ' , $ params , $ userName );
320
325
}
321
-
326
+
322
327
/**
323
328
* watchTopic
324
329
*
@@ -327,26 +332,28 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId
327
332
* If no username is given, topic will be watched with
328
333
* the system API username
329
334
*/
330
- function watchTopic ($ topicId , $ userName = 'system ' )
331
- {
335
+ function watchTopic ($ topicId , $ userName = 'system ' )
336
+ {
332
337
$ params = array (
333
- 'notification_level ' => '3 '
338
+ 'notification_level ' => '3 '
334
339
);
335
340
return $ this ->_postRequest ("/t/ {$ topicId }/notifications.json " , $ params , $ userName );
336
- }
341
+ }
337
342
338
343
/**
339
344
* createPost
340
345
*
341
- * NOT WORKING YET
346
+ * @param string $bodyText body text of topic post
347
+ * @param string $topicId topic id - must me a string not array
348
+ * @param string $userName user to create topic as
349
+ *
350
+ * @return mixed HTTP return code and API return object
342
351
*/
343
352
344
- function createPost ($ bodyText , $ topicId , $ categoryId , $ userName )
353
+ function createPost ($ bodyText , $ topicId , $ userName )
345
354
{
346
355
$ params = array (
347
356
'raw ' => $ bodyText ,
348
- 'archetype ' => 'regular ' ,
349
- 'category ' => $ categoryId ,
350
357
'topic_id ' => $ topicId
351
358
);
352
359
return $ this ->_postRequest ('/posts ' , $ params , $ userName );
@@ -358,15 +365,15 @@ function inviteUser($email, $topicId, $userName = 'system')
358
365
'email ' => $ email ,
359
366
'topic_id ' => $ topicId
360
367
);
361
- return $ this ->_postRequest ('/t/ ' . intval ($ topicId ). '/invite.json ' , $ params , $ userName );
368
+ return $ this ->_postRequest ('/t/ ' . intval ($ topicId ) . '/invite.json ' , $ params , $ userName );
362
369
}
363
370
364
371
function changeSiteSetting ($ siteSetting , $ value )
365
372
{
366
373
$ params = array ($ siteSetting => $ value );
367
374
return $ this ->_putRequest ('/admin/site_settings/ ' . $ siteSetting , $ params );
368
375
}
369
-
376
+
370
377
function getIDByEmail ($ email )
371
378
{
372
379
$ username = $ this ->getUsernameByEmail ($ email );
@@ -383,10 +390,9 @@ function logoutByEmail($email)
383
390
$ params = array ('username_or_email ' => $ email );
384
391
return $ this ->_postRequest ('/admin/users/ ' . $ user_id . '/log_out ' , $ params );
385
392
}
386
-
387
- function getUserinfoByName ($ username )
393
+
394
+ function getUserinfoByName ($ username )
388
395
{
389
396
return $ this ->_getRequest ("/users/ {$ username }.json " );
390
397
}
391
398
}
392
-
0 commit comments