• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
v3.5.2 Latest
Buy Hosting

Using REST API via PHP

Scheduled Pinned Locked Moved Technical Support
11 Posts 4 Posters 223 Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Carlo LanciaC Offline
    Carlo LanciaC Offline
    Carlo Lancia
    wrote on last edited by
    #1

    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?

    phenomlabP 1 Reply Last reply
    0
  • phenomlabP Offline
    phenomlabP Offline
    phenomlab
    replied to Carlo Lancia on last edited by phenomlab
    #2

    @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);
    ?>
    
    1 Reply Last reply
    2
  • Carlo LanciaC Offline
    Carlo LanciaC Offline
    Carlo Lancia
    wrote on last edited by
    #3

    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.

    phenomlabP 1 Reply Last reply
    0
  • phenomlabP Offline
    phenomlabP Offline
    phenomlab
    replied to Carlo Lancia on last edited by
    #4

    @Carlo-Lancia You can use the above code as a basis, and develop it further from there.

    1 Reply Last reply
    0
  • Carlo LanciaC Offline
    Carlo LanciaC Offline
    Carlo Lancia
    wrote on last edited by
    #5

    <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.

    phenomlabP 1 Reply Last reply
    0
  • phenomlabP Offline
    phenomlabP Offline
    phenomlab
    replied to Carlo Lancia on last edited by
    #6

    @Carlo-Lancia Yes, you'll need to setup a token first. The API is extensive in terms of guides.

    1 Reply Last reply
    0
  • Carlo LanciaC Offline
    Carlo LanciaC Offline
    Carlo Lancia
    wrote on last edited by
    #7

    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;
    }

    phenomlabP 1 Reply Last reply
    0
  • phenomlabP Offline
    phenomlabP Offline
    phenomlab
    replied to Carlo Lancia on last edited by
    #8

    @Carlo-Lancia said in Using REST API via PHP:

    /65/tokens

    Not convinced you need this. Does it work if you remove it ?

    1 Reply Last reply
    0
  • julianJ Offline
    julianJ Offline
    julian GNU/Linux
    wrote on last edited by
    #9

    If you have a master token, you can call all of the API routes without making individual user tokens. Just pass in a _uid parameter to specify the uid you wish to call as.

    1 Reply Last reply
    1
  • barisB baris moved this topic from NodeBB Blog on
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #10

    @Carlo-Lancia not sure how you posted this topic in nodebb blog category 😆 , I moved it over to tech support.

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #11

    Turns out this topic was forked from https://community.nodebb.org/topic/16343/tutorial-install-nodebb-on-digitalocean-ubuntu-20-04/5

    1 Reply Last reply
    1

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development