Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #33 from silktide/add-domain-domains
Browse files Browse the repository at this point in the history
Add domain domains endpoint
  • Loading branch information
andywaite authored Jan 26, 2022
2 parents 8fc8f32 + ce7fd36 commit cb72fcf
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ A PHP API client for the SEMrush API.
## Supported actions:

* domain_ranks
* domain_domains
* domain_rank
* domain_rank_history
* domain_organic
Expand Down
21 changes: 21 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,27 @@ public function getDomainRankHistory($domain, $options = [])
return $this->makeRequest(Type::TYPE_DOMAIN_RANK_HISTORY, ['domain' => $domain] + $options);
}

/**
* @param array $domains
* @param $options
* @return ApiResult
* @throws Exception
*/
public function getDomainVsDomains(array $includedDomains, array $excludedDomains = [], array $options = [])
{
$domains = [];

foreach ($includedDomains as $domain) {
$domains[] = '*|or|'.$domain;
}

foreach ($excludedDomains as $domain) {
$domains[] = '-|or|'.$domain;
}

return $this->makeRequest(Type::TYPE_DOMAIN_VS_DOMAIN, ['domains' => implode('|', $domains)] + $options);
}

/**
* @param $domain
* @param array $options
Expand Down
5 changes: 5 additions & 0 deletions src/Data/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ abstract class Column
const COLUMN_SERP_FEATURE_KEYWORD_ALSO_ASK = 'FK21';
const COLUMN_SERP_FEATURE_KEYWORD_FAQ = 'FK22';
const COLUMN_SERP_FEATURE_KEYWORD_FLIGHTS = 'FK23';
const COLUMN_FIRST_DOMAIN = 'P0';
const COLUMN_SECOND_DOMAIN = 'P1';
const COLUMN_THIRD_DOMAIN = 'P2';
const COLUMN_FOURTH_DOMAIN = 'P3';
const COLUMN_FIFTH_DOMAIN = 'P4';

/**
* Get all the possible columns
Expand Down
1 change: 1 addition & 0 deletions src/Data/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract class Type
const TYPE_DOMAIN_ORGANIC = "domain_organic";
const TYPE_DOMAIN_ADWORDS = "domain_adwords";
const TYPE_DOMAIN_ADWORDS_UNIQUE = "domain_adwords_unique";
const TYPE_DOMAIN_VS_DOMAIN = "domain_domains";
const TYPE_ADVERTISER_PUBLISHERS = "advertiser_publishers";
const TYPE_ADVERTISER_DISPLAY_ADS = "advertiser_text_ads";
const TYPE_ADVERTISER_RANK = "advertiser_rank";
Expand Down
42 changes: 42 additions & 0 deletions src/Model/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ protected function validateOption($option, $value)
$this->validateDomain($option, $value);
break;

case "domains":
$this->validateDomains($option, $value);
break;

case "database":
$this->validateDatabase($option, $value);
break;
Expand Down Expand Up @@ -218,6 +222,44 @@ protected function validateDomain($key, $domain)
}
}

/**
* Validate domain
*
* @param string $key
* @param string $domain
* @throws InvalidOptionException
*
* Validates the format <sign>|<type>|<domain>|<sign>|<type>|<domain>|...
*/
protected function validateDomains($key, $domains)
{
$parts = explode('|', $domains);

if (count($parts) > 5 * 3) { // 5 domains max, each with 3 parts
throw new InvalidOptionException("[{$key}] contains too many domains");
}

for ($i = 0; $i < count($parts); $i++) {
switch ($i % 3) {
case 0:
if (!in_array($parts[$i], ['+', '-', '/', '*'], true)) {
throw new InvalidOptionException("[{$key}] contains an invalid sign [{ $parts[$i]}]");
}
break;
case 1:
if (!in_array($parts[$i], ['or', 'ad'], true)) {
throw new InvalidOptionException("[{$key}] contains an invalid type - must be or or ad [{ $parts[$i]}]");
}
break;
case 2:
if (!preg_match('/^[a-z0-9-.]+$/i', $parts[$i])) {
throw new InvalidOptionException("[{$key}] contains an invalid domain [{ $parts[$i]}]");
}
break;
}
}
}

/**
* Validate date
*
Expand Down
49 changes: 48 additions & 1 deletion src/Model/requestDefinitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,52 @@
"COLUMN_SERP_FEATURE_KEYWORD_FLIGHTS"
]
},
"domain_domains": {
"required_fields": {
"type": "type",
"key": "string",
"domains": "domains",
"database": "database"
},
"optional_fields": {
"display_date": "date",
"export_columns": "columns",
"display_sort": "string",
"display_limit": "integer",
"export_escape": "boolean"
},
"preset_fields": {
"export_escape": "1"
},
"default_columns": [
"COLUMN_DOMAIN_KEYWORD",
"COLUMN_FIRST_DOMAIN",
"COLUMN_SECOND_DOMAIN",
"COLUMN_THIRD_DOMAIN",
"COLUMN_FOURTH_DOMAIN",
"COLUMN_FIFTH_DOMAIN",
"COLUMN_KEYWORD_ORGANIC_NUMBER_OF_RESULTS",
"COLUMN_KEYWORD_AVERAGE_CLICK_PRICE",
"COLUMN_KEYWORD_AVERAGE_QUERIES",
"COLUMN_KEYWORD_DIFFICULTY_INDEX",
"COLUMN_KEYWORD_COMPETITIVE_AD_DENSITY",
"COLUMN_KEYWORD_INTEREST"
],
"valid_columns": [
"COLUMN_DOMAIN_KEYWORD",
"COLUMN_FIRST_DOMAIN",
"COLUMN_SECOND_DOMAIN",
"COLUMN_THIRD_DOMAIN",
"COLUMN_FOURTH_DOMAIN",
"COLUMN_FIFTH_DOMAIN",
"COLUMN_KEYWORD_ORGANIC_NUMBER_OF_RESULTS",
"COLUMN_KEYWORD_AVERAGE_CLICK_PRICE",
"COLUMN_KEYWORD_AVERAGE_QUERIES",
"COLUMN_KEYWORD_DIFFICULTY_INDEX",
"COLUMN_KEYWORD_COMPETITIVE_AD_DENSITY",
"COLUMN_KEYWORD_INTEREST"
]
},
"domain_rank": {
"required_fields": {
"type": "type",
Expand Down Expand Up @@ -336,7 +382,8 @@
"COLUMN_DOMAIN_KEYWORD_NUMBER",
"COLUMN_TIMESTAMP",
"COLUMN_SERP_FEATURES_DOMAIN",
"COLUMN_SERP_FEATURES_KEYWORD"
"COLUMN_SERP_FEATURES_KEYWORD",
"COLUMN_KEYWORD_DIFFICULTY_INDEX"
]
},
"domain_shopping": {
Expand Down
2 changes: 1 addition & 1 deletion test/Integration/AbstractIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


namespace Silktide\SemRushApi\Integration;
namespace Silktide\SemRushApi\Test\Integration;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
Expand Down
2 changes: 1 addition & 1 deletion test/Integration/DomainAdwordsIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


namespace Silktide\SemRushApi\Integration;
namespace Silktide\SemRushApi\Test\Integration;

use Silktide\SemRushApi\Data\Column;
use Silktide\SemRushApi\Data\Database;
Expand Down
2 changes: 1 addition & 1 deletion test/Integration/DomainAdwordsUniqueIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


namespace Silktide\SemRushApi\Integration;
namespace Silktide\SemRushApi\Test\Integration;

use Silktide\SemRushApi\Data\Column;
use Silktide\SemRushApi\Data\Database;
Expand Down
2 changes: 1 addition & 1 deletion test/Integration/DomainOrganicIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


namespace Silktide\SemRushApi\Integration;
namespace Silktide\SemRushApi\Test\Integration;

use Silktide\SemRushApi\Data\Column;
use Silktide\SemRushApi\Data\Database;
Expand Down
2 changes: 1 addition & 1 deletion test/Integration/DomainRankHistoryIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


namespace Silktide\SemRushApi\Integration;
namespace Silktide\SemRushApi\Test\Integration;

use Silktide\SemRushApi\Data\Column;
use Silktide\SemRushApi\Data\Database;
Expand Down
2 changes: 1 addition & 1 deletion test/Integration/DomainRankIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


namespace Silktide\SemRushApi\Integration;
namespace Silktide\SemRushApi\Test\Integration;

use Silktide\SemRushApi\Data\Column;
use Silktide\SemRushApi\Data\Database;
Expand Down
2 changes: 1 addition & 1 deletion test/Integration/DomainRanksIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


namespace Silktide\SemRushApi\Integration;
namespace Silktide\SemRushApi\Test\Integration;

use Silktide\SemRushApi\Data\Column;
use Silktide\SemRushApi\Model\Result;
Expand Down
27 changes: 27 additions & 0 deletions test/Integration/DomainVsDomainsIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php


namespace Silktide\SemRushApi\Test\Integration;

use Silktide\SemRushApi\Data\Column;
use Silktide\SemRushApi\Data\Database;
use Silktide\SemRushApi\Model\Row;
use Guzzle\Http\Message\Response;

class DomainVsDomainsIntegrationTest extends AbstractIntegrationTest {

public function testDomainRankHistoryRequest()
{
$this->setupResponse('domain_domains');
$result = $this->client->getDomainVsDomains(['nike.com', 'adidas.com'], ['reebok.com'], ['database' => Database::DATABASE_GOOGLE_US]);
$this->verifyResult($result, 10);

/**
* @var Row $row
*/
$row = $result[1];
self::assertEquals(48, $row->getValue(Column::COLUMN_FIRST_DOMAIN));
self::assertEquals(22, $row->getValue(Column::COLUMN_SECOND_DOMAIN));
self::assertEquals(0, $row->getValue(Column::COLUMN_THIRD_DOMAIN));
}
}
11 changes: 11 additions & 0 deletions test/ResponseExample/domain_domains.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Keyword;nike.com;adidas.com;reebok.com;Domain4 Pos;Domain5 Pos;Number of Results;CPC;Search Volume;Keyword Difficulty;Competition;Trends
shoes;7;29;0;0;0;3830000000;0.73;1220000;100.00;1.00;0.67,0.67,0.55,0.67,1.00,1.00,0.81,0.55,0.81,0.81,0.81,0.81
slides;48;22;0;0;0;3700000000;2.44;1220000;100.00;0.21;1.00,1.00,0.45,0.30,0.37,0.81,1.00,1.00,0.81,0.81,0.81,0.67
slide;85;75;0;0;0;3510000000;0.85;823000;97.00;0.15;1.00,0.82,0.67,0.82,0.82,0.82,0.82,0.45,0.37,0.45,0.67,0.82
golf;86;59;0;0;0;4200000000;1.00;673000;100.00;0.07;0.55,0.45,0.45,0.45,0.67,0.67,1.00,0.82,0.82,0.82,0.67,0.55
nike outlet;1;70;0;0;0;459000000;0.74;673000;80.00;0.98;0.82,1.00,0.67,0.55,0.82,0.82,0.67,0.55,0.67,0.67,0.45,0.45
hoka shoes;44;56;0;0;0;3300000000;1.43;550000;79.00;1.00;0.45,0.45,0.55,0.55,0.82,0.82,0.82,0.82,1.00,1.00,1.00,1.00
sneakers;2;30;0;0;0;2030000000;0.97;550000;98.00;0.97;0.67,0.67,0.55,0.45,0.67,0.82,0.67,0.67,0.82,1.00,0.82,0.82
hoodie;16;12;0;0;0;1090000000;1.01;450000;71.00;1.00;1.00,1.00,0.55,0.45,0.45,0.37,0.30,0.24,0.37,0.45,0.55,0.82
men;22;62;0;0;0;4360000000;0.62;450000;93.00;0.02;1.00,0.82,0.82,0.82,0.82,0.82,0.82,0.82,0.82,0.82,0.82,0.82
sandals;71;68;0;0;0;1010000000;9.45;450000;92.00;1.00;0.24,0.24,0.30,0.30,0.67,1.00,1.00,1.00,0.82,0.67,0.45,0.37

0 comments on commit cb72fcf

Please sign in to comment.