logo

Using the WSO2 API Manager Swagger PHP clients

Swagger, now OpenAPI, became a popular way of defining API metadata. A few years back, WSO2 API Manager started using swagger-codegen functionality to generate client SDKs. PHP is one of the supported languages and we are going to discuss, how to use the generated PHP SDKs in client-side. Why only PHP? most of the other languages are straightforward but PHP SDKs are a bit of pain. There are two ways of generating the client SDKs.

  • Through WSO2 API Manager Store UI
  • Through swagger editor

Using the SDK generated through API Manager Store

Setting up the WSO2 API Manager

  1. Download and extract the WSO2 API Manager product from the product download page. In this case, I’m using 2.1.0 version.
  2. Open the <APIM_HOME>/repository/conf/api-manager.xml and enable the php language under supported SDK languages section as follows.
    <APIManager>
        ...
        <SwaggerCodegen>
            <ClientGeneration>
                <GroupId>org.wso2</GroupId>
                <ArtifactId>org.wso2.client.</ArtifactId>
                <ModelPackage>Swagger\Client\org\wso2\client\model\</ModelPackage>
                <ApiPackage>Swagger\Client\org\wso2\client\api\</ApiPackage>
                <SupportedLanguages>php</SupportedLanguages>
            </ClientGeneration>
        </SwaggerCodegen>
    </APIManager>

    There are few things you need to know here. Unlike most of the other languages, in PHP, packages (aka namespace) are using the backslash as the separator. So we won’t be able to use Java and PHP together as supported languages. If you do so, one SDK will not be able to use. Note: when you are defining the API package and Model Package for PHP, add Swagger\Client\ as the prefix. In PHP client, autoload function will use this as an alias.

  3. Create an API and Publish to store.
  4. Generate the PHP client library from the store.

Using the Generated Client

  1. Copy the SwaggerClient-php directory from downloaded SDK to your project root.
  2. Import the SwaggerClient-php/autoload.php to your page.require_once('SwaggerClient-php/autoload.php');
  3. Import the Default API use Swagger\Client\org\wso2\client\api\MyAPI\DefaultApi as DefaultApi;
  4. Create an Object of API, set the AccessToken, Base URL and invoke
    $api_instance = new DefaultApi();
    $api_instance->getApiClient()->getConfig()->setHost('https://servicehost/myapi/v1');
    $api_instance->getApiClient()->getConfig()->setAccessToken('ad348865-810a-3e43-898e-d586b71dc812');
    
    $recordId = "12345"; 
    try {
        $result = $api_instance->employeeRecordIdGet($recordId);
        print_r($result);
    
    } catch (Exception $e) {
        echo 'Exception when calling DefaultApi->employeeRecordIdGet: ', $e->getMessage(), PHP_EOL;
    }

The complete code block is as follows

require_once('SwaggerClient-php/autoload.php');

use Swagger\Client\org\wso2\client\api\MyAPI\DefaultApi as DefaultApi;

$api_instance = new DefaultApi();
$api_instance->getApiClient()->getConfig()->setHost('https://servicehost/myapi/v1');
$api_instance->getApiClient()->getConfig()->setAccessToken('ad348865-810a-3e43-898e-d586b71dc812');

$recordId = "12345"; 
try {
    $result = $api_instance->employeeRecordIdGet($recordId);
    print_r($result);

} catch (Exception $e) {
    echo 'Exception when calling DefaultApi->employeeRecordIdGet: ', $e->getMessage(), PHP_EOL;
}

Using the Swagger Editor generated PHP client code

When you are generating the PHP client code through the Swagger editor, you have to use the PHP composer util to import all the dependencies. New PHP SDK Clients use GuzzleHttp as the HTTP client library.

Setting up the environment.

  1. Install the PHP composer.
    1. Follow the instructions in Getting Started page of Composer website.
      php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
      php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
      php composer-setup.php
    2. Rename the composer.phar to the composer and append the location to the PATH environment variable so that you can run composer in command line from anywhere.
  2. Copy the SwaggerClient-php directory from downloaded SDK to your project root.
  3. Run the composer install command from the SwaggerClient-php directory (where you have the composer.json file). Once it is completed, you can find a new directory called a vendor and there will be a file called autoload.php inside it (and many more folders).
  4. Import the autoload.php to your page. require_once('SwaggerClient-php/vendor/autoload.php');
  5. Create a Configuration object and set the configuration values.
    $config = Swagger\Client\Configuration::getDefaultConfiguration()->setAccessToken('ad3cb00e-2d7d-3a94-8588-b5c460a90562');
    $config->setHost('https://servicehost/myapi/v1');
  6. Create an instance of DefaultAPI and invoke.
    $api_instance = new Swagger\Client\Api\DefaultApi(
        new GuzzleHttp\Client(),
        $config
    );
    
    $recordId = "12345"; 
    try {
        $result = $api_instance->employeeRecordIdGet($recordId);
        print_r($result);
    
    } catch (Exception $e) {
        echo 'Exception when calling DefaultApi->employeeRecordIdGet: ', $e->getMessage(), PHP_EOL;
    }

     

Complete code block as follows

require_once('SwaggerClient-php/vendor/autoload.php');

$config = Swagger\Client\Configuration::getDefaultConfiguration()->setAccessToken('ad3cb00e-2d7d-3a94-8588-b5c460a90562');
$config->setHost('https://servicehost/myapi/v1');

$api_instance = new Swagger\Client\Api\DefaultApi(
    new GuzzleHttp\Client(),
    $config
);

$recordId = "12345"; 
try {
    $result = $api_instance->employeeRecordIdGet($recordId);
    print_r($result);

} catch (Exception $e) {
    echo 'Exception when calling DefaultApi->employeeRecordIdGet: ', $e->getMessage(), PHP_EOL;
}

 

 

 

 

Comments are closed.