Skip to content

Commit 1e0106b

Browse files
committed
Adjustments to make up for API changes
1 parent 3bfec1f commit 1e0106b

File tree

2 files changed

+135
-82
lines changed

2 files changed

+135
-82
lines changed

example.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
require_once "lib/DiscourseAPI.php";
4+
5+
$api = new DiscourseAPI("apitest.discoursehosting.net", "....");
6+
7+
// create user
8+
$r = $api->createUser('John Doe', 'johndoe', 'johndoe@discoursehosting.com', 'foobar!!');
9+
print_r($r);
10+
11+
// in order to activate we need the id
12+
$r = $api->getUserByUsername('johndoe');
13+
print_r($r);
14+
15+
// activate the user
16+
$r = $api->activateUser($r->apiresult->user->id);
17+
print_r($r);
18+
19+
// create a category
20+
$r = $api->createCategory('a new category', 'cc2222');
21+
print_r($r);
22+
23+
$catId = $r->apiresult->category->id;
24+
25+
// create a topic
26+
$r = $api->createTopic(
27+
'This is the title of a brand new topic',
28+
"This is the body text of a brand new topic. I really don't know what to say",
29+
$catId,
30+
"johndoe"
31+
);
32+
print_r($r);
33+
34+
$topicId = $r->apiresult->id;
35+
36+
$r = $api->createPost(
37+
'This is the body of a new post in an existing topic',
38+
$topicId,
39+
$catId,
40+
'johndoe'
41+
);
42+
43+

lib/DiscourseAPI.php

Lines changed: 92 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -16,58 +16,23 @@
1616

1717
class DiscourseAPI
1818
{
19-
private $_sessionKey = null;
2019
private $_protocol = 'http';
2120
private $_apiKey = null;
22-
private $_userName = null;
2321
private $_dcHostname = null;
2422

25-
function __construct($dcHostname, $apiKey = null, $userName = null)
23+
function __construct($dcHostname, $apiKey = null)
2624
{
2725
$this->_dcHostname = $dcHostname;
2826
$this->_apiKey = $apiKey;
29-
$this->_userName = $userName;
3027
}
3128

32-
private function _ensureSession($forceNew = false)
33-
{
34-
if (($forceNew == false) && ($this->_sessionKey != null)) {
35-
return true;
36-
}
37-
38-
$url = sprintf(
39-
'%s://%s?api_key=%s&api_username=%s',
40-
$this->_protocol, $this->_dcHostname, $this->_apiKey, $this->_userName
41-
);
42-
43-
$ch = curl_init();
44-
curl_setopt($ch, CURLOPT_URL, $url);
45-
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
46-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
47-
curl_setopt($ch, CURLOPT_VERBOSE, 1);
48-
curl_setopt($ch, CURLOPT_HEADER, 1);
49-
$out = curl_exec($ch);
50-
51-
$rc = curl_getinfo($ch, CURLINFO_HTTP_CODE);
52-
53-
$hs = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
54-
$out = substr($out, 0, $hs);
55-
preg_match('/^Set-Cookie:\s*([^;]*)/mi', $out, $m);
56-
parse_str($m[1], $cookies);
57-
$this->_sessionKey = $cookies['_forum_session'];
58-
59-
curl_close($ch);
60-
61-
return ($rc == 200);
62-
}
63-
64-
private function _getRequest($reqString, $paramArray = null)
29+
private function _getRequest($reqString, $paramArray = null, $apiUser = 'system')
6530
{
6631
if ($paramArray == null) {
6732
$paramArray = array();
6833
}
6934
$paramArray['api_key'] = $this->_apiKey;
70-
$paramArray['api_username'] = $this->_userName;
35+
$paramArray['api_username'] = $apiUser;
7136
$ch = curl_init();
7237
$url = sprintf(
7338
'%s://%s%s?%s',
@@ -76,22 +41,30 @@ private function _getRequest($reqString, $paramArray = null)
7641
$reqString,
7742
http_build_query($paramArray)
7843
);
44+
7945
curl_setopt($ch, CURLOPT_URL, $url);
80-
curl_setopt($ch, CURLOPT_COOKIE, '_forum_session='.$this->_sessionKey);
8146
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
8247
$body = curl_exec($ch);
8348
$rc = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8449
curl_close($ch);
8550

86-
return ($rc == 200) ? $body : false;
51+
$resObj = new \stdClass();
52+
$resObj->http_code = $rc;
53+
$resObj->apiresult = json_decode($body);
54+
return $resObj;
55+
}
56+
57+
private function _putRequest($reqString, $paramArray, $apiUser = 'system')
58+
{
59+
return $this->_putpostRequest($reqString, $paramArray, $apiUser, true);
8760
}
8861

89-
private function _putRequest($reqString, $paramArray)
62+
private function _postRequest($reqString, $paramArray, $apiUser = 'system')
9063
{
91-
return $this->_postRequest($reqString, $paramArray, true);
64+
return $this->_putpostRequest($reqString, $paramArray, $apiUser, false);
9265
}
9366

94-
private function _postRequest($reqString, $paramArray, $putMethod = false)
67+
private function _putpostRequest($reqString, $paramArray, $apiUser = 'system', $putMethod = false)
9568
{
9669
$ch = curl_init();
9770
$url = sprintf(
@@ -100,39 +73,22 @@ private function _postRequest($reqString, $paramArray, $putMethod = false)
10073
$this->_dcHostname,
10174
$reqString,
10275
$this->_apiKey,
103-
$this->_userName
76+
$apiUser
10477
);
10578
curl_setopt($ch, CURLOPT_URL, $url);
10679
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($paramArray));
107-
curl_setopt($ch, CURLOPT_COOKIE, '_forum_session='.$this->_sessionKey);
10880
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
10981
if ($putMethod) {
11082
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
11183
}
11284
$body = curl_exec($ch);
113-
11485
$rc = curl_getinfo($ch, CURLINFO_HTTP_CODE);
11586
curl_close($ch);
116-
return ($rc == 200) ? $body : false;
117-
}
11887

119-
/**
120-
* changeSiteSetting
121-
*
122-
* @param string $name name of site setting to be modified
123-
* @param string $value value of site setting to be modified
124-
*
125-
* @return mixed json or HTTP return code
126-
*/
127-
128-
function changeSiteSetting($name, $value)
129-
{
130-
$this->_ensureSession();
131-
$rc = $this->_putRequest(
132-
'/admin/site_settings/'.$name,
133-
array('value' => $value)
134-
);
135-
return $rc;
88+
$resObj = new \stdClass();
89+
$resObj->http_code = $rc;
90+
$resObj->apiresult = json_decode($body);
91+
return $resObj;
13692
}
13793

13894
/**
@@ -143,60 +99,114 @@ function changeSiteSetting($name, $value)
14399
* @param string $emailAddress email address of new user
144100
* @param string $password password of new user
145101
*
146-
* @return mixed json or HTTP return code
102+
* @return mixed HTTP return code and API return object
147103
*/
148104

149105
function createUser($name, $userName, $emailAddress, $password)
150106
{
151-
$json = $this->_getRequest('/users/hp.json');
152-
if ($json === false) {
107+
$obj = $this->_getRequest('/users/hp.json');
108+
if ($obj->http_code != 200) {
153109
return false;
154110
}
155111

156-
$obj = json_decode($json);
157-
158112
$params = array(
159113
'name' => $name,
160114
'username' => $userName,
161115
'email' => $emailAddress,
162116
'password' => $password,
163-
'challenge' => strrev($obj->challenge),
164-
'password_confirmation' => $obj->value
117+
'challenge' => strrev($obj->apiresult->challenge),
118+
'password_confirmation' => $obj->apiresult->value
165119
);
166120

167121
return $this->_postRequest('/users', $params);
168122
}
169123

170-
function createCategory($name, $color, $textColor = '000000')
124+
/**
125+
* activateUser
126+
*
127+
* @param integer $userId id of user to activate
128+
*
129+
* @return mixed HTTP return code
130+
*/
131+
132+
function activateUser($userId)
133+
{
134+
return $this->_putRequest("/admin/users/{$userId}/activate", array());
135+
}
136+
137+
/**
138+
* getUserByUsername
139+
*
140+
* @param string $userName username of user
141+
*
142+
* @return mixed HTTP return code and API return object
143+
*/
144+
145+
function getUserByUsername($userName)
146+
{
147+
return $this->_getRequest("/users/{$userName}.json");
148+
}
149+
150+
/**
151+
* createCategory
152+
*
153+
* @param string $categoryName name of new category
154+
* @param string $color color code of new category (six hex chars, no #)
155+
* @param string $textColor optional color code of text for new category
156+
* @param string $userName optional user to create category as
157+
*
158+
* @return mixed HTTP return code and API return object
159+
*/
160+
161+
function createCategory($categoryName, $color, $textColor = '000000', $userName = 'system')
171162
{
172163
$params = array(
173-
'name' => $name,
164+
'name' => $categoryName,
174165
'color' => $color,
175166
'text_color' => $textColor
176167
);
177-
return $this->_postRequest('/categories', $params);
168+
return $this->_postRequest('/categories', $params, $userName);
178169
}
179170

180-
function createTopic($title, $bodyText, $category, $replyTo = 0)
171+
/**
172+
* createTopic
173+
*
174+
* @param string $topicTitle title of topic
175+
* @param string $bodyText body text of topic post
176+
* @param string $categoryName category to create topic in
177+
* @param string $userName user to create topic as
178+
* @param string $replyToId post id to reply as
179+
*
180+
* @return mixed HTTP return code and API return object
181+
*/
182+
183+
function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId = 0)
181184
{
182185
$params = array(
183-
'title' => $title,
186+
'title' => $topicTitle,
184187
'raw' => $bodyText,
185-
'category' => $category,
188+
'category' => $categoryId,
186189
'archetype' => 'regular',
187-
'reply_to_post_number' => $replyTo,
190+
'reply_to_post_number' => $replyToId,
188191
);
189-
return $this->_postRequest('/posts', $params);
192+
return $this->_postRequest('/posts', $params, $userName);
190193
}
191194

192-
function createPost($bodyText, $topicId)
195+
/**
196+
* createPost
197+
*
198+
* NOT WORKING YET
199+
*/
200+
201+
function createPost($bodyText, $topicId, $categoryId, $userName)
193202
{
194203
$params = array(
195204
'raw' => $bodyText,
196205
'archetype' => 'regular',
206+
'category' => $categoryId,
197207
'topic_id' => $topicId
198208
);
199-
return $this->_postRequest('/posts', $params);
209+
return $this->_postRequest('/posts', $params, $userName);
200210
}
201211

202212
}

0 commit comments

Comments
 (0)