Skip to content

Commit 8afe946

Browse files
committed
Work through the createPost request to get it working
1 parent fbc5ee4 commit 8afe946

File tree

2 files changed

+77
-74
lines changed

2 files changed

+77
-74
lines changed

example.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
// create a topic
2626
$r = $api->createTopic(
27-
'This is the title of a brand new topic',
27+
'This is the title of a brand new topic',
2828
"This is the body text of a brand new topic. I really don't know what to say",
2929
$catId,
3030
"johndoe"
@@ -36,14 +36,11 @@
3636
$r = $api->createPost(
3737
'This is the body of a new post in an existing topic',
3838
$topicId,
39-
$catId,
4039
'johndoe'
4140
);
42-
41+
4342
// change sitesetting
4443
// use 'true' and 'false' between quotes
4544

4645
$r = $api->changeSiteSetting('invite_expiry_days', 29);
4746
print_r($r);
48-
49-

lib/DiscourseAPI.php

Lines changed: 75 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?php
22

33
/**
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+
*/
1616

1717
class DiscourseAPI
1818
{
@@ -22,11 +22,11 @@ class DiscourseAPI
2222
private $_httpAuthName = '';
2323
private $_httpAuthPass = '';
2424

25-
function __construct($dcHostname, $apiKey = null, $protocol='http', $httpAuthName='', $httpAuthPass='')
25+
function __construct($dcHostname, $apiKey = null, $protocol = 'http', $httpAuthName = '', $httpAuthPass = '')
2626
{
2727
$this->_dcHostname = $dcHostname;
2828
$this->_apiKey = $apiKey;
29-
$this->_protocol=$protocol;
29+
$this->_protocol = $protocol;
3030
$this->_httpAuthName = $httpAuthName;
3131
$this->_httpAuthPass = $httpAuthPass;
3232
}
@@ -39,18 +39,18 @@ private function _getRequest($reqString, $paramArray = null, $apiUser = 'system'
3939
$ch = curl_init();
4040
$url = sprintf(
4141
'%s://%s%s?%s',
42-
$this->_protocol,
43-
$this->_dcHostname,
44-
$reqString,
42+
$this->_protocol,
43+
$this->_dcHostname,
44+
$reqString,
4545
http_build_query($paramArray)
4646
);
4747

48-
curl_setopt($ch, CURLOPT_HTTPHEADER, [
48+
curl_setopt($ch, CURLOPT_HTTPHEADER, [
4949
"Api-Key: " . $this->_apiKey,
5050
"Api-Username: $apiUser"
5151
]);
52-
53-
if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) {
52+
53+
if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) {
5454
curl_setopt($ch, CURLOPT_USERPWD, $this->_httpAuthName . ":" . $this->_httpAuthPass);
5555
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
5656
}
@@ -74,28 +74,32 @@ private function _putRequest($reqString, $paramArray, $apiUser = 'system')
7474

7575
private function _postRequest($reqString, $paramArray, $apiUser = 'system')
7676
{
77+
7778
return $this->_putpostRequest($reqString, $paramArray, $apiUser, false);
7879
}
7980

8081
private function _putpostRequest($reqString, $paramArray, $apiUser = 'system', $putMethod = false)
8182
{
83+
8284
$ch = curl_init();
8385
$url = sprintf(
8486
'%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,
8991
$apiUser
9092
);
93+
94+
9195
curl_setopt($ch, CURLOPT_URL, $url);
9296
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($paramArray));
9397
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
9498
if ($putMethod) {
9599
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
96100
}
97-
98-
if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) {
101+
102+
if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) {
99103
curl_setopt($ch, CURLOPT_USERPWD, $this->_httpAuthName . ":" . $this->_httpAuthPass);
100104
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
101105
}
@@ -126,8 +130,8 @@ function group($groupname, $usernames = array())
126130
return false;
127131
}
128132

129-
foreach($obj->apiresult as $group) {
130-
if($group->name === $groupname) {
133+
foreach ($obj->apiresult as $group) {
134+
if ($group->name === $groupname) {
131135
$groupId = $group->id;
132136
break;
133137
}
@@ -141,7 +145,7 @@ function group($groupname, $usernames = array())
141145
)
142146
);
143147

144-
if($groupId) {
148+
if ($groupId) {
145149
return $this->_putRequest('/admin/groups/' . $groupId, $params);
146150
} else {
147151
return $this->_postRequest('/admin/groups', $params);
@@ -214,19 +218,19 @@ function activateUser($userId)
214218
return $this->_putRequest("/admin/users/{$userId}/activate", array());
215219
}
216220

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+
*/
224228

225229
function suspendUser($userId)
226230
{
227231
return $this->_putRequest("/admin/users/{$userId}/suspend", array());
228232
}
229-
233+
230234
/**
231235
* getUsernameByEmail
232236
*
@@ -237,19 +241,20 @@ function suspendUser($userId)
237241

238242
function getUsernameByEmail($email)
239243
{
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']
242247
);
243-
foreach($users->apiresult as $user) {
244-
if($user->email === $email) {
248+
foreach ($users->apiresult as $user) {
249+
if ($user->email === $email) {
245250
return $user->username;
246251
}
247252
}
248-
253+
249254
return false;
250255
}
251256

252-
/**
257+
/**
253258
* getUserByUsername
254259
*
255260
* @param string $userName username of user
@@ -261,18 +266,18 @@ function getUserByUsername($userName)
261266
{
262267
return $this->_getRequest("/users/{$userName}.json");
263268
}
264-
269+
265270
/**
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+
}
276281

277282
/**
278283
* createCategory
@@ -307,7 +312,7 @@ function createCategory($categoryName, $color, $textColor = '000000', $userName
307312
* @return mixed HTTP return code and API return object
308313
*/
309314

310-
function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId = 0)
315+
function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId = 0)
311316
{
312317
$params = array(
313318
'title' => $topicTitle,
@@ -318,7 +323,7 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId
318323
);
319324
return $this->_postRequest('/posts', $params, $userName);
320325
}
321-
326+
322327
/**
323328
* watchTopic
324329
*
@@ -327,26 +332,28 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId
327332
* If no username is given, topic will be watched with
328333
* the system API username
329334
*/
330-
function watchTopic($topicId, $userName = 'system')
331-
{
335+
function watchTopic($topicId, $userName = 'system')
336+
{
332337
$params = array(
333-
'notification_level' => '3'
338+
'notification_level' => '3'
334339
);
335340
return $this->_postRequest("/t/{$topicId}/notifications.json", $params, $userName);
336-
}
341+
}
337342

338343
/**
339344
* createPost
340345
*
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
342351
*/
343352

344-
function createPost($bodyText, $topicId, $categoryId, $userName)
353+
function createPost($bodyText, $topicId, $userName)
345354
{
346355
$params = array(
347356
'raw' => $bodyText,
348-
'archetype' => 'regular',
349-
'category' => $categoryId,
350357
'topic_id' => $topicId
351358
);
352359
return $this->_postRequest('/posts', $params, $userName);
@@ -358,15 +365,15 @@ function inviteUser($email, $topicId, $userName = 'system')
358365
'email' => $email,
359366
'topic_id' => $topicId
360367
);
361-
return $this->_postRequest('/t/'.intval($topicId).'/invite.json', $params, $userName);
368+
return $this->_postRequest('/t/' . intval($topicId) . '/invite.json', $params, $userName);
362369
}
363370

364371
function changeSiteSetting($siteSetting, $value)
365372
{
366373
$params = array($siteSetting => $value);
367374
return $this->_putRequest('/admin/site_settings/' . $siteSetting, $params);
368375
}
369-
376+
370377
function getIDByEmail($email)
371378
{
372379
$username = $this->getUsernameByEmail($email);
@@ -383,10 +390,9 @@ function logoutByEmail($email)
383390
$params = array('username_or_email' => $email);
384391
return $this->_postRequest('/admin/users/' . $user_id . '/log_out', $params);
385392
}
386-
387-
function getUserinfoByName($username)
393+
394+
function getUserinfoByName($username)
388395
{
389396
return $this->_getRequest("/users/{$username}.json");
390397
}
391398
}
392-

0 commit comments

Comments
 (0)