Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I set up the Semrush API client using Dependency Injection in WordPress


How do I set up the Semrush API client using Dependency Injection in WordPress


Here's an example of how you can set up the Semrush API client using Dependency Injection in a WordPress plugin:

1. Install the Semrush API client library for PHP using Composer:

bash
composer require silktide/semrush-api

2. Create a new WordPress plugin and add the following code:

php
<?php
/*
Plugin Name: Semrush API Integration
*/

require_once __DIR__ . '/vendor/autoload.php';

use Silktide\SemRushApi\ClientFactory;
use Silktide\SemRushApi\Client;

class SemrushApiIntegration {
    private $client;

    public function __construct(Client $client) {
        $this->client = $client;
    }

    public function getDomainRanks($domain) {
        $result = $this->client->getDomainRanks($domain, [
            'database' => \Silktide\SemRushApi\Data\Database::DATABASE_GOOGLE_US,
            'export_columns' => [
                \Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_RANK,
                \Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_TRAFFIC,
                \Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_ADWORDS_BUDGET,
            ]
        ]);

        return $result;
    }
}

function semrush_api_integration() {
    $client = ClientFactory::create('your_semrush_api_key');
    $integration = new SemrushApiIntegration($client);

    return $integration;
}

add_action('admin_menu', function() {
    $integration = semrush_api_integration();
    $result = $integration->getDomainRanks('example.com');

    foreach ($result as $row) {
        $rank = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_RANK);
        $traffic = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_TRAFFIC);
        $adwords_budget = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_ADWORDS_BUDGET);

        echo "Domain Rank: $rank, Traffic: $traffic, AdWords Budget: $adwords_budget\n";
    }
});

3. Replace `'your_semrush_api_key'` with your actual Semrush API key.

4. The code creates a new WordPress admin page under Tools > Semrush API Integration that displays the domain ranks, traffic, and AdWords budget for the domain "example.com" in the US database.

5. The `SemrushApiIntegration` class is responsible for interacting with the Semrush API client. It takes the `Client` instance as a dependency in its constructor, allowing for Dependency Injection.

6. The `semrush_api_integration()` function is responsible for creating the `SemrushApiIntegration` instance with the appropriate dependencies. It uses the `ClientFactory` to create the `Client` instance with the API key.

7. The `getDomainRanks()` method of the `SemrushApiIntegration` class makes the API call to retrieve the domain ranks and returns the `Result` object.

By using Dependency Injection, you can easily swap out the `Client` implementation or mock it for testing purposes. This also makes the `SemrushApiIntegration` class more testable and maintainable in the long run.

Remember to replace `'your_semrush_api_key'` with your actual Semrush API key.

Citations:
[1] https://github.com/silktide/semrush-api
[2] https://www.semrush.com/kb/5-api
[3] https://developer.semrush.com/api/basics/get-started/
[4] https://backlinko.com/hub/seo/semrush
[5] https://www.semrush.com/website/api.taboola.com/competitors/