Integrating external SDKs

In this tutorial, you will learn how to integrate an external SDK into your plugin. By integrating an external SDK and adding a dependency between your plugin and a repository on Packagist, you can access the functionality of the external SDK.

Step 1: Cloning the HelloWorld plugin

In this tutorial, we will extend the functionality of the HelloWorld plugin by integrating Guzzle, a PHP HTTP client. We then use the external SDK to send an HTTP request to an API, e.g. the Packagist API, to retrieve certain information.

The HelloWorld plugin structure

Below, you find an overview of the existing structure of our HelloWorld plugin.

    ├── resources/
    │   └── views/
    │       └── content/
    │           └── hello.twig
    ├── src/
    │   ├── Providers/
    │   │   ├── HelloWorldServiceProvider.php
    │   │   └── HelloWorldRouteServiceProvider.php
    │   │
    │   └── Controllers/
    │       └── ContentController.php
    ├── plugin.json // plugin information
    └── // additional files (Readme, license etc.)

Step 2: Extending the plugin structure

In order to integrate the external SDK, we have to make changes to the following existing files. We also have to add a new folder with a new file:

  1. Update the plugin.json
  2. Create the resources/lib folder and add the guzzle_connector.php file
  3. Extend the ContentController.php
  4. Extend the hello.twig

Updating the plugin.json

First, we have to specify the dependencies of our plugin to the Guzzle SDK by updating the plugin.json file.

    "description":"My first plugin",
    "author":"Your name",
    "dependencies" : {
        "guzzlehttp/guzzle": "6.3.*"

Creating the guzzle_connector.php

External PHP code can only be executed in PHP files that are saved in the lib folder. So, we create a new PHP file that will be the link between the plentymarkets plugin API and an external API.

We take the code example from Guzzle and make some adaptions so that the code fits our needs. Remember that we want to send an HTTP request to the Packagist API. Let's search packages by name.


$client = new \GuzzleHttp\Client();
$res = $client->request(
        'query' => ['q' => SdkRestApi::getParam('packagist_query')]

/** @return array */
return json_decode($res->getBody(), true);

Extending the ContentController

In order to execute the previously created PHP file, we need to enable HTTP requests in the ContentController, add functionality to address the PHP response and process the returned data.


namespace HelloWorld\Controllers;

use Plenty\Plugin\Controller;
use Plenty\Plugin\Templates\Twig;
use Plenty\Modules\Plugin\Libs\Contracts\LibraryCallContract;
use Plenty\Plugin\Http\Request;

 * Class ContentController
 * @package HelloWorld\Controllers
class ContentController extends Controller
	 * @param Twig $twig
	 * @param LibraryCallContract $libCall
	 * @param Request $request
	 * @return string
	public function sayHello(
		Twig $twig,
		LibraryCallContract $libCall,
		Request $request

		$packagistResult =
				['packagist_query' => $request->get('search')]
		return $twig->render('HelloWorld::content.hello', ['packagistResult' => $packagistResult);

Making a request

We mentioned above that we want to search Packagist packages by name. So when sending the HTTP request to get all packages with the name plentymarkets, e.g. with Postman, we receive the following response consisting of the results array and the total number of packages:
    "results": [
        "name": "plentymarkets/plugin-hack-api",
        "description": "API for plentymarkets plugin development with Hack",
        "url": "",
        "repository": "",
        "downloads": 13,
        "favers": 1
        "name": "composer/installers",
        "description": "A multi-framework Composer library installer",
        "url": "",
        "repository": "",
        "downloads": 4931419,
        "favers": 641
        "name": "alexcold/yii2-plentyconnector",
        "description": "PlentyMarkets SOAP API connector",
        "url": "",
        "repository": "",
        "downloads": 10,
        "favers": 0
        "name": "plentymarkets/plugin-hello-world",
        "description": "Creates an empty frontend plugin for plentymarkets plugin development",
        "url": "",
        "repository": "",
        "downloads": 12,
        "favers": 0
    "total": 4

Extending the twig template

In our TWIG template we don't need all the information of the JSON response. We only want to display the total number of packages, the package name and the package repository. If no packages were found, we want to display the text No entries.

<h1>Hello World!</h1>

<p>{{ }} Results</p>
    {% for packagistItem in packagistResult.results %}
        <li>{{ }} : {{ packagistItem.repository }}</li>
    {% else %}
        <li>No entries</li>
    {% endfor %}

Searching for packages

Next, we deploy the plugin in a plugin set. Now, we are able to search for packages via the address bar of the browser. Open your browser and go to The template will be rendered and an empty result list will be displayed.

By adding ?search=plentymarkets to the URL, we will send an HTTP request to Packagist that returns all packages with the search word plentymarkets. The items of the result will be rendered in the result list. Requests are limited to prevent an overflow of queries.

Is this article helpful?


Thank you for your Feedback

you can close this field now!