A PHP API client for the SEMrush API.
- domain_ranks
- domain_rank
- domain_rank_history
- domain_organic
- domain_adwords
- domain_adwords_unique
composer require silktide/semrush-api
composer update
This library was designed to use Dependency Injection (DI). If you don't use DI, you could use the factory to set up the API client:
$client = \Silktide\SemRushApi\ClientFactory::create("[YOUR SEMRUSH API KEY]");
The API library can use a cache to reduce calls to the API. This library comes with an in-memory cache:
$cache = new \Silktide\SemRushApi\Cache\MemoryCache();
$client->setCache($cache);
You can also write your own cache using the provided CacheInterface (\Silktide\SemRushApi\Cache\CacheInterface
).
Getting the SEMrush "domain_ranks" for a website:
$result = $client->getDomainRanks('silktide.com');
Getting the SEMrush "domain_rank" for a website:
$result = $client->getDomainRank(
'silktide.com',
[
'database' => \Silktide\SemRushApi\Data\Database::DATABASE_GOOGLE_US
]
);
Getting the SEMrush "domain_rank_history" for a website:
$result = $client->getDomainRankHistory(
'silktide.com',
[
'database' => \Silktide\SemRushApi\Data\Database::DATABASE_GOOGLE_US
]
);
Getting the SEMrush "domain_organic" for a website:
$result = $client->getDomainOrganic(
'silktide.com',
[
'database' => \Silktide\SemRushApi\Data\Database::DATABASE_GOOGLE_US
]
);
Getting the SEMrush "domain_adwords" for a website:
$result = $client->getDomainAdwords(
'silktide.com',
[
'database' => \Silktide\SemRushApi\Data\Database::DATABASE_GOOGLE_US
]
);
Getting the SEMrush "domain_adwords_unique" for a website:
$result = $client->getDomainAdwordsUnique(
'silktide.com',
[
'database' => \Silktide\SemRushApi\Data\Database::DATABASE_GOOGLE_US
]
);
Here's an example of passing options to the domain ranks action in order to return a specific set of columns.
$result = $client->getDomainRanks('silktide.com', [
'export_columns' => [
\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_ADWORDS_BUDGET,
\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_ADWORDS_KEYWORDS,
\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_ADWORDS_TRAFFIC
]
]);
All API actions will return a Result
object. Result objects contain a number of Row
objects and are iterable and
countable. Here's a (non exhaustive) example of how they can be used.
foreach ($result as $row) {
$budget = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_ADWORDS_BUDGET);
echo "\nThe AdWords spend of this site in the last month was an estimated ${$budget}";
}