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/threads/what-would-creating-a-new-thread-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" ] } } ] }
 

sledov

Member
Code:
$post = json_encode($post);
I believe POST parameters should be www-form-urlencoded, not json-encoded. See examples here.
 

aisller

Member
@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:

Zambfd

New member
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()
 
Top