We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Simple API

How Do I?

vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

Is there some way to achieve simple API commands natively with Expression Engine 6?

There is the following module:

https://expressionengine.com/add-ons/reinos-webservice

But I don’t actually need a full API web service for multiple users. Just my own use.

One idea is to create an add-on and then execute it with the new CLI. My requirements are actually very simple.

  • Read a specific channel entry and output data in JSON
  • Be able to add a new entry
  • Modify an entry

Is there any tutorial, or should I just get the posted add-on?

If possible, I would always prefer native EE functionality over third-party code that requires extra work to maintain and support.

While are tutorials on the CLI there are not really any about making such simple plugins or extensions.

       
szgalbraith's avatar
szgalbraith
6 posts
4 years ago
szgalbraith's avatar szgalbraith

You could make your own addon and use the Model Service to manage any channel entries and output JSON results.

See: https://docs.expressionengine.com/latest/development/models/channel-entry.html

       
vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

That is what I was asking, some tutorial might be helpful for a basic add-on or code examples.

While the documentation is detailed about every section, it does not really explain on how to tight all the things together from start to the end output.

One more idea for tutorials on the EE university.

       
vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

By tutorials I mean, for example, you have the following example:

https://docs.expressionengine.com/latest/development/models/channel-entry.html

$entry              = ee('Model')->make('ChannelEntry');
$entry->author_id   = ee()->session->userdata('member_id'); // Returns currently Logged-in user ID.
$entry->channel_id  = 1,
$entry->title       = 'An Awesome Title';
$entry->url_title   = ee('Format')->make('Text', 'An Awesome Title')->urlSlug()->compile(); // Returns an-awesome-title. Must be unique.
$entry->status      = ee('Model')->get('Status', 1)->first()->status; // Returns 'open';
$entry->entry_date  = ee()->localize->now; // Returns time in seconds: 1623945317

// Validate and Save.
$result = $entry->validate();

if ($result->isValid())
{
  $entry->save();
}

$entry->entry_id; // Will now return the new Entry ID.

This is supposed to create a simple entry ID. But where is the rest of the code?

Assuming I’m not creating an add-on but just want to use this directly inside PHP in a EE template for testing purposes.

       
dougblackjr's avatar
dougblackjr
34 posts
4 years ago
dougblackjr's avatar dougblackjr

@vw000 I have an add-on I am testing that does this very thing. I can share a copy if you’d be willing to test!

? 1
       
vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

Do I need to load a library first on my PHP template for the above code?

Example: ee()->load->library(‘model’);

I tried that but I get an error while trying to execute that simple example in a template with PHP enabled.

       
dougblackjr's avatar
dougblackjr
34 posts
4 years ago
dougblackjr's avatar dougblackjr

You shouldn’t need to load anything. Can you post the error you’re seeing?

The one thing I did notice is this line:

$entry->channel_id  = 1,

That comma should be a semicolon.

       
vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

That is straight from the documentation. I guess there is a typo in the code.

I don’t get any errors now, but putting that inside PHP in a template does not seem to do anything. Nothing is posted as an entry.

I will try to see what is missing. Is there something similar for comments?

       
dougblackjr's avatar
dougblackjr
34 posts
4 years ago
dougblackjr's avatar dougblackjr

Can you share your code?

       
vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

The same as above, posted here inside PHP opening and closing tags. Nothing else.

I guess my body field is missing for the entry. My channel body field is called “message_body”

I suspect nothing is being posted for that reason, since it’s missing. Any examples on how this would work on a proper channel with the title and one body field?

The example would probably be better if it displayed the errors on posting.

       
dougblackjr's avatar
dougblackjr
34 posts
4 years ago
dougblackjr's avatar dougblackjr

I think the problem with doing this in the template is that it doesn’t parse the actual template, nor just return the data.

If you are trying to read an entry, I think there are two ways to go about this.

Create an Addon

You could create an addon that gets the actual entry from a template parameter, and then parse and return as JSON.

So your template might be:

{exp:my_addon entry_id="{segment_2}"}

Which would then run your code above:

$entry = ee('Model')->get('ChannelEntry')
    ->filter('entry_id', ee()->TMPL->fetch_param('entry_id'))
    ->first();

// Do any manipulations here

// Then you'll need to manually assign an object to send back. EE ChannelEntry models
// Aren't JSONSerializable by default.

$output = [];

foreach ($entry as $key => $value) {
    $output[$key] = $value;
}

return ee()->output->send_ajax_response($output);

Create a JSON template

What may be easier is just to utilize EE’s native functionality and create a JSON template.

{
    "data": "{exp:channel:entries entry_id="{segment_2}"}{
        "title": "{title}",
        "entry_id": "{entry_id}"
        ...
    }{/exp:channel:entries}
}

Creating an entry would be about the same, but I would definitely go the addon route for that:

public function createEntry()
{
    $entry              = ee('Model')->make('ChannelEntry');
    $entry->author_id   = ee()->session->userdata('member_id'); // Returns currently Logged-in user ID.
    $entry->channel_id  = ee()->input->get_post('channel'), // This will get your data and clean it, for security purposes
    $entry->title       = ee()->input->get_post('title');
    ...

    // Validate and Save.
    $result = $entry->validate();

    if ($result->isValid())
    {
      $entry->save();
    }

    $output = [
        'entry_id' => $entry->entry_id,
    ];

    return ee()->output->send_ajax_response($output);
}

This will need some work for your specific needs, but should get you close.

       
vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

Thanks, I will try that out.

       
jaygreentree's avatar
jaygreentree
9 posts
3 years ago
jaygreentree's avatar jaygreentree

I know I’m a bit late to this post but I found a plugin that can do pretty much what is listed above. https://github.com/rsanchez/json

       
vw000's avatar
vw000
482 posts
3 years ago
vw000's avatar vw000

Thanks for sharing @jaygreentree

Since I posted this, I actually purchased the Reinos Add-on:

https://expressionengine.com/add-ons/reinos-webservice

I only did some testing, but it works, not using it yet.

And there is now also Bones which does also JSON output:

https://expressionengine.com/add-ons/bones

I was going to just call the data with a PHP code and output in JSON, similar to the plugin you posted, but for the price of those add-ons I guess it’s just cheaper to purchase them and have a solution that works out of the box. My problem was a bit more complex since I need to create or edit channels (POST data) to EE.

If you just need a simple JSON for Ajax calls, that plugin would do the job.

If you need something more fancy, Bones would do it, but from checking the docs it’s mostly to get data out of EE or just read data.

In case you need to get data into Expression Engine which is hard because you can’t just do a POST request with CURL or similar without having to disable the XSS security check (which is not advisable) then the Reinos add-on seems to be the only option it seems.

It’s a bit more complex, but it basically runs a full REST API service for your installation. Some modules like comments and others are not updated to EE 6, but the developer is very responsive to fix things when you point them out. A bit more expensive than other add-ons, but seems to work nice so far in my dev installation.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.