XF 2.1 What would creating a new thread look like using the API with Guzzle?

Brent W

Well-known member
Trying to wrap my head around this. Making a get request is pretty simple for just a thread.

Using this documentation: https://guzzle.readthedocs.io/en/latest/quickstart.html
Using this within Wordpress.

Working get request:

PHP:
<?php

define("WP_USE_THEMES", false);
require_once('/home/nginx/domains/bamapolitics.com/public/wp-blog-header.php');
require_once('/home/nginx/domains/bamapolitics.com/vendor/autoload.php');

use GuzzleHttp\Client;

$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'https://forum.bamapolitics.com/api/',
    // You can set any number of default request options.
    'timeout'  => 2.0,

    'headers' => ['XF-Api-Key' => '<key>']
]);

$response = $client->request('GET', 'threads/2/');

$body = json_decode($response->getBody(), true);

dump($body);
How would I use POST to create a thread with the required parameters as well as an array of tags?
 
Last edited:

Brent W

Well-known member
This appears to work fine but haven't done tags yet.

PHP:
$response = $client->request('POST', 'threads/', [
    'form_params' => [
        'node_id' => 32,
        'title' => 'Test',
        'message' => 'Test'
        ]
    ]);

dump($response);
 

Chris D

XenForo developer
Staff member
This appears to work fine but haven't done tags yet.
PHP:
$response = $client->request('POST', 'threads/', [
    'form_params' => [
        'node_id' => 32,
        'title' => 'Test',
        'message' => 'Test',
        'tags' => ['an', 'array', 'of', 'tags']
        ]
    ]);

dump($response);
 

Brent W

Well-known member
PHP:
$response = $client->request('POST', 'threads/', [
    'form_params' => [
        'node_id' => 32,
        'title' => 'Test',
        'message' => 'Test',
        'tags' => ['an', 'array', 'of', 'tags']
        ]
    ]);

dump($response);
Awesome thank you. This API is amazing. Thank you for all the hard work.
 

Brent W

Well-known member
What would be the best way to create line breaks as if you pressed enter when creating a post with the API?
 

Chris D

XenForo developer
Staff member
PHP:
$response = $client->request('POST', 'threads/', [
    'form_params' => [
        'node_id' => 32,
        'title' => 'Test',
        'message' => "First line of text.\n\nSecond line of text.\nAnother line directly below second.",
        'tags' => ['an', 'array', 'of', 'tags']
    ]
]);

dump($response);
Using " vs ' is significant otherwise literal \n characters will get inserted into the message.
 
Top