#1 November 24, 2017 3:26pm

daansk44
Member
From: Amersfoort, The Netherlands
Registered: September 12, 2016
Posts: 42

Add pages using API

Hey,

Is it possible to make a page using the API and fill in the info of the page directly.
Let's say you have a video website. And all video's are uploaded at the back of the server.

So those video's got ripped and the data is putted in the page using the API

The page has a template (lets say video).
That contains the title, the url and the description

Is this possible?

Last edited by daansk44 (November 24, 2017 3:28pm)

Offline

#2 November 24, 2017 7:18pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Add pages using API

You can use the internal API to do this fairly easy by calling BigTreeAdmin's createPage method. There's no public REST API so you'd need to implement that yourself through AJAX calls or a routed template.

Offline

#3 November 25, 2017 4:47am

daansk44
Member
From: Amersfoort, The Netherlands
Registered: September 12, 2016
Posts: 42

Re: Add pages using API

I wanted to do it using PHP (internal),
So you say I can use the createPage($data) class.

How how to add data in that page (using an array)?

Offline

#4 November 25, 2017 8:12am

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Add pages using API

The $resources parameter contains the page data that would normally be entered by the user.

Offline

#5 December 24, 2018 7:24am

daansk44
Member
From: Amersfoort, The Netherlands
Registered: September 12, 2016
Posts: 42

Re: Add pages using API

Thanks for your reply. Suppose you want to create a (sub) page. As an example 'Products'. Below you will find a product 'products / usbstick'. How can you indicate that these are hung under products (2)?

Offline

#6 December 24, 2018 7:57am

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Add pages using API

You can pass in the parent page ID as “parent”. For example:

$admin->createPage([
    “parent” => 2,
     “nav_title” => “USB Stick”
]);

Offline

#7 December 24, 2018 10:21am

daansk44
Member
From: Amersfoort, The Netherlands
Registered: September 12, 2016
Posts: 42

Re: Add pages using API

Thank you and we wish you a Merry Christmas from the Netherlands smile

Offline

#8 December 24, 2018 10:38am

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Add pages using API

Merry Christmas to you as well!

Offline

#9 December 24, 2018 11:20am

daansk44
Member
From: Amersfoort, The Netherlands
Registered: September 12, 2016
Posts: 42

Re: Add pages using API

Let's say I have made a Template file, named Products
And this contains some Resources

$Model = Model - Text
$ItemCode = ItemCode - Text
$ItemDesc = ItemDesc - Text
$ExtDesc = ExtDesc - Text

How can I make the page with this template and add the data to it?
Also, how can I fill in the SEO data smile

$admin->createPage([
    “parent” => 2,
     “nav_title” => “USB Stick”,
“Template“ => “Products“, 
“ItemCode“ => “MyCode“
]);

Offline

#10 December 24, 2018 1:09pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Add pages using API

$admin->createPage([
    "parent" => 2,
    "nav_title" => "USB Stick",
    "template" => "products",
    "resources" => [
        "ItemCode" => "MyCode",
        "Model" => "MyModel",
        "ItemDesc" => "qwerty",
        "ExtDesc" => "asdf"
    ],
    "meta_description" => "This is the SEO meta description",
    "title" => "This is the SEO page title"
]);

Offline

#11 December 25, 2018 7:47am

daansk44
Member
From: Amersfoort, The Netherlands
Registered: September 12, 2016
Posts: 42

Re: Add pages using API

Thank you.

We are running in the next problem. We are writing a script that fetches an XML string and will loop through it.

class SHOPXML
{
    public $url;

    public function FetchandreturnShopXML($url)
    {
        $xml = simplexml_load_file($url);
        return($xml);
    }

    public function FetchProductsSHOPXML(){
         $admin = new BigTreeAdmin;
        var_dump($admin->createPage([
            "parent" => 1,
            "nav_title" => "USB Stick",
            "template" => "products",
            "resources" => [
                "ItemCode" => "MyCode",
                "Model" => "MyModel",
                "ItemDesc" => "qwerty",
                "ExtDesc" => "asdf"
            ],
            "meta_description" => "This is the SEO meta description",
            "title" => "This is the SEO page title"
        ]));
}

Since it is a script for a cronjob, we put it in `site\job\classes\SHOPXML`.
But it cannot find the call the createPage function.

Wich class do we need to call before we can use the createPage function


Fatal error: Uncaught Error: Class 'BigTreeCms' not found in C:\xampp\htdocs\projecten\xx\Source\site\job\classes\SHOPXML\index.php:4 Stack trace: #0 C:\xampp\htdocs\projecten\xx\Source\site\job\classes\classloader.php(3): include() #1 C:\xampp\htdocs\projecten\xx\Source\site\job\index.php(2): include('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\projecten\xx\Source\site\job\classes\SHOPXML\index.php on line 4

When we call the function

  $admin = new BigTreeAdmin;
        var_dump($admin->createPage());[ /code]
It returns  [ code] Too few arguments to function BigTreeAdminBase::createPage(), 0 passed

That's okay. But when we call the function, using the code above. It returned NULL without making an page

Last edited by daansk44 (December 25, 2018 8:25am)

Offline

#12 December 25, 2018 6:13pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Add pages using API

If you're running something from within the site directory it hasn't bootstrapped BigTree so it doesn't have access to $admin and $cms.

Try something like this at the top of your file to bootstrap BigTree first:

$server_root = str_replace("site/job/classes/SHOPXML/index.php","",strtr(__FILE__, "\\", "/"));
include $server_root."custom/environment.php";
include $server_root."custom/settings.php";
include $server_root."core/bootstrap.php";

Offline

#13 December 26, 2018 5:43am

daansk44
Member
From: Amersfoort, The Netherlands
Registered: September 12, 2016
Posts: 42

Re: Add pages using API

Thank you, Tim, for your reply. I really Appreciate your help

We have replaced  the files to

templates\ajax\classes\SHOPXML

And try to call the function in the ajax/index file.

include 'classes/classloader.php';
$shopxml = new SHOPXML;
$shopxml->FetchProductsSHOPXML();

When we call it with the wrong data, it returns an error (that's okay).
But when we push the right data, it returns with nothing and does not make a page

public function FetchProductsSHOPXML()
    {
        $admin = new BigTreeAdmin;
        var_dump($admin->createPage());
        $admin->createPage([
            "parent" => 1,
            "nav_title" => "USB Stick",
            "template" => "Products",
            "resources" => [
                "ItemCode" => "MyCode",
                "Model" => "MyModel",
                "ItemDesc" => "qwerty",
                "ExtDesc" => "asdf",
            ],
            "meta_description" => "This is the SEO meta description",
            "title" => "This is the SEO page title",
        ], $publishing_change = true);
    }

It has no problem finding the function at all. But it does not generate an error

Last edited by daansk44 (December 26, 2018 6:14am)

Offline

#14 December 26, 2018 2:26pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Add pages using API

It's possible that your database is running in strict standards mode and there's a column that is not receiving a value that is set to NOT NULL. Try doing this after your createPage call to see what errors are returned from MySQL:

global $bigtree;
print_r($bigtree["sql"]["errors"]);
print_r(SQL::$ErrorLog);

Offline

#15 December 29, 2018 1:28pm

daansk44
Member
From: Amersfoort, The Netherlands
Registered: September 12, 2016
Posts: 42

Re: Add pages using API

Thank you Tim,

The first two we have fixed, but it still returns this on the development server (live server is working, but we have 27000 rows so running this remotely is a bit slow).

Array ( [0] => Column 'last_edited_by' cannot be null [1] => Column 'entry' cannot be null )

We have last_edited_by set to Zero

     $admin->createPage([
                "parent" => 1,
                "nav_title" => $xmlitem->ItemDesc." | " .$xmlitem->SimpleColor,
                "in_nav" => "on",
                "template" => "Products",
                "new_window" => "new",
                "entry" => "new",
                "created_at" => "2018-12-28 03:16:44",
                "last_edited_by" => 0,

And the

 entry 

column does not even exist so that is a bit weird.

The problem still exists after we added

entry

Last edited by daansk44 (December 29, 2018 2:22pm)

Offline

#16 December 31, 2018 11:23am

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Add pages using API

Looking into the createPage method, the only time "entry" is in the code is for inserting tags into the database. I'm not sure what's going on there.

Can you try doing:

global $bigtree;
print_r($bigtree["sql"]["queries"]);
print_r(SQL::$QueryLog);

That will show exactly what queries are being run. Make sure you have debug on in your environment.php file or queries won't be logged.

Offline

Board footer

Powered by FluxBB

The Discussion Forum is not available on displays of this size.