XF 2.1 API post thread with curl

sdev

Member
Can't get the API to work.

Does anyone know how one could post a thread with it?

(btw, I've searched the whole API forum by hand. There is no information on how to post a thread, best thread is this one but I cant get Guzzle to work anyway https://xenforo.com/community/threa...d-look-like-using-the-api-with-guzzle.160858/. btw2, the API has real potential for a game changer).

Code:
$headers = array(
   'Content-type: application/json',
   'XF-Api-User: 1',
   'XF-Api-Key: xxxxx'
);

$post = [
    'node_id' => 6,
    'title' => 'test',
    'message' => 'TestMessage',
   
];

$post = json_encode($post);

$url = 'https://www.example.com/api/threads/';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);

echo $json;

Output
Code:
{ "errors": [ { "code": "required_input_missing", "message": "Required input missing: node_id, title, message", "params": { "missing": [ "node_id", "title", "message" ] } } ] }
 
Code:
$post = json_encode($post);

I believe POST parameters should be www-form-urlencoded, not json-encoded. See examples here.
 
@aisller Can you post the code?

Sending a message (but it's Python)

Code:
import requests

headers = {
          'Content-type' : 'application/x-www-form-urlencoded',
           'XF-Api-User': '1',
          'XF-Api-Key' : 'u0bqd8ukoXWqSj1fW4IOKZwc3YEMjO3o'
          }

url = 'http://test1.ru/api/posts/'
data = {
    'thread_id': 694,
    'message': 'PostFor Topic',
    'attachment_key': ''

r = requests.post(url,headers=headers,data=data)
}
 
Last edited:
Code:
$headers = array(
   'Content-type: application/json',
   'XF-Api-User: 1',
   'XF-Api-Key: xxxxx'
);

$post = [
    'node_id' => 6,
    'title' => 'test',
    'message' => 'TestMessage',
 
];

$post = http_build_query($post);

$url = 'https://www.example.com/api/threads/';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);

echo $json;

Replace
Code:
$post = json_encode($post);
with
Code:
$post = http_build_query($post);

Add:
Code:
curl_setopt($ch, CURLOPT_POST, true);

And Add:
Code:
curl_close($ch);
after curl_exec()
 
Thanks, it works perfect to read data.

But I want to manipulate the message of an existing post.

PHP:
$headers = array(
   'Content-type: application/json',
   'XF-Api-User: 1',
   'XF-Api-Key: xxxxx'
);

$post = [
'message'] => 'this is an updated message'
'silent' => true,
'clear_edit' => true,
'author_alert' => false,
'author_alert_reason' => '',
'attachment_key' => ''
];

$post = http_build_query($post);

$url = 'https://www.example.com/api/posts/12345/';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);

echo $json;

The response is "success" and I got the array of the specific post but the message was not updated.

Whats wrong?
 
This works to update date.

PHP:
$headers = array(
   'Content-type: application/x-www-form-urlencoded',
   'XF-Api-User: 1',
   'XF-Api-Key: xxxxx'
);

$post = [
'message'] => 'this is an updated message'
'silent' => true,
'clear_edit' => true,
'author_alert' => false,
'author_alert_reason' => '',
'attachment_key' => ''
];

$post = http_build_query($post);

$url = 'https://www.example.com/api/posts/12345/';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
$json = curl_exec($ch);

echo $json;
 
FYI - I wrote a basic intro for how to call the XenForo API using various tools or libraries (Postman; cURL command line; cURL library; Guzzle; Laravel v7) - https://xenforo.com/community/resources/how-to-call-the-xenforo-api.7875/

The examples use the User endpoints rather than the post/thread endpoints - but it should help as a starting point. When I get a chance, I might add some more examples that include creating a thread/post.
 
Back
Top Bottom