Using REST API via PHP
-
Hello, I have installed and updated to nodeBB v.2.8.10 release on Ubuntu 20.04.5 LTS.
I want to start integrating this fantastic product, need examples to use REST API in PHP language.
I linked to the reference documentation at the link https://docs.nodebb.org/api/ , didn't find any examples.
Can you help me? -
@Carlo-Lancia Something like this ?
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://localhost:4567/api/v3/topics'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"cid\": 27,\"title\": \"$subject\",\"content\": \"$body\",\"tags\": [\"test\",\"topic\"]}"); $headers = array(); $headers[] = 'Authorization: Bearer <your-token-here>'; $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } print_r($result); curl_close($ch); ?>
-
That's right, it would be useful to know how to develop code making REST API calls in PHP, specific to authentication and iframe control passing.
-
@Carlo-Lancia You can use the above code as a basis, and develop it further from there.
-
<your-token-here> how do I get the token?
Are you referencing the /admin/settings/api page?
Because this solution I can not adopt it.
I need to authenticate with login and password then launch another page where I have already been authenticated.I was thinking about the API
POST
http://localhost:4567/api/v3/utilities/login
{
"username": "user",
"password": "password"
}redirect the page to a specific category.
-
@Carlo-Lancia Yes, you'll need to setup a token first. The API is extensive in terms of guides.
-
Thank you so much for the time you gave me.
But I don't understand why the creation of the user token for each user from the master token doesn't work.
Where am I wrong?<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "http://localhost:4567/api/v3/users/65/tokens",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n\t"_uid": 65\n}\n",
CURLOPT_COOKIE => "express.sid=s%253A4LJ4VwxZXxtNg9qGh8yoq-OphkRQrcs3.M6EW%252FB%252BiwxTcwTo0atCsqvcFugMR%252B8H7v1iFSIFBIzM",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer 05a87379-9274-4bf3-93fb-ca5ec0ded3e2", // MASTER TOKEN
"Content-Type: application/json"
],
]);$response = curl_exec($curl);
$err = curl_error($curl);curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
} -
@Carlo-Lancia said in Using REST API via PHP:
/65/tokens
Not convinced you need this. Does it work if you remove it ?
-