How to correctly implement 3rd party SDK

I’m trying to integrate a 3rd party php SDK in my Kirby project.
I’ve installed it via composer, created a plugin folder and file and I’m able to use the SDK on my site.
When I try to access the panel I get a 500 error… What’s the correct way to implement this in Kirby?

Do other subpages work? Did this issue only occur after installing the SDK?

Maybe this helps: Panel 500 Error

Other subpages do work. It only occurs after installing the SDK. When I disable it, everything work just fine.
I’m running php 7.1.1 on MAMP 4.1.1.

Well, without knowing what and how you have implemented it, I’m afraid I can’t really help you.

The sdk is installed via composer. This is my plugin file in plug-ins/moltin/moltin.php

<?php
	require 'vendor/autoload.php';

	use Moltin\SDK\Facade\Moltin as Moltin;   
	Moltin::Authenticate('ClientCredentials', [
	'client_id'     => '1234',
	'client_secret' => '5678'
	]);

And where did you install the SDK, in the plugins folder?

No, its in my root /vendor folder. I was looking in the wrong log file. the autoload file can’t be found in the panel, when I change it to …/vendor the panel works again. What’s the best practice in this case?

I’d create the composer.json in the moltin folder and install into that.

Cool, thanks. Got it sorted now!

Another option, if you’re using this plugin on the front-end only, is to have the composer.json at the root, and require it from site.php:

<?php
require_once __DIR__ . '/vendor/autoload.php';
$kirby = kirby();

Then in your plugin file, you can check if the class exists before doing anything:

<?php
use Moltin\SDK\Facade\Moltin as Moltin;

if (class_exists('Moltin')) {
  Moltin::Authenticate('ClientCredentials', [
    'client_id'     => '1234',
    'client_secret' => '5678'
  ]);
  // etc.
}

Note that both the front pages (start point: index.php) and the panel pages (start point: panel/index.php) will load site.php, so your Composer dependencies will be available in both contexts.

1 Like