I hope it would be helpful for somebody who wants to use RESTful APIs for a batch process.
How to Create a Post with a Title, an Image from AWS S3 and Multiple Tags
for ($i = 1 ; $i <= $count; $i++) {
$num = sprintf("%02d", $i);
$title = "No. " . $num;
$body = ""; /* Markdown format */
/*
* Make JSON data format
*
* Tag list includes tag IDs like
* $taglist = array(
* array('type' => 'tags', 'id' => '17'),
* array('type' => 'tags', 'id' => '27'),
* array('type' => 'tags', 'id' => '28')
* );
*/
$data = array(
'data' => array(
'type' => 'discussions',
'attributes' => array('title' => $title, 'content' => $body),
'relationships' => array('tags' => array('data' => $taglist))
));
$url = startDiscussion($data);
/* Do something with the URL */
}
How to Start a Discussion and Get the URL of the Created One
function startDiscussion($data) {
$url = "https://<your domain>/api/discussions";
$content = json_encode($data);
/* Please refer to https://discuss.flarum.org/d/1871-bypassing-flood-control. */
$token = "<ID value (40 characters) in your api_keys table>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type: application/json',
'Accept: application/json',
'Authorization: Token ' . $token . '; userId=1'));
$result = curl_exec($ch);
$response = json_decode($result);
curl_close($ch);
/* Discussion URL format: <domain>/d/id-slug */
$data = $response->{'data'};
$attr = $data->{'attributes'};
$url = "https://<your domain>/d/" . $data->{'id'} . "-" . $attr->{'slug'};
return $url;
}
References